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
  2 件のコメント
high speed
high speed 2022 年 3 月 10 日
@_ Thank you so much! it works
Voss
Voss 2022 年 3 月 10 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by