Explanation of this statement
古いコメントを表示
for i=1:N
yd(i) = sum(w(i)' .* x(i));
end
Can anyone explain the above statement to me?? I can't understand the use of sum as acc. to me w(i) and x(i) are just numbers and not whole vector. So what's the sum for. Don't doubt the code. It is correct. Just need an explanation.
回答 (2 件)
dpb
2014 年 6 月 30 日
Did you just look and see??? But, yes, you're evaluation is correct of the code as written--now whether that's what was intended is another question.
The above loop could be rewritten as
yd=w' .* x;
Try
all(yd)==all(w' .* x)
to prove it.
6 件のコメント
Ashutosh Shukla
2014 年 6 月 30 日
A) Don't believe that one at all...unless you've got infinite storage :)
B) That's possible; but that's easily-enough fixed if both are column vectors. The question is whether there's any need at all for the ' operator -- I'm guessing not but it's possible w could be complex in which case it makes a difference computationally.
I'm guessing this is a remnant left over from development that never got cleaned up and started out intended to be something as
y=w.' * x;
but inadvertently wrote the complex transpose ' instead of the non-conjugate version .' which would be the cheaper computationally again assuming w isn't actually complex, but just a weight function. then for some reason decided need the individual values instead of just the weighted sum. But, of course, in isolation there's no way to really tell what the intent really was/is...
Example--
>> w=[0.1:.1:.3].';
>> x=1:3;x=x.';
>> w.' * x
ans =
1.4000
>> s=0;for i=1:3;z=w(i)*x(i);s=s+z;disp([z s]),end
0.1000 0.1000
0.4000 0.5000
0.9000 1.4000
>>
Joseph Cheng
2014 年 6 月 30 日
編集済み: Joseph Cheng
2014 年 6 月 30 日
My take is that it is leftover as vestigial code. Probably at one point this was to do n by m (of either full row or column per loop) as there wouldn't be a need to transpose w(i) so it could do element by element multiplication with x(i) for the current single element multiplication.
dpb
2014 年 6 月 30 日
Essentially what I said other than the caveat of IFF w were actually complex then the complex transpose ' does do something...
Ashutosh Shukla
2014 年 7 月 1 日
dpb
2014 年 7 月 1 日
no complex numbers in calculation....
Which I presumed was likely the case but just pointing out there is a difference if w were to be complex.
Robert Cumming
2014 年 6 月 30 日
Looking at the code, unless sum is an overloaded function that does something other than the standard inbuild sum - it looks redundant.
To check what sum is - type:
which sum
To check its not doing anything - remove it and run the code again!
4 件のコメント
Ashutosh Shukla
2014 年 6 月 30 日
Robert Cumming
2014 年 6 月 30 日
its doing nothing, the sum of a scalar is equal to the input scalar.
Ashutosh Shukla
2014 年 7 月 1 日
dpb
2014 年 7 月 1 日
That's why I brought up the complex number issue; the ' is the complex transpose which has no net effect if the values are real but does if they're not. Newbies often mistakenly use it when the non-complex transpose operator .' is intended instead, which is the case I presumed happened here.
See
doc punct
for details.
As noted, if it's written as a loop on an individual element for a real value it has no effect, just as the sum of a single value is simply the value.
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!