|
From: | Harbinson, Jeremy |
Subject: | Trying to avoid loops when adjusting two variables. |
Date: | Tue, 17 Apr 2007 14:30:53 +0200 |
Hi,
This is one of those how-do-I-avoid loops questions.
Simple case of one variable in a function:
if I have a vector (A) and a function (eg y=2*x^2) I can easily apply the function to the vector without looping as follows
2.*A.^2
and I get an output in which the function has been applied sequentially to every element of the input vector.
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?
Thanks,
Jeremy Harbinson
[Prev in Thread] | Current Thread | [Next in Thread] |