Replace sub matrices in the correspondant matrix (MATLAB)

조회 수: 2 (최근 30일)
high speed
high speed 2022년 3월 10일
댓글: Voss 2022년 3월 10일
Dear
I have this program for m=4 for example:
m=4;
P=hankel(1:m,m:-1:1);
a=zeros(m-1,m-1,m-2);
a(:,:,1)=eye(m-1);
for k=2:m-1
a(:,:,k)=circshift(a(:,:,k-1),1,2 );
Pm=zeros(m-1);
end
the correspondant matrix P is:
P =
1 2 3 4
2 3 4 3
3 4 3 2
4 3 2 1
where the sub matrix a which is an identity matrix is considered as '1' in the matrix P.
The other two sub-matrices which are shifts of a are considered as '2' and '3' respectively in P.
And Pm is considered as '4' in P.
I want to replace '1', '2', '3' and '4' in P with their correspondant sub matrices. And the result must be:
P =
1 0 0 0 1 0 0 0 1 0 0 0
0 1 0 0 0 1 1 0 0 0 0 0
0 0 1 1 0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 0 0 0 0 1
0 0 1 1 0 0 0 0 0 1 0 0
1 0 0 0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 1 0 0 0 0 1
0 1 0 0 0 0 0 1 0 1 0 0
0 0 0 0 0 1 0 1 0 1 0 0
0 0 0 1 0 0 0 0 1 0 1 0
0 0 0 0 1 0 1 0 0 0 0 1
How can I program this in general way for any m value please!

채택된 답변

Voss
Voss 2022년 3월 10일
편집: Voss 2022년 3월 10일
m=4;
P=hankel(1:m,m:-1:1);
a = zeros(m-1,m-1,m);
a(:,:,1) = eye(m-1);
for k = 2:m-1
a(:,:,k) = circshift(a(:,:,k-1),1,2);
end
a(:,:,m) = zeros(m-1);
P_new = zeros(m*(m-1));
for ii = 1:m
for jj = 1:m
P_new((m-1)*(ii-1)+(1:m-1),(m-1)*(jj-1)+(1:m-1)) = a(:,:,P(ii,jj));
end
end
disp(P_new);
1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by