January 30, 2006

Question time

Last time I used C++ was quite a while ago, and I need help. Serious help. I built a simple but wonderful program in MATLAB, but I'll need better performance soon, especially as I can't wait for more than 2 years to have it perform operations on arrays with more than 10,000 entries.

So I'm trying to rewrite it in C++, assuming that this more advanced language is more powerful. And forgetting that those lovely simple algorithms in MATLAB such as sortrows, and randn, and variable array sizes are more involved to implement in C++.

So here I am calling the Warwick blogosphere for help. I've got a 10,000×3 array [for instance] and want to sort the first column in ascending order. I'd also like to delete rows without having to rearrange the matrix each time [rows are deleted quite often] and add rows similarly. I'm tired of using Google and c++.com and what not, and the book I have is only introductory. If you can help me understand what to do, I… don't know what I'll do, but I'd extremely grateful to say the least!

- 7 comments by 1 or more people Not publicly viewable

[Skip to the latest comment]
  1. My suggestion would be to look into the standard template library (STL), which has built in classes and algorithms for this sort of thing. I think there's a postgrad computing seminar at the maths dept this week on C++ which will go into STL programming a bit, you should probably turn up.

    30 Jan 2006, 16:02

  2. Yeah I thought I should too. It seems too much of a coincidence that now I finally get to campus in the morning every day, and now I've finally started working with C++ again, that this specific seminar is organized. I think the STL gave me the sort() algorithm, but I can't see how it works for multidimensional arrays…

    30 Jan 2006, 16:15

  3. My suggestion would be to keep it in matlab and talk nicely to the guys at CSC link and they may let you have remote access to one of the clusters which will run your programs much quicker.

    30 Jan 2006, 17:32

  4. Yeah, you'd need to do some fiddling to get it to work for multidimensional arrays. I think you can pass an overloaded < operator to the sort() algorithm, you might also be able to pass it an overloaded swap() function (it might not work like this, I'm going from memory here), in which case it wouldn't be too much work. If you can't pass it a swap function, you could write your own container that extends one of the standard ones, but that might be more heavy duty C++ than you want to get into.

    31 Jan 2006, 03:36

  5. Something like this should work (I haven't tested it):

    class threecol

    double a[3];

    double operator[](int i) { return a[i]; }

    bool threecol_compare(const threecol& tc1, const threecol& tc2)

    return tc1[1] < tc2[1] ? true : false;

    list<threecol> l;


    31 Jan 2006, 03:57

  6. I hope you didn't stay up late just to find this solution! I'm gonna go to the seminar in a minute now, but looking at the documentation provided and looking at your last comment, I'm afraid my current C++ knowledge is a bit too limited. Guess I'll have to find me a better book than An Introduction to C++ and Numerical Methods

    31 Jan 2006, 10:48

  7. Heh, no I just realised after I wrote comment 4 that it was probably quite simple, so I quickly googled for "c++ sort predicate STL" or something like that and it took me about 10 minutes to work out the solution. It's really not that complicated, the class "threecol" is just an array of 3 doubles, I should probably have called it "row" since thats what it represents. The operator[] thing just means that if tc is a threecol then you can write tc[i] instead of tc.a[i]. The variable l is just a linked list (so you can add or remove rows very quickly) of these threecols, so if you had 10 000 items in the linked list l then it would be an array of 10 000 rows and 3 columns. The function threecol_compare() passed to sort() just sorts by the first element of the array in threecol, i.e. it sorts the array by the first column. When you write a<b?true:false it means return true if a<b otherwise return false, it's just a shorthand.

    I'd recommend Stroustrup's book "The C++ Programming Language" if you've already got an introductory book. It has just about everything in it and its reasonably easy to read (and you can't argue with a book written by the guy that invented C++).

    31 Jan 2006, 11:09

Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.

Search this blog



Most recent comments

  • Nice banner meht! by on this entry
  • Now if only they could convict the show's directors for that annoying countdown and excessive editin… by on this entry
  • Whatever by Sue on this entry
  • i have to say your are handsome!!even if i dont know you your pics are catchy and full of questions … by sab on this entry
  • 1. Fact: I used to work as a security guard in the National Portrait Gallery in Wales. I loved it, s… by Hayley on this entry
Not signed in
Sign in

Powered by BlogBuilder