3
for i=0:255
m(i+1)=sum((0:i)'.*p(1:i+1)); end

What is happening can anyone explain. p is an array of size 256 elements same as m.

4

2 回答 2

4
p = (0:255)';
m = zeros(1,256);
for i=0:255
    m(i+1)=sum((0:i)'.*p(1:i+1)); 

end

m[i+1] contains the scalar product of [0,1,2,..,i] with (p[1],...,p[i+1])

You can write it as :

p = (0:255);
m = zeros(1,256);
for i=0:255
    m(i+1)=sum((0:i).*p(1:i+1)); 

end

Or:

p = (0:255);
m = zeros(1,256);
for i=0:255
    m(i+1)=(0:i)*p(1:i+1)'; 

end

In case you don't recall, that is the definition of scalar product

于 2012-04-09T08:41:08.980 回答
1

Whatever the p is, you can calculate m by:

dm = (0 : length(p) - 1)' .* p(:); % process as column vector
m = cumsum(dm);

Hint: write the formula for m[n], then for m[n+1], then subtract to get the formula:

m[n+1] - m[n] = (n - 1) * p[n]

and this is dm.

于 2012-04-09T14:03:03.510 回答