Matlab - Summation two variables

Thread Starter

JoeJo

Joined Dec 16, 2010
1
I'm fairly new to Matlab programming and am having difficulty expressing this equation on Matlab:

g(j)= ∑ (y(n)*x(n-j))

where the sum is from n = 8 to 10 for example.

The end result I want is a discrete function like:

g(j) = y(8)*x(8-j) + y(9)*x(9-j) + y(10)*x(10-j)

which I'll ultimately evaluate with a for loop at say j = 6 and 7.

x(n) and y(n) are defined from n=0,...,1000 by the way.

Anybody have any tips on how to accomplish this. This may be simple, but for some reason is giving me problems.

Thanks.
 

etuzuner

Joined Mar 21, 2009
23
JoeJo,

If I got you wrong, please let me know.

Being new to MATLAB, you may not know some critical points. If you're not working with Symbolic concept, MATLAB works with only numbers (i.e., matrices). So, I think choosing j=6,7 does not make sense. I'm ok with the creation of the function g, I think you're trying to convolve these two signals but why do you want it to take the values 6 and 7 at the same time? Let's say there is no mistake. The requested form is like shown below;

Rich (BB code):
x=1:2:2000;% linspace command could be used like x=linspace(0,2000,1000);
y=0:2:1999;% linspace command could be used like y=linspace(1,1999,1000);
g=[]; 
sum6=0;
sum7=0;
j=6;    
for i=8:10
    g(j)=y(i)*x(i-j);
    sum6=sum6+g(j);
end
j=7;
for i=8:10
    g(j)=y(i)*x(i-j);
    sum7=sum7+g(j);
end
fprintf('g(6) is equal to %d\n', sum6)
fprintf('g(7) is equal to %d\n', sum7)
This code may be reorganized but I prepared the code like this because you need to learn MATLAB step by step. If you need to ask sth, please don't hesitate.

BR
Emre
 
If you're interested in learning how to actually code this, the above is a good suggestion.

But if you just want the result, try the conv() function (or any of the assortment of fir() type functions)
 
Top