Page 1 sur 1

vector^2 without a warning?

Publié : mar. mai 28, 2019 10:30 am
par niccolo
I often need to "dot product" a vector with itself. E.g., "(pointB - pointA)^2". But when I do this I see the warning:
Warning, ^ is ambiguous on non square matrices. Use .^ to apply ^ element by element.
Is there a way to get rid of this warning? I know I can do "dot(expr, expr)" instead, but then I have to type the expression twice.

Re: vector^2 without a warning?

Publié : mar. mai 28, 2019 10:40 am
par parisse
Just follow the hint: replace ^ by .^ like (pointB - pointA).^2

Re: vector^2 without a warning?

Publié : mar. mai 28, 2019 12:48 pm
par niccolo
parisse a écrit :
mar. mai 28, 2019 10:40 am
Just follow the hint: replace ^ by .^ like (pointB - pointA).^2
Given the vector [a,b,c], I want to arrive at a^2 + b^2 + c^2.

[a,b,c]^2 does this (but gives a warning).

[a,b,c].^2 gives [a^2, b^2, c^2]. Not good.

I know I can do "[a,b,c]*[a,b,c]" or "sum([a,b,c].^2)", but I was wondering if there's a shorter syntax.

Re: vector^2 without a warning?

Publié : mar. mai 28, 2019 6:43 pm
par parisse
Indeed, sorry. l2norm(v)^2 will do.