Creating an array based off column value

12 ビュー (過去 30 日間)
lsutiger1
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
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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by