MATLAB Answers

How do you call an inline or anonymous function within cellfun?

9 views (last 30 days)
Megna Hari
Megna Hari on 20 Jan 2020
Commented: Megna Hari on 24 Jan 2020
I have three variables (simplifying this and changing variable names for the question since the actual variables are over 200,000 rows long)
A= [1; 2; 3; ...] % numbers that arent necessarily in order but they're unique and there arent gaps
B{1,1}= [1 3]
B{2,1}= [9 11 12]
and so on (the cell array B has varying sizes within each cell)
C= [25 50 75;
30 60 90;
10 20 30;
...] % same length as A since each row is basically an ID and coordinates for each value in A
I want to look at each row of B, find the locations of those B values in A and then extract the corresponding values in C into something that looks like this:
result{1,1}= [ 25 50 75;
30 60 90];
result{2,1}= basically the values corresponding to those 3 numbers in B{2,1};
If I use a for loop it takes TOO LONG (more than an hour). I can do a cellfun if I define the function in a separate file or by defining it with "function" within the script but I would like to just do an inline function and call that using cellfun. So I saved A, B, and C into a 1xn cell array where each row is a 1x3 cell (A, B, C) so that cellfun would work (single input)
findVal = @(x)x{3}(ismember(x{1},x{2}),:);
coords=cellfun(@findVal, vals, 'UniformOutput',false);
But then I get this error:
Error: "findVal" was previously used as a variable, conflicting with its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB documentation for details.
if I just call findVal(vals{1,1}) then it gives me what I want. And if I define the function in a different file it works too. Why doesn't this work for inline defined functions?
EDIT: I reverted back to the for loop because I realized cellfun was quite slow. But would still like to know how to use anonymous functions within cellfun

  0 Comments

Sign in to comment.

Accepted Answer

Matt J
Matt J on 20 Jan 2020
Edited: Matt J on 20 Jan 2020
But would still like to know how to use anonymous functions within cellfun
There's no difference. Since findVal is already a function handle, you need to omit the '@',
coords=cellfun(findVal, vals, 'UniformOutput',false);
But as you mentioned, cellfun is not expected to outperform a for-loop.

  1 Comment

Megna Hari
Megna Hari on 22 Jan 2020
oh wow I just assumed all functions being called in cellfun required the @. Good to know!

Sign in to comment.

More Answers (1)

Matt J
Matt J on 20 Jan 2020
Edited: Matt J on 20 Jan 2020
I reverted back to the for loop
This might be faster than the for-loop,
allB=[B{:}];
idx=ismember(allB,A);
Blens=cellfun('length',B);
G=repelem(1:numel(Blens),Blens);
rlens=sparse(G,1:numel(G),1)*idx(:);
results=mat2cell( C(allB(idx),:) ,rlens,size(C,2) );

  2 Comments

Megna Hari
Megna Hari on 22 Jan 2020
That's an interesting way to do it. I'll try it out - thanks!
Megna Hari
Megna Hari on 24 Jan 2020
OMG THANK YOU
This helped reduce my run time by 10 whole minutes.
Instead of 40 minutes it's 30 minutes which is definitely much better!

Sign in to comment.

Sign in to answer this question.