Finding row numbers of a matrix where certain column entries match a criterion

2 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 24 Apr 2019
find(C0(:,2) == 4 & C0(:,3) == 3)
  3 Comments
Walter Roberson
Walter Roberson on 28 Apr 2019
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!