Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot understand this MATLAB syntax?

Tags:

matlab

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.

like image 658
siddharth Avatar asked Aug 04 '26 06:08

siddharth


1 Answers

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

like image 112
0x90 Avatar answered Aug 06 '26 02:08

0x90