Shift and duplicate values in a data set

I have a data set of M=magic(4)
M =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
How can I have it so that you duplicate the data multiple times but also shift the whole thing one row to the right each time so that it would look like:
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
0 16 2 3
0 5 11 10
0 9 7 6
0 4 14 15
0 0 16 2
0 0 5 11
0 0 9 7
0 0 4 14

 採用された回答

Paul
Paul 2023 年 7 月 9 日

0 投票

Couple of options
M = magic(4);
Z = zeros(12,4);
for ii = 1:3
Z(4*(ii-1)+1:4*(ii-1)+4,:) = [zeros(4,ii-1) M(:,1:end-ii+1)];
end
Z
Z = 12×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 0 16 2 3 0 5 11 10 0 9 7 6 0 4 14 15 0 0 16 2 0 0 5 11
C{1} = M;
for ii = 2:3
C{ii} = [zeros(4,ii-1) M(:,1:end-ii+1)];
end
Z = vertcat(C{:})
Z = 12×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 0 16 2 3 0 5 11 10 0 9 7 6 0 4 14 15 0 0 16 2 0 0 5 11
Z = cellfun(@(M,ii) [zeros(4,ii-1) M(:,1:end-ii+1)],repmat({M},1,3),num2cell(1:3),'uni',false);
Z = vertcat(Z{:})
Z = 12×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 0 16 2 3 0 5 11 10 0 9 7 6 0 4 14 15 0 0 16 2 0 0 5 11

1 件のコメント

Daniel
Daniel 2023 年 7 月 9 日
Brilliant Paul thanks!!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

製品

リリース

R2023a

質問済み:

2023 年 7 月 9 日

コメント済み:

2023 年 7 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by