multiplying arrays

  • Follow


Hi,

Is there a way to do this array product without resorting to "for
loops". Maybe this is just a trivial question, but I am not able to
figure it out.

theta=0.0:0.01:2.0;
n=[0 20 3];
alpha(j)=sqrt(sin(rad(theta)).^2-2*n(j));

What I want is, for every n, I want alpha(n) do to evaluated over the
full theta range.

Thanks.

Suresh
0
Reply sureshn (43) 2/16/2004 8:41:49 PM

On Mon, 16 Feb 2004 15:41:49 -0500, Suresh Narayanan wrote:

> Hi,
> 
> Is there a way to do this array product without resorting to "for
> loops". Maybe this is just a trivial question, but I am not able to
> figure it out.
> 
> theta=0.0:0.01:2.0;
> n=[0 20 3];
> alpha(j)=sqrt(sin(rad(theta)).^2-2*n(j));
> 
> What I want is, for every n, I want alpha(n) do to evaluated over the
> full theta range.
> 
> Thanks.
> 
> Suresh

Repmat is your friend here.  Try something like this (untested):

j=1:3;
thetaM=repmat(theta,length(j),1);
nM=repmat(n',1,length(theta));
alpha=sqrt(sin(rad(thetaM)).^2-2*nM);

Dan
0
Reply dan4519 (925) 2/16/2004 9:03:36 PM


On Mon, 16 Feb 2004 15:41:49 -0500, Suresh Narayanan wrote:

> Hi,
> 
> Is there a way to do this array product without resorting to "for
> loops". Maybe this is just a trivial question, but I am not able to
> figure it out.
> 
> theta=0.0:0.01:2.0;
> n=[0 20 3];
> alpha(j)=sqrt(sin(rad(theta)).^2-2*n(j));
> 
> What I want is, for every n, I want alpha(n) do to evaluated over the
> full theta range.
> 
> Thanks.
> 
> Suresh

Another thought:  Take a look at meshgrid as well.  Something like
[a,b]=meshgrid(theta,n);

Dan
0
Reply dan4519 (925) 2/16/2004 9:04:19 PM

Dan,

Thank you very much. I had never tried "repmat". That worked. Thanks
a lot.

Suresh

Dan Hensley wrote:
>
>
> On Mon, 16 Feb 2004 15:41:49 -0500, Suresh Narayanan wrote:
>
>> Hi,
>>
>> Is there a way to do this array product without resorting to
"for
>> loops". Maybe this is just a trivial question, but I am not
able
> to
>> figure it out.
>>
>> theta=0.0:0.01:2.0;
>> n=[0 20 3];
>> alpha(j)=sqrt(sin(rad(theta)).^2-2*n(j));
>>
>> What I want is, for every n, I want alpha(n) do to evaluated
over
> the
>> full theta range.
>>
>> Thanks.
>>
>> Suresh
>
> Another thought: Take a look at meshgrid as well. Something like
> [a,b]=meshgrid(theta,n);
>
> Dan
>
0
Reply sureshn (43) 2/16/2004 9:20:08 PM

On Mon, 16 Feb 2004 16:20:08 -0500, Suresh Narayanan wrote:

> Dan,
> 
> Thank you very much. I had never tried "repmat". That worked. Thanks
> a lot.

You're welcome.  In this particular case I think I like meshgrid better,
but both repmat and meshgrid are really useful tools.

Dan


> 
> Suresh
> 
> Dan Hensley wrote:
>>
>>
>> On Mon, 16 Feb 2004 15:41:49 -0500, Suresh Narayanan wrote:
>>
>>> Hi,
>>>
>>> Is there a way to do this array product without resorting to
> "for
>>> loops". Maybe this is just a trivial question, but I am not
> able
>> to
>>> figure it out.
>>>
>>> theta=0.0:0.01:2.0;
>>> n=[0 20 3];
>>> alpha(j)=sqrt(sin(rad(theta)).^2-2*n(j));
>>>
>>> What I want is, for every n, I want alpha(n) do to evaluated
> over
>> the
>>> full theta range.
>>>
>>> Thanks.
>>>
>>> Suresh
>>
>> Another thought: Take a look at meshgrid as well. Something like
>> [a,b]=meshgrid(theta,n);
>>
>> Dan
>>

0
Reply dan4519 (925) 2/16/2004 10:32:08 PM

4 Replies
34 Views

(page loaded in 0.091 seconds)


Reply: