如何在不使用repmat函数的情况下复制矩阵的元素 [英] How to duplicate elements of a matrix without using the repmat function
问题描述
给出矩阵I = [1,2;3,4]
,我想复制元素以创建矩阵I2
,这样:
Given the matrix I = [1,2;3,4]
, I would like to duplicate the elements to create a matrix I2
such that:
I2 = [1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4]
除了使用repmat
以外,还有哪些其他方法或功能可用?
Other than using repmat
, what other methods or functions are available?
推荐答案
使用 kron
:
>> N = 3 %// Number of times to replicate a number in each dimension
>> I = [1,2;3,4];
>> kron(I, ones(N))
ans =
1 1 1 2 2 2
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
3 3 3 4 4 4
如果您不知道kron
的功能,则可能需要对此进行一些解释. kron
代表 Kronecker张量积.在大小为m x n
的两个矩阵A
和大小为p x q
的B
之间的kron
创建大小为mp x nq
的输出矩阵,使得:
This probably deserves some explanation in case you're not aware of what kron
does. kron
stands for the Kronecker Tensor Product. kron
between two matrices A
of size m x n
and B
of size p x q
creates an output matrix of size mp x nq
such that:
因此,对于A
中的每个系数,我们取该值,并将其与矩阵B
中的每个值相乘,然后按照与A
中所见相同的顺序放置这些矩阵.这样,如果我们将A = I
和B
设为充满1的3 x 3矩阵,那么您将得到上述结果.
Therefore, for each coefficient in A
, we take this value, multiply it with every value in the matrix B
and we position these matrices in the same order as we see in A
. As such, if we let A = I
, and B
be the 3 x 3 matrix full of ones, you thus get the above result.
这篇关于如何在不使用repmat函数的情况下复制矩阵的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!