简体   繁体   中英

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...

This "works"; but, isn't it processing the matrix 3 times?

xx = [X(:,1),X(:,2) .-2000,X(:,3)]

Best practice please... ;-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". 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM