从MATLAB一维数组生成二维数组 [英] Producing 2D array from a 1D array in MATLAB
本文介绍了从MATLAB一维数组生成二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道是否有从一维数组,其中在2D行是通过重复一维数组中的相应元件产生产生一个二维数组的方式。
即:
一维数组二维数组 | 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 |
解决方案
在奖金答案的精神,这里有一些我自己的:
让 A =(1:5)
-
使用指数[快于repmat]:
B = A(:,那些(5,1))
-
使用矩阵外积:
B = A *的人(1,5)
-
使用bsxfun()[不是做的最好的方式吧]
B = bsxfun(@plus,A,零(1,5))
%# 要么
B = bsxfun(@times,A,那些(1,5))
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.
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|
解决方案
In the spirit of bonus answers, here are some of my own:
Let A = (1:5)'
Using indices [faster than repmat]:
B = A(:, ones(5,1))
Using matrix outer product:
B = A*ones(1,5)
Using bsxfun() [not the best way of doing it]
B = bsxfun(@plus, A, zeros(1,5)) %# or B = bsxfun(@times, A, ones(1,5))
这篇关于从MATLAB一维数组生成二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文