help-octave
[Top][All Lists]

## Re: subtraction of vectors results in 0 [bug?]

 From: Nicholas Jankowski Subject: Re: subtraction of vectors results in 0 [bug?] Date: Mon, 30 Nov 2015 09:58:46 -0500

On Mon, Nov 30, 2015 at 9:50 AM, Jonathan Camilleri wrote:
I am not sure if this is correct.

v
v =

1   0
1   1
2   4

>> x
x =

1   0
1   1
2   4

>> ans = v-x
ans =

0   0
0   0
0   0

error: operator *: nonconformant arguments (op1 is 3x2, op2 is 3x2)

--
Jonathan Camilleri

Mobile (MT): ++356 7982 7113

no bug.  this is how vector/matrix addition and subtraction is supposed to work. v(i,j) - x(i,j) = ans(i,j)

more of an issue is how you got a nonconformant argument error.  You didn't show any usage of the * operator to create that error message.  it looks like the error you should get if you tried:

>> x*v
error: operator *: nonconformant arguments (op1 is 3x2, op2 is 3x2)

because * is for matrix multiplication, which has very specific size requirements. See:
https://en.wikipedia.org/wiki/Matrix_multiplication

If you want to multiply the elements, you have to use the element-wise multiplication operator .*

>> x.*v
ans =

1    0
1    1
4   16