[英]Matlab: add vector to matrix
I have a 3XN
matrix representing a list of 3D coordinates,something like 我有一个
3XN
矩阵表示3D坐标列表,类似于
33 33 33 33 34 34 34 34 34 35 35
17 18 19 20 16 17 18 19 20 16 17
10 10 10 10 10 10 10 10 10 10 10
I want to shift all coordinates by some vector v=[1 2 3]
, that is add the 3D vector to each column of the matrix. 我想将所有坐标移动一些向量
v=[1 2 3]
,即将3D向量添加到矩阵的每一列。
I know how to do that with a for
loop, but how can I do it without a loop? 我知道如何用
for
循环来做到这一点,但是如何在没有循环的情况下做到这一点? Surely there's a way... 当然有办法......
you mean like this? 你的意思是这样的?
D=[33 33 33 33 34 34 34 34 34 35 35;
17 18 19 20 16 17 18 19 20 16 17;
10 10 10 10 10 10 10 10 10 10 10 ];
A=[1 2 3]';
C= bsxfun(@plus, D, A)
C =
34 34 34 34 35 35 35 35 35 36 36
19 20 21 22 18 19 20 21 22 18 19
13 13 13 13 13 13 13 13 13 13 13
Use repmat
: 使用
repmat
:
M = randn(3, N); % your 3 x N matrix
v = randn(3, 1); % your vector
r = M + repmat(v, [1 N]); % add v to every column of M
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.