从矢量获得矢量的矩阵 [英] get matrix of vectors from a vector
本文介绍了从矢量获得矢量的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个vector X = [1,3,5,6,7]
,我想制作一个矩阵是
其中行Y(K)= X(K:K + 2)。因此,在这种情况下,所得到的矩阵将是
I have a vector x = [1,3,5,6,7]
and I want to produce a matrix y
in which rows y(k) = x(k:k+2). So the resulting matrix in this case would be
1 3 5
3 5 6
5 6 7
我怎样才能做到这一点的没有使用一个循环?是否与索引那么这是一种聪明的方式?
How can I achieve this without using a loop? Is there a clever way of doing it with indexing?
推荐答案
使用稍微做作的方式 meshgrid
:
k = (length(x) + 1) / 2;
[a b] = meshgrid(1:k, 0:k-1);
y = x(a+b);
使用小型相当于
或 bsxfun
y = x(bsxfun(@plus, (1:k)', 0:k-1));
或确实的傻单行:
y = x(interp2([1 3], [1;3], [1 3; 3 5], 1:3, (1:3)'));
这篇关于从矢量获得矢量的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文