Replace sub matrices in the correspondant matrix (MATLAB)

2 vues (au cours des 30 derniers jours)
high speed
high speed le 10 Mar 2022
Commenté : Voss le 10 Mar 2022
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!

Réponse acceptée

Voss
Voss le 10 Mar 2022
Modifié(e) : Voss le 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
  2 commentaires
high speed
high speed le 10 Mar 2022
@_ Thank you so much! it works
Voss
Voss le 10 Mar 2022
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by