Generating matrix from another array

2 visualizaciones (últimos 30 días)
lilly lord
lilly lord el 10 de Sept. de 2020
Comentada: lilly lord el 11 de Sept. de 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 comentario
BOB MATHEW SYJI
BOB MATHEW SYJI el 10 de Sept. de 2020
Hi, Can you please elaborate the question?

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 10 de Sept. de 2020
Editada: Stephen23 el 10 de Sept. de 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 comentario
lilly lord
lilly lord el 11 de Sept. de 2020
It works well. I got the required result. Thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by