Generating matrix from another array

2 vues (au cours des 30 derniers jours)
lilly lord
lilly lord le 10 Sep 2020
Commenté : lilly lord le 11 Sep 2020
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
  1 commentaire
BOB MATHEW SYJI
BOB MATHEW SYJI le 10 Sep 2020
Hi, Can you please elaborate the question?

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 10 Sep 2020
Modifié(e) : Stephen23 le 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
  1 commentaire
lilly lord
lilly lord le 11 Sep 2020
It works well. I got the required result. Thanks

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by