Replace sub matrices in the correspondant matrix (MATLAB)

2 次查看(过去 30 天)
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 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by