Creating an array based off column value

12 Ansichten (letzte 30 Tage)
lsutiger1
lsutiger1 am 29 Mär. 2017
Kommentiert: lsutiger1 am 29 Mär. 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?

Akzeptierte Antwort

Stephen23
Stephen23 am 29 Mär. 2017
Bearbeitet: Stephen23 am 29 Mär. 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 Kommentar
lsutiger1
lsutiger1 am 29 Mär. 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by