How to repeat a rectangular matrix in matlab?
2 次查看(过去 30 天)
显示 更早的评论
How to repeat a rectangular matrix in matlab?
Not using loops, just matlab's build-in commands.
Thanks a lot!
0 个评论
采纳的回答
Star Strider
2014-7-24
编辑:Star Strider
2014-7-24
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 个评论
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
2014-7-24
编辑:Azzi Abdelmalek
2014-7-24
A=[1 2 ; 3 4]
B=repmat(A,3,2)
3 个评论
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.
Andrei Bobrov
2014-7-24
编辑:Andrei Bobrov
2014-7-24
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));
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!