Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Embedded For Loop?

Subject: Embedded For Loop?

From: Carp Fisher

Date: 11 Mar, 2009 02:50:56

Message: 1 of 6

Lets say I have two arrays:

x = [1 2 3 4 5]
y = [9 8 7 6 5]

How would I write a loop that would solve for

z = x + y

for each different combination of x and y values, i.e
z(1,9) z(2,9) z(3,9)... etc?

Thanks,

Kim

Subject: Embedded For Loop?

From: Carp Fisher

Date: 11 Mar, 2009 02:54:41

Message: 2 of 6

My z array would then be a 5 x 5...

Subject: Embedded For Loop?

From: Matt Fig

Date: 11 Mar, 2009 03:10:21

Message: 3 of 6

One approach:

x = [1 2 3 4 5];
y = [9 8 7 6 5];
[xx,yy] = meshgrid(x,y);
z = xx+yy

Another:

x = [1 2 3 4 5];
y = [9 8 7 6 5]' ; % Note the transpose
z = x(ones(1,5),:)+y(:,ones(1,5))




&wc82lhawalwmf`YY]Ywm[qY]eelg`gdw=w~]gQh`~Zw[nwY]_YdgYgZEgf

Subject: Embedded For Loop?

From: TideMan

Date: 11 Mar, 2009 03:23:49

Message: 4 of 6

On Mar 11, 3:54=A0pm, Carp Fisher <bm...@gmail.com> wrote:
> My z array would then be a 5 x 5...

People here at CSSM tend to be more helpful if you have had a go at
solving the problem yourself, rather than just asking us to give you
the solution.
Have you had any thoughts about how you might go about it?

Subject: Embedded For Loop?

From: Roger Stafford

Date: 11 Mar, 2009 03:53:01

Message: 5 of 6

Carp Fisher <bmjim@gmail.com> wrote in message <16409623.1236739893454.JavaMail.jakarta@nitrogen.mathforum.org>...
> Lets say I have two arrays:
>
> x = [1 2 3 4 5]
> y = [9 8 7 6 5]
>
> How would I write a loop that would solve for
>
> z = x + y
>
> for each different combination of x and y values, i.e
> z(1,9) z(2,9) z(3,9)... etc?
>
> Thanks,
>
> Kim

  You could also do:

 z = round(log(exp(x.')*exp(y)));

assuming x and y contain only positive integers as in your example. Otherwise remove the 'round' portion.

  To be candid with you, I don't actually recommend this method but only present it here as a curiosity.

Roger Stafford

Subject: Embedded For Loop?

From: Carp Fisher

Date: 11 Mar, 2009 19:52:32

Message: 6 of 6

Awesome! Thanks alot

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us