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

조회 수: 11 (최근 30일)
Megna Hari
Megna Hari 2020년 1월 20일
댓글: Megna Hari 2020년 1월 24일
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
Megna Hari
Megna Hari 2020년 1월 22일
oh wow I just assumed all functions being called in cellfun required the @. Good to know!

댓글을 달려면 로그인하십시오.

추가 답변 (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월 22일
That's an interesting way to do it. I'll try it out - thanks!
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에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by