Clear Filters
Clear Filters

Replace sub matrices in the correspondant matrix (MATLAB)

2 views (last 30 days)
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!

Accepted Answer

Voss
Voss on 10 Mar 2022
Edited: Voss on 10 Mar 2022
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

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!