在MATLAB中从一维数组生成二维数组

时间:2023-01-17 21:59:43

Does anyone know if there is a way to produce a 2D array from a 1D array, where the rows in the 2D are generated by repeating the corresponding elements in the 1D array.

有没有人知道是否有办法从1D数组生成2D数组,其中2D中的行是通过重复1D数组中的相应元素生成的。

I.e.:

1D array      2D array

  |1|       |1 1 1 1 1|
  |2|       |2 2 2 2 2|
  |3|  ->   |3 3 3 3 3|
  |4|       |4 4 4 4 4|
  |5|       |5 5 5 5 5|

4 个解决方案

#1


9  

In the spirit of bonus answers, here are some of my own:

本着奖金答案的精神,这里有一些我自己的:

Let A = (1:5)'

设A =(1:5)'

  1. Using indices [faster than repmat]:

    使用索引[比repmat更快]:

    B = A(:, ones(5,1))
    
  2. Using matrix outer product:

    使用矩阵外产品:

    B = A*ones(1,5)
    
  3. Using bsxfun() [not the best way of doing it]

    使用bsxfun()[不是最好的方法]

    B = bsxfun(@plus, A, zeros(1,5))
    %# or
    B = bsxfun(@times, A, ones(1,5))
    

#2


8  

You can do this using the REPMAT function:

您可以使用REPMAT功能执行此操作:

>> A = (1:5).'

A =

     1
     2
     3
     4
     5

>> B = repmat(A,1,5)

B =

     1     1     1     1     1
     2     2     2     2     2
     3     3     3     3     3
     4     4     4     4     4
     5     5     5     5     5

EDIT: BONUS ANSWER! ;)

编辑:奖励答案! ;)

For your example, REPMAT is the most straight-forward function to use. However, another cool function to be aware of is KRON, which you could also use as a solution in the following way:

对于您的示例,REPMAT是最直接的功能。但是,要注意的另一个很酷的功能是KRON,您也可以通过以下方式将其用作解决方案:

B = kron(A,ones(1,5));

For small vectors and matrices KRON may be slightly faster, but it is quite a bit slower for larger matrices.

对于小向量和矩阵,KRON可能稍微快一些,但对于较大的矩阵来说它要慢一些。

#3


1  

repmat(a, [1 n]), but you should also take a look at meshgrid.

repmat(a,[1 n]),但你也应该看看meshgrid。

#4


0  

You could try something like:

你可以尝试类似的东西:

a = [1 2 3 4 5]'
l = size(a)
for i=2:5
    a(1:5, i) = a(1:5)

The loop just keeps appending columns to the end.

循环只是将列附加到末尾。

#1


9  

In the spirit of bonus answers, here are some of my own:

本着奖金答案的精神,这里有一些我自己的:

Let A = (1:5)'

设A =(1:5)'

  1. Using indices [faster than repmat]:

    使用索引[比repmat更快]:

    B = A(:, ones(5,1))
    
  2. Using matrix outer product:

    使用矩阵外产品:

    B = A*ones(1,5)
    
  3. Using bsxfun() [not the best way of doing it]

    使用bsxfun()[不是最好的方法]

    B = bsxfun(@plus, A, zeros(1,5))
    %# or
    B = bsxfun(@times, A, ones(1,5))
    

#2


8  

You can do this using the REPMAT function:

您可以使用REPMAT功能执行此操作:

>> A = (1:5).'

A =

     1
     2
     3
     4
     5

>> B = repmat(A,1,5)

B =

     1     1     1     1     1
     2     2     2     2     2
     3     3     3     3     3
     4     4     4     4     4
     5     5     5     5     5

EDIT: BONUS ANSWER! ;)

编辑:奖励答案! ;)

For your example, REPMAT is the most straight-forward function to use. However, another cool function to be aware of is KRON, which you could also use as a solution in the following way:

对于您的示例,REPMAT是最直接的功能。但是,要注意的另一个很酷的功能是KRON,您也可以通过以下方式将其用作解决方案:

B = kron(A,ones(1,5));

For small vectors and matrices KRON may be slightly faster, but it is quite a bit slower for larger matrices.

对于小向量和矩阵,KRON可能稍微快一些,但对于较大的矩阵来说它要慢一些。

#3


1  

repmat(a, [1 n]), but you should also take a look at meshgrid.

repmat(a,[1 n]),但你也应该看看meshgrid。

#4


0  

You could try something like:

你可以尝试类似的东西:

a = [1 2 3 4 5]'
l = size(a)
for i=2:5
    a(1:5, i) = a(1:5)

The loop just keeps appending columns to the end.

循环只是将列附加到末尾。