http://www.mathworks.com/matlabcentral/newsreader/view_thread/280105
MATLAB Central Newsreader  Basic Matlab help
Feed for thread: Basic Matlab help
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 21 Apr 2010 23:47:05 +0000
Basic Matlab help
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280105#738256
Ashley Wright
I'm really rusty with MATLAB and need some assistance. I'm trying to write a code to take r = sqrt((Dy)^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:<br>
<br>
r(1,1) would be sqrt((D1)^2+1^2), r(1,2) would be sqrt((D1)^2+2^2), ..., r(x,y) = sqrt((Dy)^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.

Wed, 21 Apr 2010 23:58:38 +0000
Re: Basic Matlab help
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280105#738258
Nathan
On Apr 21, 4:47 pm, "Ashley Wright" <navyas...@gmail.com> wrote:<br>
> I'm really rusty with MATLAB and need some assistance. I'm trying to write a code to take r = sqrt((Dy)^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:<br>
><br>
> r(1,1) would be sqrt((D1)^2+1^2), r(1,2) would be sqrt((D1)^2+2^2), ..., r(x,y) = sqrt((Dy)^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.<br>
<br>
<br>
You could try something like this:<br>
x = 1:1000;<br>
y = 1:1000;<br>
r = cell2mat(arrayfun(@(a)sqrt((Da).^2+x'.^2),y,'un',0));<br>
<br>
Nathan

Thu, 22 Apr 2010 00:02:07 +0000
Re: Basic Matlab help
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280105#738260
Roger Stafford
"Ashley Wright" <navyasw02@gmail.com> wrote in message <hqo2po$h11$1@fred.mathworks.com>...<br>
> I'm really rusty with MATLAB and need some assistance. I'm trying to write a code to take r = sqrt((Dy)^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:<br>
> <br>
> r(1,1) would be sqrt((D1)^2+1^2), r(1,2) would be sqrt((D1)^2+2^2), ..., r(x,y) = sqrt((Dy)^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.<br>
<br>
[X,Y] = meshgrid(1:1000,1:1000);<br>
r = sqrt((DY).^2+X.^2);<br>
<br>
Roger Stafford

Thu, 22 Apr 2010 00:12:06 +0000
Re: Basic Matlab help
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280105#738261
Ashley Wright
Thanks for the help guys!

Thu, 22 Apr 2010 00:23:06 +0000
Re: Basic Matlab help
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280105#738264
dpb
Ashley Wright wrote:<br>
> I'm really rusty with MATLAB and need some assistance. I'm trying to <br>
> write a code to take r = sqrt((Dy)^2+x^2) and its not working out the <br>
> way I would like. x and y are values from 1 to 1000. I'd like MATLAB to <br>
> calculate r1 using the equation above for each combination of values in <br>
> the matrix. For example:<br>
> <br>
> r(1,1) would be sqrt((D1)^2+1^2), r(1,2) would be sqrt((D1)^2+2^2), <br>
> ..., r(x,y) = sqrt((Dy)^2+x^2), etc. How do I go about doing that? I <br>
> can do it with loops, but that is very inefficient. Thanks for the help.<br>
<br>
Or, just for grins compared to others...<br>
<br>
R=abs(complex(DY,X));<br>
<br>
:)<br>
<br>


Thu, 22 Apr 2010 12:36:21 +0000
Re: Basic Matlab help
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280105#738376
Simon
dpb <none@non.net> wrote in message <hqo50f$7vi$1@news.eternalseptember.org>...<br>
> Ashley Wright wrote:<br>
> > I'm really rusty with MATLAB and need some assistance. I'm trying to <br>
> > write a code to take r = sqrt((Dy)^2+x^2) and its not working out the <br>
> > way I would like. x and y are values from 1 to 1000. I'd like MATLAB to <br>
> > calculate r1 using the equation above for each combination of values in <br>
> > the matrix. For example:<br>
> > <br>
> > r(1,1) would be sqrt((D1)^2+1^2), r(1,2) would be sqrt((D1)^2+2^2), <br>
> > ..., r(x,y) = sqrt((Dy)^2+x^2), etc. How do I go about doing that? I <br>
> > can do it with loops, but that is very inefficient. Thanks for the help.<br>
> <br>
> Or, just for grins compared to others...<br>
> <br>
> R=abs(complex(DY,X));<br>
> <br>
> :)<br>
> <br>
> <br>
You could also try swapping around to something like this:<br>
x = 1:2000;<br>
y = 1:2000;<br>
r = cell2mat(arrayfun(@(a)sqrt((Da).^2+x'.^2),y,'un',0));<br>
<br>
Best,<br>
Simon<br>
<a href="http://www.schieldenver.com/">http://www.schieldenver.com/</a>