Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
|
|
Re: Reference previous index in calculation
Posted:
Feb 24, 2013 1:43 PM
|
|
On Monday, February 25, 2013 6:13:39 AM UTC+13, firm...@gmail.com wrote: > Hello and thank you for possibly helping. I'm trying to teach myself MatLab and haven't been able to find the solution to what seems fairly straight forward, at least not in a simplified or elegant way. I want to calculate across variables (e.g. element wise multiplication) but want to be able to reference the "previous variable" in array [italic]x[/italic] > > > > For example, 2 one-dimensional arrays: > > > > a = 1, 2, 3, 4, 5, 6, 7, 8, 9 > > b = 1, -1, 1, -1, 1, -1, 1, -1, 1 > > > > I want to calculate two variables, c and d. > > > > c = a .* b > > therefore > > c = 1, -2, 3, -4, 5, -6, 7, -8, 9 > > > > now my issue is for variable d I want to take each element in a and multiply it by the corresponding element in b, but one index earlier. The first element in d can't have an answer as there is no -1 index in b so that we'll assign a zero. > > > > d = [0; ....] > > calculated results should be > > d = [0], [2*1], [3*-1], [4*1], etc ... > > > > This is a simplified version of what I'm trying to accomplish of course. If I can add clarity please let me know and thanks again. > > > > I'm hoping that there is a manner to reference these elements without creating another array and shifting items.
d=[0 a(1:end-1).*b(2:end)]
|
|
|
|