[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trying to avoid loops when adjusting two variables.

From: Kim Hansen
Subject: Re: Trying to avoid loops when adjusting two variables.
Date: Tue, 17 Apr 2007 15:21:47 +0200

On 4/17/07, Harbinson, Jeremy <address@hidden> wrote:

More complicated case (the one for which I would like to avoid a for loop):

I now have 2 vectors A and B and a function with two variables (eg y=z*x^2).
I want to apply the function to both A and B, using B for variable z and A
for variable x, and get an output which is a grid within which each element
is the result of applying the function to a particular pair of elements from
A and B. At the moment the only way I can think of doing this is to use a
loop in which I first calculate B(1).*A.^2, then calculate B(2).*A.^2 etc
until I have cycled through every element in B. Can this procedure be
vectorised or is looping over one of the variables the only solution?

Something like:

z = repmat(A, size(B'));
x = repmat(B', size(A));
y = z .* x .^ 2;

Kim Hansen
Vadgårdsvej 3,
2860 Søborg
Fastnet: 3956 2437  --  Mobil: 3091 2437

reply via email to

[Prev in Thread] Current Thread [Next in Thread]