如果想让矩阵A(m,n)的数据的每一行复制b遍,组成一个m*b行的大矩阵,可以用
B = A(reshape(ones(b,1)*(1:m),m*b,1),:);
转自https://blog.csdn.net/cckit/article/details/41501277
向量复制成矩阵常用方法是repmat
>> a = [2 3 4];
>> b = repmat(a, 3, 1)
b =
2 3 4
2 3 4
2 3 4
1
2
3
4
5
6
7
8
今天还发现了一种复制的方法:
>> c = a(ones(3, 1), :)
c =
2 3 4
2 3 4
2 3 4
>> d = [2 3 4]';
>> e = d(:, ones(3, 1))
e =
2 2 2
3 3 3
4 4 4
---------------------
作者:明歌天下
来源:CSDN
原文:https://blog.csdn.net/haiming_yeyeye/article/details/50733517
版权声明:本文为博主原创文章,转载请附上博文链接!
B = repmat(A,m,n)
B = repmat(A,[m n])
B = repmat(A,[m n p...])
这是一个处理大矩阵且内容有重复时使用,其功能是以A的内容堆叠在(MxN)的矩阵B中,B矩阵的大小由MxN及A矩阵的内容决定,如果A是一个3x4x5的矩阵,有B = repmat(A,2,3)则最后的矩阵是6x12x5
例如:
>>B=repmat( [1 2;3 4],2,3)
B =
1 2 1 2 1 2
3 4 3 4 3 4
1 2 1 2 1 2
3 4 3 4 3 4
其结果变为4X6。A也可以置放文字串,如:
>>C=repmat(' Long live the king!', 2,2)
C =
Long live the king! Long live the king!
Long live the king! Long live the king!
转自https://blog.csdn.net/haiming_yeyeye/article/details/50733517
复制向量为矩阵:
a = [1 2 3];
a(ones(1,3),:)
ans =
1 2 3
1 2 3
1 2 3
复制矩阵为矩阵:
a = [1 2;3 4];
a(:,:,ones(1,2))
ans(:,:,1) =
1 2
3 4
ans(:,:,2) =
1 2
3 4
---------------------
作者:wohuiluanshuo
来源:CSDN
原文:https://blog.csdn.net/wohuiluanshuo/article/details/49489247
版权声明:本文为博主原创文章,转载请附上博文链接!