how to select the highest values form each column and set the other values to zero.

조회 수: 1 (최근 30일)
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
Raj
Raj 2019년 3월 29일
Looks pretty straight forward. However, can you first share what you have tried so far on this?
jaah navi
jaah navi 2019년 3월 29일
when I tried with the command max(A) it gives only one maximum value from each column.
But i am unable to get the output what i want.that was the reason of posting my question

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 3월 29일
편집: Andrei Bobrov 2019년 3월 29일
[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개)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by