[英]What's the best practice for subtracting a constant from say col 2 in Octave 3x n Matrix
Here, I subtract 2000 from column 2 and return the complete 3 column vector... 在这里,我从第2列中减去2000,然后返回完整的3列向量。
This "works"; 这个“作品”; but, isn't it processing the matrix 3 times?
但是,它不是处理矩阵3次吗?
xx = [X(:,1),X(:,2) .-2000,X(:,3)]
Best practice please... ;-0 请最佳做法...;-0
The simplest way to do this operation is to simply: 执行此操作的最简单方法是:
X(:,2) -= 2000;
which is also a lot easier to read. 这也更容易阅读。 This will modify the second column
X
"in place". 这将修改第二列
X
“就地”。 If you want to make a copy of it where the second column is subtracted, then simply: 如果要在减去第二列的位置制作副本,则只需:
xx = X;
xx(:,2) -= 2000;
An example: 一个例子:
octave-cli-3.8.2> X = randi (9, 5, 3)
X =
1 4 4
1 2 6
8 4 3
7 7 1
7 7 2
octave-cli-3.8.2> X(:,2) -= 10
X =
1 -6 4
1 -8 6
8 -6 3
7 -3 1
7 -3 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.