フィルターのクリア

How to replicate an array diagonally for a given number of times?

2 ビュー (過去 30 日間)
Ongun
Ongun 2014 年 11 月 11 日
コメント済み: yeungor 2018 年 7 月 23 日
I would like to replicate an array diagonally for a given number of times, so repmat command does not work in my case. I also tried blkdiag, but I have to write the name of the matrix 5 times if I want it to be recreated five times. I heard that it can be done with convolution from a friend of mine, yet my aim is to achieve the effect using basic commands.

採用された回答

the cyclist
the cyclist 2014 年 11 月 11 日
編集済み: the cyclist 2014 年 11 月 11 日
Here is a very mundane way to do it. (I've assumed a square input, but it could easily be generalized.)
A = magic(5);
m = size(A,1);
N = 3;
AN = zeros(N*m);
for n=1:N
range = (n-1)*m+1:n*m;
AN(range,range) = A;
end
  1 件のコメント
Ongun
Ongun 2014 年 11 月 11 日
Thanks it worked my friend suggested the following:
conv2(diag([upsample(ones(1,N-1),size(sub2D,1)),1]),sub2D);
with N being the number of replications, but yours are much easier to interpret.

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

その他の回答 (1 件)

yeungor
yeungor 2018 年 7 月 9 日
Alternatively, if you're not against "eval", you can use it to type out those repetitions n times.
a = rand(m,n);
N = 100;
A = eval(['blkdiag(a' repmat(',a', 1, N) ');']);
But the convolution way seems like less of a hack and it's still one line, it's a shame that it's black magic
  1 件のコメント
yeungor
yeungor 2018 年 7 月 23 日
a = rand(m,n);
A = kron(eye(N), a)
Also works quite well

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by