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
0 个评论
采纳的回答
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!