Getting largest to lowest values from a row

2 views (last 30 days)
rod schola
rod schola on 16 Feb 2015
Commented: rod schola on 17 Feb 2015
Hi, I have a matrix with 3 rows and 10 columns. I would like to use a loop to get the largest value of the second row first, then the lowest of the third row, then again largest value of the second but excluding the one previously selected. To summarize, I have this matrix [1 2 3 4 5 6 7 8 9 10; 5 6 7 8 9 10 11 12 13; 2 5 6 5 7 8 9 3 4 0] and I would like to use a loop to take 13 from second row first, then 0 from third, then 12 from the second row, then 2 from the third, etc...

Answers (2)

Roger Stafford
Roger Stafford on 16 Feb 2015
Let A be your original matrix.
B = [sort(A(2,:),'descend');sort(A(3,:),'ascend')];
B = B(:);
  3 Comments
Roger Stafford
Roger Stafford on 16 Feb 2015
That's what this code does! Have you tried it out? I tried it on your example and it produces:
B = [13;0;12;2;11;3;10;4;9;5;8;5;7;6;6;7;5;8;4;9]
just as you requested. (Note: I inserted a 4 in front of your second line because that line had only nine elements, and that 4 appears next to last in the twenty elements of B.)
rod schola
rod schola on 17 Feb 2015
I need to store the columns in a matrix though. Your code works for getting highest value of row 2 then lowest of row 3, etc... What I am looking for is to store the entire column of the largest value of row 2 in a matrix A, then the entire column of the lowest value in matrix B, then from the remaining 8 columns, the column of the largest value of row 2 in A, then from the remaining 7 columns, the column of the lowest value of column 3 in B. So I should end up with 2 matrices of 3 rows and 5 columns each.

Sign in to comment.


Image Analyst
Image Analyst on 16 Feb 2015
Do a loop over rows, then call sort(). Alternate between 'ascend' and 'descend' options.
  4 Comments
Image Analyst
Image Analyst on 16 Feb 2015
Or if you just want a column vector with the mins and maxes, that would be output(:,1) of the above code, or you can use this code to give a vector output instead of an array where you keep and sort all the elements:
clc;
m = [1 2 3 4 5 6 7 8 9 10;
5 6 7 8 9 10 11 12 13 0;
2 5 6 5 7 8 9 3 4 0]
[rows, columns] = size(m)
output = m(1,1); % Initialize
for row = 2 : rows
if rem(row, 2) == 0
% Row is even. Sort row with largest number first.
output(row) = max(m(row, :));
else
% Row is odd. Sort row with smallest number first.
output(row) = min(m(row, :));
end
end
% Print to command window:
output
rod schola
rod schola on 17 Feb 2015
Sorry I may have not explained what I want correctly. I have a matrix already, what I wanna do is not sort the rows, I want a loop that alternates between the second and third row, starting with the second, and that picks the largest value of the second along with its corresponding values from the first and third row, and then picks the smallest value from the third row with the corresponding values from first and second rows. To summarize, I have this matrix [1 2 3 4 5 6 7 8 9 10; .5 .6 .7 .8 .9 .1 .2 .3 .5 .6; 1 2 3 4 5 5 6 7 8 9] I want a loop to do the following, pick .9(being the largest in row 2 in this case), along with 5 from row 1 and 5 from row 3 (those 2 values being in the same column of .9) and store it in a matrix A, then pick 1 from row 3, along with 1 and .5 (again being in the same column ) and store it in another matrix B, and then picking the largest of the remaining values in row 2, and then the smallest of the remaining values in row 3. so I should end up with 2 matrices A and B, A having 3 rows and 5 columns, and B 3 rows and 5 columns as well. Thanks

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!