필터 지우기
필터 지우기

square of a vector

조회 수: 6 (최근 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