Info

此问题已关闭。 请重新打开它进行编辑或回答。

Split one column data into 4 arrays

1 次查看(过去 30 天)
Francisco Anaya
Francisco Anaya 2019-4-3
关闭: MATLAB Answer Bot 2021-8-20
I have an array of 90872x1 double with 4 conditions. As follows:
A=
1
1
1
2
2
2
3
3
4
4
1
1
2
2
3
3
4
4
1
1
1
I want to split the vector into 4 arrays, one per condition. Each column of the new 4 arrays must contain de data of the conditions. Therefore.
A1=
1 1 1
1 1 1
1 1
A2=
2 2
2 2
2
A3=
3 3
3 3
A4=
4 4
4 4
Any help?
  3 个评论
Francisco Anaya
Francisco Anaya 2019-4-3
I used the following and worked well...
[C,ia,ib]=unique(B); %groups
G=length(C);
iwant=cell(G,1);
for j=1:G
A=(B==C(j))';
jj=zeros(size(A));
ii=A==1;
jj(strfind([0,ii(:)'],[0 1]))=1;
idx=cumsum(jj).*ii;
iwant{j}=accumarray(idx(ii)',B(ii)',[],@(x){x'});
end
Walter Roberson
Walter Roberson 2019-4-3
That creates a cell array, but your question requires a numeric array as output.

回答(1 个)

KSSV
KSSV 2019-4-3
A=[1
1
1
2
2
2
3
3
4
4
1
1
2
2
3
3
4
4
1
1
1] ;
C = unique(A) ;
iwant = cell(length(C),1) ;
for i = 1:length(C)
iwant{i} = A(A==C(i)) ;
end

此问题已关闭。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by