Creating an array based off column value

조회 수: 3 (최근 30일)
lsutiger1 . 2017년 3월 29일
댓글: lsutiger1 . 2017년 3월 29일
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 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 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에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by