[英]Matlab vector to matrix conversion
I want to convert the following vector A
into matrix B
, best demonstrated by this example: 我想将以下向量
A
转换为矩阵B
,这个例子最好地证明了:
n = 4;
A = [1 2 3 4 5 6];
B = [ 1 2 3 4;
2 3 4 5;
3 4 5 6; ]
I am currently using a loop to achieve this and wondered if it was possible to vectorize it? 我目前正在使用循环来实现这一点,并想知道是否可以对其进行矢量化?
Thanks L. 谢谢L.
You can use bsxfun
- 你可以用
bsxfun
-
A(bsxfun(@plus,[0:numel(A)-n]',1:n))
You can also use hankel
- 你也可以使用
hankel
-
hankel(A(1:n),A(n:end)).'
Sample run - 样品运行 -
>> A = [3,4,6,0,1,2]
A =
3 4 6 0 1 2
>> n
n =
4
>> A(bsxfun(@plus,[0:numel(A)-n]',1:n))
ans =
3 4 6 0
4 6 0 1
6 0 1 2
>> hankel(A(1:n),A(n:end)).'
ans =
3 4 6 0
4 6 0 1
6 0 1 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.