Finding row numbers of a matrix where certain column entries match a criterion
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a 64x3 matrix C0 as below. I would like to look at row 63=[4 4 3], and for each j=1:3, find the row numbers of C0 such that the not-j's column entries in that row equals the corresponding values in row 63. e.g. for j=1, find all rows of C0 such that the 2nd and 3rd values are [4 3]. (but would like a succinct way of writing this in a loop for all j)
Thanks for your help!
C0=[];
for R1=1:4
for R2=1:4
for R3=1:4
C0=[C0; [R1 R2 R3]];
end
end
end
댓글 수: 0
채택된 답변
Walter Roberson
2019년 4월 24일
find(C0(:,2) == 4 & C0(:,3) == 3)
댓글 수: 3
Walter Roberson
2019년 4월 28일
RowOfInterest = C0(64,:);
nc = size(C0,2);
vec = 1 : nc;
results = cell(nc,1);
mask = C0 == RowOfInterest;
for j = 1 : nc
nonj = vec; nonj(j) = [];
results{j} = find( all(mask(:,nonj), 2 ) );
end
The detection of matching rows could probably be vectorized (at the cost of temporary memory), but find() is difficult to vectorize.
추가 답변 (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!