Sorting from highest to lowest for a particular column
Afficher commentaires plus anciens
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];
Réponse acceptée
Plus de réponses (1)
per isakson
le 5 Avr 2019
Modifié(e) : per isakson
le 5 Avr 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 commentaires
GMDI
le 5 Avr 2019
Modifié(e) : per isakson
le 6 Avr 2019
per isakson
le 6 Avr 2019
I don't get it
- you want to create a function, B = foo( A ); A is input and B is output
- foo implements the first and second condition
- the sizes of A and B are equal
- the conditions have the output B(:,3:end) as input
What have I missed?
GMDI
le 6 Avr 2019
Modifié(e) : per isakson
le 6 Avr 2019
per isakson
le 7 Avr 2019
@Gazi Iqbal: In your April 5 comment you wrote "1st condition is:::: sum(B(for each row,3:end)) <=5…if it is already greater than 5 , then the other elements of that row should be 0." Your April 6 comment make me think it should have been sum( A (for each row,3:end)). And the same mistake in condition 2.
.. more to come
GMDI
le 7 Avr 2019
per isakson
le 7 Avr 2019
Modifié(e) : per isakson
le 7 Avr 2019
@Gazi Iqbal: "A(13,1:end)=[3 2 0] …but to satisfy summation of each column <=25 , I had to make it in B as B(13,1:end)=[5 0 0]" couldn't that result in sum(B(:,1) > 25 ? (With another set of input data.)
GMDI
le 7 Avr 2019
per isakson
le 7 Avr 2019
Modifié(e) : per isakson
le 7 Avr 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
GMDI
le 7 Avr 2019
Modifié(e) : per isakson
le 7 Avr 2019
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!