Sorting from highest to lowest for a particular column
5 views (last 30 days)
Show older comments
I have A (3*5) matrix. I have B(14*6) matrix.
Now I want C (14*5) matrix from A and B in such a way that column 2 and 3 for each row of matrix B will be replaced by corresponding cell value of A.
For example – column 2 and 3 of 1st row of B is 1 and 4. Then we need to find the value of A(1,4) which is 10. This 10 will be second column value for 1st row of matrix C.
C(:,1)= B(:,1) ; C(:,2) = from above condition; C(:,3) = B(:,4); C(:,4) = B(:,5) ; C(:,5) = B(:,6)
Eventually, I want matrix D(14*5) from C with the second column value sorted from highest to lowest. Then all the rows in column 1, 3, 4, and 5 will be according to corresponding second column.
Can anyone please help me how to get this D matrix from A and B? I have attached A, B, C, and D matrix here for the clarification.
My B matrix row number is much larger in real case. I just made it smaller here to simplify the problem. Matrix A (3*5) is fixed.
Thanks in advance.
A=[13 12 11 10 3
13 9 8 7 2
13 6 5 4 1];
B=[1 1 4 0 3 2
2 1 2 2 0 2
3 1 2 0 5 0
4 2 2 3 2 0
5 3 5 3 0 2
6 3 4 0 0 5
7 2 5 3 2 0
8 2 4 0 3 0
9 2 3 0 2 3
10 3 2 0 3 2
11 3 3 0 0 5
12 1 3 0 5 0
13 1 1 1 2 2
14 1 5 0 2 2];
C=[1 10 0 3 2
2 12 2 0 2
3 12 0 5 0
4 9 3 2 0
5 1 3 0 2
6 4 0 0 5
7 2 3 2 0
8 7 0 3 0
9 8 0 2 3
10 6 0 3 2
11 5 0 0 5
12 11 0 5 0
13 13 1 2 2
14 3 0 2 2];
D=[13 13 1 2 2
2 12 2 0 2
3 12 0 5 0
12 11 0 5 0
1 10 0 3 2
4 9 3 2 0
9 8 0 2 3
8 7 0 3 0
10 6 0 3 2
11 5 0 0 5
6 4 0 0 5
14 3 0 2 2
7 2 3 2 0
5 1 3 0 2];
0 Comments
Accepted Answer
Stephen23
on 5 Apr 2019
Edited: Stephen23
on 5 Apr 2019
>> X = sub2ind(size(A),B(:,2),B(:,3));
>> C = [B(:,1),A(X),B(:,4:end)]
C =
1 10 0 3 2
2 12 2 0 2
3 12 0 5 0
4 9 3 2 0
5 1 3 0 2
6 4 0 0 5
7 2 3 2 0
8 7 0 3 0
9 8 0 2 3
10 6 0 3 2
11 5 0 0 5
12 11 0 5 0
13 13 1 2 2
14 3 0 2 2
>> D = sortrows(C,-2)
D =
13 13 1 2 2
2 12 2 0 2
3 12 0 5 0
12 11 0 5 0
1 10 0 3 2
4 9 3 2 0
9 8 0 2 3
8 7 0 3 0
10 6 0 3 2
11 5 0 0 5
6 4 0 0 5
14 3 0 2 2
7 2 3 2 0
5 1 3 0 2
3 Comments
Stephen23
on 6 Apr 2019
B = nan(size(A,1),size(A,2));
for i=1:size(A,1)
B(:,1:end) = A(:,1:end);
end
the loop is totally superlfluous, because the indexing (:,1:end) already access all rows of those arrays. How to use indexing on arrays is a basic MATLAB concept you need to learn:
I do not understand your description, e.g. " then the other elements of that row should be 0" what are the "other elements" ? Probably the simplest is if you provide the expected output using your example data.
More Answers (1)
per isakson
on 5 Apr 2019
Edited: per isakson
on 5 Apr 2019
Try this
%%
C1 = nan( size( B(:,1:end-1) ) );
%%
for rr = 1 : size(B,1)
C1( rr, 2 ) = A( B(rr,2), B(rr,3) );
end
C1(:,[1,3,4,5]) = B(:,[1,4,5,6]);
%%
D1 = sortrows( C1, 2, 'descend' );
The names C1 and D1 to avoid overwriting C and D
9 Comments
per isakson
on 7 Apr 2019
Edited: per isakson
on 7 Apr 2019
The sum of the rows in this particular A, satisfy sum(A(rr,:)) <= 5 for all rr. However, the algorithm cannot be based on that - or ?
I'm prepared to throw in the towel
%%
csA2 = cumsum( A, 2 );
is_row_cumsum_gt_5 = csA2 > 5;
%
csA1 = cumsum( A, 1 );
is_col_cumsum_gt_25 = csA1 > 25;
%%
B1 = A;
for rr = 2 : size( A, 1 )
% change what's needed to fullfil the conditions
% I envision a lot of edge cases resulting in as
% many if-statements, which in turn requires a lot
% of testing
end
See Also
Categories
Find more on Mathematics and Optimization 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!