how to look for repetions in a matrix with condition on the last column
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!