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

13 次查看(过去 30 天)
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

采纳的回答

Matt J
Matt J 2020-1-20
编辑:Matt J 2020-1-20
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 个)

Matt J
Matt J 2020-1-20
编辑:Matt J 2020-1-20
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 个评论
Megna Hari
Megna Hari 2020-1-24
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!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by