Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Abr. de 2019
find(C0(:,2) == 4 & C0(:,3) == 3)
  3 comentarios
Walter Roberson
Walter Roberson el 28 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by