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:
Basic Matlab help

Subject: Basic Matlab help

From: Ashley Wright

Date: 21 Apr, 2010 23:47:05

Message: 1 of 6

I'm really rusty with MATLAB and need some assistance. I'm trying to write a code to take r = sqrt((D-y)^2+x^2) and its not working out the way I would like. x and y are values from 1 to 1000. I'd like MATLAB to calculate r1 using the equation above for each combination of values in the matrix. For example:

r(1,1) would be sqrt((D-1)^2+1^2), r(1,2) would be sqrt((D-1)^2+2^2), ..., r(x,y) = sqrt((D-y)^2+x^2), etc. How do I go about doing that? I can do it with loops, but that is very inefficient. Thanks for the help.

Subject: Basic Matlab help

From: Nathan

Date: 21 Apr, 2010 23:58:38

Message: 2 of 6

On Apr 21, 4:47 pm, "Ashley Wright" <navyas...@gmail.com> wrote:
> I'm really rusty with MATLAB and need some assistance.  I'm trying to write a code to take r = sqrt((D-y)^2+x^2) and its not working out the way I would like.  x and y are values from 1 to 1000. I'd like MATLAB to calculate r1 using the equation above for each combination of values in the matrix.  For example:
>
> r(1,1) would be sqrt((D-1)^2+1^2), r(1,2) would be sqrt((D-1)^2+2^2), ..., r(x,y) = sqrt((D-y)^2+x^2), etc.  How do I go about doing that?  I can do it with loops, but that is very inefficient.  Thanks for the help.


You could try something like this:
x = 1:1000;
y = 1:1000;
r = cell2mat(arrayfun(@(a)sqrt((D-a).^2+x'.^2),y,'un',0));

-Nathan

Subject: Basic Matlab help

From: Roger Stafford

Date: 22 Apr, 2010 00:02:07

Message: 3 of 6

"Ashley Wright" <navyasw02@gmail.com> wrote in message <hqo2po$h11$1@fred.mathworks.com>...
> I'm really rusty with MATLAB and need some assistance. I'm trying to write a code to take r = sqrt((D-y)^2+x^2) and its not working out the way I would like. x and y are values from 1 to 1000. I'd like MATLAB to calculate r1 using the equation above for each combination of values in the matrix. For example:
>
> r(1,1) would be sqrt((D-1)^2+1^2), r(1,2) would be sqrt((D-1)^2+2^2), ..., r(x,y) = sqrt((D-y)^2+x^2), etc. How do I go about doing that? I can do it with loops, but that is very inefficient. Thanks for the help.
---------
 [X,Y] = meshgrid(1:1000,1:1000);
 r = sqrt((D-Y).^2+X.^2);

Roger Stafford

Subject: Basic Matlab help

From: Ashley Wright

Date: 22 Apr, 2010 00:12:06

Message: 4 of 6

Thanks for the help guys!

Subject: Basic Matlab help

From: dpb

Date: 22 Apr, 2010 00:23:06

Message: 5 of 6

Ashley Wright wrote:
> I'm really rusty with MATLAB and need some assistance. I'm trying to
> write a code to take r = sqrt((D-y)^2+x^2) and its not working out the
> way I would like. x and y are values from 1 to 1000. I'd like MATLAB to
> calculate r1 using the equation above for each combination of values in
> the matrix. For example:
>
> r(1,1) would be sqrt((D-1)^2+1^2), r(1,2) would be sqrt((D-1)^2+2^2),
> ..., r(x,y) = sqrt((D-y)^2+x^2), etc. How do I go about doing that? I
> can do it with loops, but that is very inefficient. Thanks for the help.

Or, just for grins compared to others...

R=abs(complex(D-Y,X));

:)

--

Subject: Basic Matlab help

From: Simon

Date: 22 Apr, 2010 12:36:21

Message: 6 of 6

dpb <none@non.net> wrote in message <hqo50f$7vi$1@news.eternal-september.org>...
> Ashley Wright wrote:
> > I'm really rusty with MATLAB and need some assistance. I'm trying to
> > write a code to take r = sqrt((D-y)^2+x^2) and its not working out the
> > way I would like. x and y are values from 1 to 1000. I'd like MATLAB to
> > calculate r1 using the equation above for each combination of values in
> > the matrix. For example:
> >
> > r(1,1) would be sqrt((D-1)^2+1^2), r(1,2) would be sqrt((D-1)^2+2^2),
> > ..., r(x,y) = sqrt((D-y)^2+x^2), etc. How do I go about doing that? I
> > can do it with loops, but that is very inefficient. Thanks for the help.
>
> Or, just for grins compared to others...
>
> R=abs(complex(D-Y,X));
>
> :)
>
> --
You could also try swapping around to something like this:
x = 1:2000;
y = 1:2000;
r = cell2mat(arrayfun(@(a)sqrt((D-a).^2+x'.^2),y,'un',0));

Best,
Simon
http://www.schieldenver.com/

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