[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Problem with arrayfun and parameter dimensions
From: |
John W. Eaton |
Subject: |
Problem with arrayfun and parameter dimensions |
Date: |
Fri, 7 Oct 2011 10:50:22 -0400 |
On 7-Oct-2011, Christoph Mahnke wrote:
|
| Hello everybody,
|
| I want to avoid for-loops when applying a function on a matrix of
| values, so my idea is to choose arrayfun.
| A problem arises, as the function expects input parameters with
| different dimensions, here as a simplified example:
|
| function result = dummyfunction( x, parameter1, parameter2)
| result = x * parameter1 - sum(x ./ parameter2 );
| endfunction
|
| where parameter1 is a simple number and parameter2 is a (fixed-length
| 1xN) vector.
| I want to apply arrayfun on a array X of values, but i'm not sure how
| to create a suitable array for the vector parameter. Some simple code to
| illustrate my problem:
|
| %
| % for a single value
| %
|
| x1 = rand()
| parameter1 = 7
| parameter2 = 1:10
| result1 = dummyfunction(x1,parameter1,parameter2) % (works well)
|
| %
| % this time a matrix
| %
| X=rand(3,4)
|
| p1array= parameter1 * ones( rows(X), columns(X));
| p2array= ???
|
| R = arrayfun(@dummyfunction, X, p1array, p2array)
|
|
| Maybe you can give me a hint how i have to create the parametermatrix
| p2array to use in arrayfun.
Do you want to apply the function for each value of X with the
parameters fixed for each value of X? If so, then I think you want
something like
X = ...;
parameter1 = ...;
parameter2 = ...;
arrayfun (@(X) dummyfunction (X, parameter1, parameter2), X);
When the anonymous function
@(X) dummyfunction (X, parameter1, parameter2)
is constructed, parameter1 and parameter2 are set from the context in
which the anonymous function is constructed.
But if I understand correctly, your particular problem would probably
solved more efficiently by writing
X * (parameter1 - sum (1 ./ parameter2))
jwe