필터 지우기
필터 지우기

How to retrieve index in a matrix

조회 수: 1 (최근 30일)
Danish Nasir
Danish Nasir 2022년 6월 28일
댓글: Voss 2022년 6월 28일
Suppose a matrix A= [20 100 35 50
30 30 25 40
40 5 45 15
50 4 20 20]
After [B,I]=sort (A,’ascend’), I get the index matrix
I= [ 1 4 4 1
2 3 2 3
3 2 1 4
4 1 3 2 ]
Now I want to make a matrix C which have only one index (corrsponds to minima) of each column I which is non repeating
C=[ 1 4 2 3] . The selection of these index will give column wise minima with no repeatation. How to make matrix C?
  댓글 수: 2
Jonas
Jonas 2022년 6월 28일
i do not fully understand, if you want the index of ea column of minimum element, you can use [~,idx]=min(A), but it gives [1 4 4 3]
Danish Nasir
Danish Nasir 2022년 6월 28일
I do not want repetition of index . C matrix should have unique elements. Since there is a tie between 2nd and 3rd column , so 3rd column index is 2. Now only 3 left as unique index, so 4th cloumn index is 3

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

채택된 답변

Voss
Voss 2022년 6월 28일
편집: Voss 2022년 6월 28일
One way is to find the index of the minimum in each column of A one at a time, setting that row of A to NaN each time so that that index will not be found again for subsequent columns.
A= [20 100 35 50
30 30 25 40
40 5 45 15
50 4 20 20];
C = zeros(1,size(A,2))
C = 1×4
0 0 0 0
A_temp = A
A_temp = 4×4
20 100 35 50 30 30 25 40 40 5 45 15 50 4 20 20
for ii = 1:size(A_temp,2)
% output displayed in command window
% so you can see the process
[~,C(ii)] = min(A_temp(:,ii))
A_temp(C(ii),:) = NaN
end
C = 1×4
1 0 0 0
A_temp = 4×4
NaN NaN NaN NaN 30 30 25 40 40 5 45 15 50 4 20 20
C = 1×4
1 4 0 0
A_temp = 4×4
NaN NaN NaN NaN 30 30 25 40 40 5 45 15 NaN NaN NaN NaN
C = 1×4
1 4 2 0
A_temp = 4×4
NaN NaN NaN NaN NaN NaN NaN NaN 40 5 45 15 NaN NaN NaN NaN
C = 1×4
1 4 2 3
A_temp = 4×4
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
C
C = 1×4
1 4 2 3
  댓글 수: 2
Danish Nasir
Danish Nasir 2022년 6월 28일
Thanx a lot....its great....
Voss
Voss 2022년 6월 28일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by