myname
myname

Reputation: 137

Use a function with dynamic variables

I have an array A of size [m x n], a function f(array, a, b, c) and an array of variables [a1 b1 c1; a2 b2 c2; ... ak bk ck].

I want to get an array:

[f(A, a1, b1, c1); f(A, a2, b2, c2); ... f(A, ak, bk, ck)]

Is it an ellegant way to realize this in MATLAB without using cumbersome loop structure?

for i = 1:k
B(i) = f(A, a(i), b(i), c(i));
end

Upvotes: 2

Views: 55

Answers (1)

Shai
Shai

Reputation: 114796

How about using arrayfun?
let P be the k by 3 matrix with parameters [a1 b1 c1;...' ak bk ck] then

B = arrayfun( @(a,b,c) f( A, a, b, c), P(:,1), P(:,2), P(:,3) );

BTW
It is best not to use i as a variable name in matlab.

Upvotes: 1

Related Questions