square of a vector

8 次查看(过去 30 天)
Berbia
Berbia 2012-10-31
I have to calculate a square of vector which should results a scalar value. i.e) A=(B-C)^2 B and C are vectors and I need A as a scalar. How can I implement this in matlab??

采纳的回答

Honglei Chen
Honglei Chen 2012-10-31
编辑:Honglei Chen 2012-10-31
My guess is you need an inner product, i.e. A = |B-C|^2, you can do it many different ways, one way is
B = ones(3,1);
C = ones(3,1);
A = (B-C)'*(B-C)
  6 个评论
Honglei Chen
Honglei Chen 2012-10-31
I'd say
temp = B-C;
temp'*temp
Matt J
Matt J 2012-10-31
编辑:Matt J 2012-10-31
You should think about whether the loop can be avoided altogether, since this sounds like a very vectorizable operation. If B and C are matrices and you want the dot product along columns, for example
temp=B-C;
result = sum(temp.^2,1);

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2012-10-31

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by