Reference "previous index" - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Reference "previous index"

javamarketjavamarket Posts: 2Member
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. 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 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.

Sign In or Register to comment.