Creating an array based off column value

12 visualizaciones (últimos 30 días)
lsutiger1
lsutiger1 el 29 de Mzo. de 2017
Comentada: lsutiger1 el 29 de Mzo. de 2017
I have an array, size 8809x3, that I would like to separate into three independent arrays based off of the value in Column 3.
Column 3 is either a 1, 2, or 3. So I would like to create three arrays, each of which only contain the rows of 1's, 2's, and 3's.
I have tried to use if statements and for loops, none of which I have been able to get to work. Do any of you know how I can do this?

Respuesta aceptada

Stephen23
Stephen23 el 29 de Mzo. de 2017
Editada: Stephen23 el 29 de Mzo. de 2017
Use logical indexing:
>> A = randi(3,5,3);
>> X = A(A(:,3)==1,:);
>> Y = A(A(:,3)==2,:);
>> Z = A(A(:,3)==3,:);
Although it may be more efficient to keep your data together in one array and simply access the parts of the array when required. In general you should keep data together as much as possible, rather than trying to split it apart. For example you could easily split the array into a cell array of matrices:
>> accumarray(A(:,3),1:size(A,1),[],@(n){A(n,:)})
ans =
[1x3 double]
[2x3 double]
[2x3 double]
  1 comentario
lsutiger1
lsutiger1 el 29 de Mzo. de 2017
I plan on keeping the data together in the original array, but need to be able to visualize each of them by themselves and manipulate it. It would just be easier to have copies of each one by itself. I am going to try this when I get back to my computer in the morning!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays 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