How do you call an inline or anonymous function within cellfun?
조회 수: 11 (최근 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.
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!