How to split divide an array on specific sections?

15 ビュー (過去 30 日間)
Jorge Rodriguez
Jorge Rodriguez 2017 年 8 月 16 日
コメント済み: Jorge Rodriguez 2017 年 8 月 17 日
I have a double array of dimension (10000,5), the first column of the array is an ID going from 1-10. The other 4 columns are the related data. I'm trying to create individual arrays based on each ID value and the quantity of each ID can vary with respect to each other.
%For example:
%CA is the vector with the data =(ID,DATA1,DATA2,DATA3,DATA4)
%ID goes from 1-10
%The first array should be GC1=(1,DATA1,DATA2,DATA3,DATA4) with "x" number of rows
%I have tried to make this code work but I don't know what's wrong? I have a loop to create 11 array GC(id), although i now is not recommended.
k =size(ID,1); %size of vector, size of data base
x=0;
jl=1;
CAA=zeros(k,1);
CAA=CA(:,1); %extraction of the ID column from CA
for id=1:10
for i=jl:k
if CAA(i,1)==1
x=x+1;
else
GC(id)=zeros(x,5);
GC(id)=CAA(jl:x,5);
end
end
end

採用された回答

Sebastian Castro
Sebastian Castro 2017 年 8 月 16 日
編集済み: Sebastian Castro 2017 年 8 月 16 日
You can do this with logical indexing. Say, for example, you want to grab the data whose ID corresponds to 5:
% Generate dummy data
% First column is a random number 1-10
data = [randi(10,100,1) rand(100,4)];
% Extract the data from those with ID #5
subset5 = data(data(:,1)==5,:);
- Sebastian
  3 件のコメント
Jorge Rodriguez
Jorge Rodriguez 2017 年 8 月 16 日
Sebastian, do you know id there is a way to create the vector subset in a loop. In other words subset(i)? this is just in case the ID varies from more than 1 to 10? Thanks
Sebastian Castro
Sebastian Castro 2017 年 8 月 16 日
Andrei's answer below is the way to go!

サインインしてコメントする。

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 8 月 16 日
GC = accumarray(data(:,1),(1:size(data,1))',[],@(x){data(x,:)});
  1 件のコメント
Jorge Rodriguez
Jorge Rodriguez 2017 年 8 月 17 日
Thanks Andrei, it works perfectly for what i wanted

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by