如何在不使用repmat函数的情况下复制矩阵的元素 [英] How to duplicate elements of a matrix without using the repmat function

查看:103
本文介绍了如何在不使用repmat函数的情况下复制矩阵的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出矩阵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 qB之间的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 = IB设为充满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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆