The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Sorting a Cell Array with Multiple Data types
Replies: 4   Last Post: May 17, 2012 4:41 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 107
Registered: 2/27/10
Re: Sorting a Cell Array with Multiple Data types
Posted: May 16, 2012 2:52 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Gregory" wrote in message <jp06q2$dqk$>...
> I have a table of data stored as a cell array that I need to sort into an order based on part of one the rows.
> Here is the table: (called "Table")
> 'MAR - Groups Total' 'CE1' 'CE2' 'CE3' 'CE5' 'CE6' 'CE7' 'CE8' 'Total'
> 'Forecast' [27.6900] [62.1000] [88.0900] [62.4000] [73.5400] [ 94.2500] [80.2600] [488.3300]
> 'Actual' [29.0600] [69.8500] [92.9400] [61.2700] [82.9100] [112.8000] [82.6100] [531.4400]
> 'Difference' [-1.3700] [-7.7500] [-4.8500] [ 1.1300] [-9.3700] [-18.5500] [-2.3500] [-43.1100]
> What I want to do is ignore the first and last column, which contain the row titles and the totals, and sort by the absolute value of row 4. Then re order the rest of the rows based on the results of the sorting.
> for example. The lowest absolute difference is for group 'CE5' in column 5, so this should move to the start, column 2. The next smallest is 'CE1' which should be in column 3, then 'CE8' which should be in column 4 etc.
> I can do:
> sort(abs(cell2mat(Table(4,2:end-1))))
> which sort into the correct order, but then how do I resort the rest of the data in the table based on this.
> Thanks for you help!

Sort can also output an index of the element locations before sorting. You can use this index to arrange the other rows in a similar manner:

[B,IX] = sort(A,...) also returns an array of indices IX, where size(IX) == size(A). If A is a vector, B = A(IX). If A is an m-by-n matrix, then each column of IX is a permutation vector of the corresponding column of A, such that

for j = 1:n
B(:,j) = A(IX(:,j),j);

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.