How to repeat a rectangular matrix in matlab?

How to repeat a rectangular matrix in matlab?
Not using loops, just matlab's build-in commands.
Thanks a lot!

 採用された回答

Star Strider
Star Strider 2014 年 7 月 24 日
編集済み: Star Strider 2014 年 7 月 24 日

0 投票

This works:
a = [1 1 1 1; 2 2 2 2];
A = zeros(6);
for k1 = 1:2:size(A,1)-1
A(k1:k1+1, k1:k1+3) = a;
end
A % Show Result

6 件のコメント

rui
rui 2014 年 7 月 24 日
Hi there, is there any way that I can avoid using loop? Thanks.
Star Strider
Star Strider 2014 年 7 月 24 日
編集済み: Star Strider 2014 年 7 月 24 日
Not that I can think of. Every approach requires a loop because of the way you want your matrix.
Here’s a more efficient way to do it, but it still requires a loop:
a = [1 1 1 1; 2 2 2 2];
A = [a zeros(2,4)];
A1 = A;
for k1 = 1:2
A1 = [A1; circshift(A, [2 2]*k1)];
end
A1 % Show Result
rui
rui 2014 年 7 月 24 日
編集済み: rui 2014 年 7 月 24 日
The matrix that I am using actually has millions of entries, I'm worried that using loop will significantly prolong the process, but thanks again.
Star Strider
Star Strider 2014 年 7 月 24 日
編集済み: Star Strider 2014 年 7 月 24 日
Loops really aren’t evil! The second version is more efficient than the first, but there is no built-in function that will do what you want. I didn’t preallocate the result matrices in my second example because they’re small. Preallocating your millions-dimension result matrices as in my first example as:
A = zeros(6,8);
will significantly speed up the process. I would favour my first example because of that.
rui
rui 2014 年 7 月 24 日
I used circshift, sparse and loops in my code but it still takes a very long time to obtain the results, and I've been searching all day but with no luck, so I guess that loop is inevitable in this situation. Thank you so much for your help, I really appreciate it!
Star Strider
Star Strider 2014 年 7 月 24 日
My pleasure!
The circshift approach (that I used) expands the matrix with each step. That takes more time, because MATLAB has to allocate new memory each time.
I suggest using my first approach and preallocating the matrix. That eliminates the problem of expanding the matrix at each step, eliminates the call to circshift, and produces the same result.

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

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 24 日
編集済み: Azzi Abdelmalek 2014 年 7 月 24 日

0 投票

A=[1 2 ; 3 4]
B=repmat(A,3,2)

3 件のコメント

rui
rui 2014 年 7 月 24 日
編集済み: rui 2014 年 7 月 24 日
Hi there, A is a square matrix, but I want to know how to repeat a rectangular matrix, and automatically fill other positions with zeros, thanks.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 24 日
You didn't say anything about how do you want to shift your matrix? it's not just repeating a matrix.
rui
rui 2014 年 7 月 24 日
My mistake. Shifting the matrix by 2 positions in 2-dimensions. At the beginning there is a=[1 1 1 1;2 2 2 2], after doing it 3 times without using any loops (just using build-in commands), it becomes a 6-by-8 matrix as shown in the picture. Those extra positions are filled by 0s automatically. Thanks again.

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

Andrei Bobrov
Andrei Bobrov 2014 年 7 月 24 日
編集済み: Andrei Bobrov 2014 年 7 月 24 日

0 投票

for your case:
t = zeros(6,2);
out = [kron(eye(3),a(:,1:2)),t]+[t,kron(eye(3),a(:,3:4))];
variant
m = 3;
k=2;
s = size(a);
n = (m-1)*k+s(2);
m1 = m*s(1);
out = zeros(m1,n);
t = sub2ind([m1,n],1:s(1):m1,1:k:k*m);
t2 = bsxfun(@plus,(0:s(2)-1)*m1,(0:s(1)-1)');
out(bsxfun(@plus,t,t2(:))) = a(:,:,ones(m,1));

1 件のコメント

rui
rui 2014 年 7 月 25 日
This looks quite awesome, thanks!

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

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

rui
2014 年 7 月 24 日

コメント済み:

rui
2014 年 7 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by