Getting largest to lowest values from a row

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)

Let A be your original matrix.
B = [sort(A(2,:),'descend');sort(A(3,:),'ascend')];
B = B(:);

3 Comments

What about alternating and getting the largest value of the second row and the lowest of the third?
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.)
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.

Do a loop over rows, then call sort(). Alternate between 'ascend' and 'descend' options.

4 Comments

I am not sure how to do this. Sorry I am new to Matlab
Try this:
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; % Initialize
for row = 2 : rows
if rem(row, 2) == 0
% Row is even. Sort row with largest number first.
output(row, :) = sort(m(row, :), 'descend');
else
% Row is odd. Sort row with smallest number first.
output(row, :) = sort(m(row, :), 'ascend');
end
end
% Print to command window:
output
Or if you're certain you have only 3 rows, use Roger's code.
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
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

Asked:

on 16 Feb 2015

Commented:

on 17 Feb 2015

Community Treasure Hunt

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

Start Hunting!