Creating an array based off column value

6 次查看(过去 30 天)
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?

采纳的回答

Stephen23
Stephen23 2017-3-29
编辑:Stephen23 2017-3-29
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 个评论
lsutiger1
lsutiger1 2017-3-29
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by