Generating matrix from another array

조회 수: 2 (최근 30일)
lilly lord
lilly lord 2020년 9월 10일
댓글: lilly lord 2020년 9월 11일
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
BOB MATHEW SYJI
BOB MATHEW SYJI 2020년 9월 10일
Hi, Can you please elaborate the question?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 9월 10일
편집: Stephen23 2020년 9월 10일
>> 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
lilly lord
lilly lord 2020년 9월 11일
It works well. I got the required result. Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by