From: Ashley Wright on
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.
From: Nathan on
On Apr 21, 4:47 pm, "Ashley Wright" <navyas...(a)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
From: Ashley Wright on
Thanks for the help guys!
From: dpb on
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));

:)

--