problem with multiplication of vectors and matrix [please help]

조회 수: 12 (최근 30일)
agatte
agatte 2011년 12월 5일
답변: F46 2022년 8월 23일
Hi all Matlab Users ,
I' m begining my adventure with Matlab .. I need to write some code in matlab... I reveived always the same error .. and I don't see my fault ;) Could someone look at this line of code ?
I would appreciate for any help ;)
wsp_sigma(:,1:c)=sum(P(:,1:c));
for i=1:c
wsp_sigma(:,i) = (sum(P(:,i))).*((X-wsp_lambda(:,i)).*(X-wsp_lambda(:,i)).')./sum(P(:,i)) % here it doesn't work ??
end
Best, Agata
  댓글 수: 2
Paulo Silva
Paulo Silva 2011년 12월 5일
Can you please tell us what's the error you get?
Jan
Jan 2011년 12월 5일
The error messages of Matlab are very helpful. Usually they contain enough information to solve the problem. So at first read them carefully, and, if this does not solve the problem already, post the complete message here.

댓글을 달려면 로그인하십시오.

답변 (3개)

Walter Roberson
Walter Roberson 2011년 12월 5일
(X-wsp_lambda(:,i)) is a column vector
(X-wsp_lambda(:,i)).' transposes that to a row vector.
Then you try to elementwise multiply ( .* ) the column vector and the row vector. The shapes will not match, so you will not be able to do that.
Perhaps you meant to transpose to apply to the result of the element-wise multiplication; you could certainly code that by moving the brackets.
You have sum(P(:,i)) in both the numerator and denominator. The sum() of a column vector will be a scalar. That scalar is going to cancel between the numerator and denominator (unless the scalar is 0 or one of the infinities). The same would hold true even if you were doing a matrix multiplication: c*(A*B) is the same as A*(c*B) when A and B are matrices and c is a non-zero scalar.
It is thus difficult for us to guess what you had intended your code to be.

agatte
agatte 2011년 12월 5일
I received en error like this :
??? Error using ==> times Matrix dimensions must agree.
Error in ==> AgataGMM at 92 wsp_sigma(:,i) =sum(sum(P(:,i))).*((X-wsp_lambda(:,i)).*(X-wsp_lambda(:,i)).')/sum(P(:,i));
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 12월 5일
Yes, and that is for the reasons I explained. You cannot do element-wise multiplication of a row vector and a column vector.

댓글을 달려면 로그인하십시오.


F46
F46 2022년 8월 23일
I am having difficulty undersatnding what is going on. The code is simple svd.
I would be very grateful for anyone who can clarify the outut. I pasted the output below
Thanks a million in advance
a=randi(3,2,3)
[ua,sa,va]=svd(a)
c1=[a*va(:,1),sa(1,1)*ua(:,1)] %these two should be eqaul and are
c2=[a*va(:,2),s(2,2)*ua(:,2)] %these two should be equal and are
c31=va'*va(:,3) %the value for c3 on last line shoould be zero. I am trying to do it step by step
c312=sa*c31 %in the last line multiplied by va' now multiply by sa (usaully denoted singm)
c313=ua*c312 %multiply by ua
c3=a*va(:,3)
OUTUP
a =
3 1 2
1 1 1
ua =
-0.9153 0.4027
-0.4027 -0.9153
sa =
4.0791 0 0
0 0.6005 0
va =
-0.7719 0.4873 -0.4082
-0.3231 -0.8538 -0.4082
-0.5475 -0.1832 0.8165
c1 =
-3.7338 -3.7338
-1.6425 -1.6425
c2 =
0.2418 0.1607
-0.5497 -0.3653
c31 =
0.0000
0.0000
1.0000
c312 =
0.4529
0.0167
c313 =
-0.4078
-0.1976
c3 =
-0.4441
0

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by