square of a vector
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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??
0 comentarios
Respuesta aceptada
Honglei Chen
el 31 de Oct. de 2012
Editada: Honglei Chen
el 31 de Oct. de 2012
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 comentarios
Matt J
el 31 de Oct. de 2012
Editada: Matt J
el 31 de Oct. de 2012
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);
Más respuestas (1)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!