assign the same vector to be the same cell
Mostrar comentarios más antiguos
Let's say, I have the matrix:
A=[x,y]=[1 2;1.1 2;1.2 2;1 3;1.1 3;1.2 3;1 4;1.1 4;1.2 4];

If i wanna group all vector having the same value of y coordinate. How can I do that?
Example, the result like that:
Cell1=[1 2
1.1 2
1.2 2]
Cell2=[1 3
1.1 3
1.2 3]
Cell3=[1 4
1.1 4
1.2 4]
2 comentarios
madhan ravi
el 23 de Nov. de 2018
People here put some efforts to help you and you mercilessly close the question without clarifying how rude
ha ha
el 24 de Nov. de 2018
Respuesta aceptada
Más respuestas (2)
Andrei Bobrov
el 21 de Nov. de 2018
0 votos
Cell = mat2cell(A,accumarray(findgroups(A(:,2)),1),size(A,2));
Can be done easily with findgroups (or the older unique) and splitapply (or the older accumarray), in just one line:
A = [1 2;1.1 2;1.2 2;1 3;1.1 3;1.2 3;1 4;1.1 4;1.2 4];
C = splitapply(@(rows) {A(rows, :)}, (1:size(A, 1))', findgroups(A(:, 2)));
celldisp(C)
with unique and accumarray, you need two lines as you need the 3rd return value of unique:
A = [1 2;1.1 2;1.2 2;1 3;1.1 3;1.2 3;1 4;1.1 4;1.2 4];
[~, ~, id] = unique(A(:, 2));
C = accumarray(id, (1:size(A, 1))', [], @(rows) {A(rows, :)});
celldisp(C)
Categorías
Más información sobre Data Exploration en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!