필터 지우기
필터 지우기

how to look for repetions in a matrix with condition on the last column

조회 수: 2 (최근 30일)
didi
didi 2018년 2월 26일
댓글: Stephen23 2018년 2월 26일
I have a matrix A like this:
[0 1 0 2 1 ;
0 1 1 2 0 ;
0 0 1 2 1 ]
I would like to obtain a vector filled with the column position where there is a same number for a certain condition on the last column...in other words: look in the A(1:4, columns where A(:,5)==1) which correspond to
A(1,1:4) = [0 1 0 2] and
A(3,1:4) = [0 0 1 2]
and find the position where there is the same number; save in a vector 1 and 4. thanks!

채택된 답변

KL
KL 2018년 2월 26일
You just need to use indexing with find to get the indices of the rows that fullfil your condition,
A = [0 1 0 2 1 ;
0 1 1 2 0 ;
0 0 1 2 1 ];
ind = find(A(:,end)==1);
res = A(ind,1:end-1)
here ind has the row numbers of the matrix A that satisfy your condition and res extract the needed elements from the respective rows.
ind =
1
3
res =
0 1 0 2
0 0 1 2
  댓글 수: 1
Stephen23
Stephen23 2018년 2월 26일
didi'a "Answer" moved here:
Thanks a lot! But i need to search also for the repetion in the res matrix....and create a vector with the column indexes where the repetions happen...in this case vector with only one elemnt,1, because only in column 1 i have a repetion of 0.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sources and Sinks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by