how to select the highest values form each column and set the other values to zero.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
jaah navi
am 29 Mär. 2019
Bearbeitet: Andrei Bobrov
am 29 Mär. 2019
If
A=[0.1290 0.2880 0.0193 0.2064 0.5245 0.5067 0.0255 0.2789;
0.2721 0.3500 0.0916 0.0913 0.5780 0.4616 0.3746 0.4651;
0.2011 0.2672 0.3051 0.1836 0.5149 0.3960 0.3730 0.3996;
0.0610 0.0374 0.0366 0.0453 0.0633 0.2699 0.2800 0.2522]
I want to generate the matrix from A such that it should contain the highest two values present in each column and the rest of values should be set to zero and it should diaplay in the following manner.
A=
[0 0.2880 0 0.2064 0.5245 0.5067 0 0;
0.2721 0.3500 0.0916 0 0.5780 0.4616 0.3746 0.4651;
0.2011 0 0.3051 0.1836 0 0 0.3730 0.3996;
0 0 0 0 0 0 0 0]
Could anyone please help me on this.
2 Kommentare
Raj
am 29 Mär. 2019
Looks pretty straight forward. However, can you first share what you have tried so far on this?
Akzeptierte Antwort
Andrei Bobrov
am 29 Mär. 2019
Bearbeitet: Andrei Bobrov
am 29 Mär. 2019
[m,n] = size(A);
[~,ii] = sort(A);
A(bsxfun(@plus,ii(1:end-2,:),(0:n-1)*m)) = 0;
or
[m,n] = size(A);
[~,ii] = sort(A);
A(ii(1:end-2,:) + (0:n-1)*m) = 0;
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!