Generating matrix from another array

1 view (last 30 days)
I am tring to generate a matrix S1,S2,S3 from an array E3 .
All three matrices should contain numbers from 0 to 25 but in the order it appears in E3, i.e in first S box 2,1,0,4,25,29 goes to S1 and for another 2 it will be added to S2, and for the next 2 it will check S1,S2 both and as both contain 2 so it will be added in S3. The code gives error so if anyone can help me in this regard.
E3=[2 1 0 4 25 19 2 2 3 3 4 5 6 7 8 7 19 .....];%800 numbers but value less than 26
S1=zeros(1,25); % for generating 5*5 mATRIX
S2=zeros(1,25);S3=[];
for i=1:lengtH(E3)
S1(i)=E3(i);
if ismember(S1,E3(i))
S2(I)=E3(i);
if ismember(S2,E3(i))
S3(i)=E3(i)
end
end

Accepted Answer

Stephen23
Stephen23 on 10 Sep 2020
Edited: Stephen23 on 10 Sep 2020
>> E3 = [2,1,0,4,25,19,2,2,3,3,4,5,6,7,8,7,19];
>> X = diag(cumsum(bsxfun(@eq,E3,E3(:)),1));
>> C = arrayfun(@(n)E3(X==n),1:max(X),'uni',0);
>> C{1}
ans =
2 1 0 4 25 19 3 5 6 7 8
>> C{2}
ans =
2 3 4 7 19
>> C{3}
ans =
2

More Answers (0)

Community Treasure Hunt

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

Start Hunting!