Matlab - Summation two variables

Discussion in 'Programmer's Corner' started by JoeJo, Dec 16, 2010.

1. JoeJo Thread Starter New Member

Dec 16, 2010
1
0
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.

2. etuzuner Member

Mar 21, 2009
23
0
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;

Code ( (Unknown Language)):
1. x=1:2:2000;% linspace command could be used like x=linspace(0,2000,1000);
2. y=0:2:1999;% linspace command could be used like y=linspace(1,1999,1000);
3. g=[];
4. sum6=0;
5. sum7=0;
6. j=6;
7. for i=8:10
8.     g(j)=y(i)*x(i-j);
9.     sum6=sum6+g(j);
10. end
11. j=7;
12. for i=8:10
13.     g(j)=y(i)*x(i-j);
14.     sum7=sum7+g(j);
15. end
16. fprintf('g(6) is equal to %d\n', sum6)
17. 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

3. guitarguy12387 Active Member

Apr 10, 2008
359
12
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)