Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Reference previous index in calculation
Replies: 1   Last Post: Feb 24, 2013 1:43 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
Derek Goring

Posts: 3,892
Registered: 12/7/04
Re: Reference previous index in calculation
Posted: Feb 24, 2013 1:43 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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)]



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.