필터 지우기
필터 지우기

How to deal with error "Input #3 expected to be a cell array, was double instead." using cellfun?

조회 수: 5 (최근 30일)
Hello everyone,
as I'm trying to lean how to code with vectorization and do this magic with little lines and incredible performence there are a few hurdles for me to overcome. First I need to learn how to use cellfun correctly. Though I think the descriptions MathWorks gives are often usefull they are just examples and not always show me the solutions to my issues. Please note I am here to learn. There are a lot of things to learn for me. So I am thankful for ever advice to improve my coding. And I am not a native english speaker so there is the possibility that I just don't get right what the discription is supposed to tell me.
I have the following code:
[~,locs{1},~,~] = findpeaks(Mbx,t,'MinPeakHeight',maxPeakx,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis ,'Annotate','extents');
[~,locs{2},~,~] = findpeaks(-Mbx,t,'MinPeakHeight',maxPeakx,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis,'Annotate','extents');
[~,locs{3},~,~] = findpeaks(Mby,t,'MinPeakHeight',maxPeaky,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis ,'Annotate','extents');
[~,locs{4},~,~] = findpeaks(-Mby,t,'MinPeakHeight',maxPeaky,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis,'Annotate','extents');
In this case Mbx, Mby and t are arrays and the code works. It gives me the locations of the peaks in terms of t (time vector).
But I want to be able to get the same result in just one line without using a loop.
So I formed a cellarray with:
Mb = {Mbx -Mbx Mby -Mby};
and tried to perform a cellfun like this:
[~,locs,~,~] = cellfun(@(x)findpeaks(x,t,'MinPeakHeight',maxPeakx,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis ,'Annotate','extents'),Mb,'UniformOutput',false);
I thought x is the input for my function and it performes findpeaks with all given name and value pairs on each cell of it.
But a get the error:
Error using cellfun
Input #3 expected to be a cell array, was double instead.
So what is Input #3 in this case? And how do I hand over the name value pairs for findpeaks correctly to the cellfun?
kind regards
Fabian

답변 (1개)

David Hill
David Hill 2021년 6월 25일
Loops are not bad. cellfun is performing a loop.
Mb = {Mbx -Mbx Mby -Mby};
for k=1:4
[~,locs{k},~,~] = findpeaks(Mb{k},t,'MinPeakHeight',maxPeakx,'MinPeakWidth',MinPeakWi,'MinPeakDistance', MinPeakDis ,'Annotate','extents');
end
  댓글 수: 5
Stephen23
Stephen23 2021년 6월 25일
편집: Stephen23 2021년 6월 25일
"I read comments from which I conclude the opposite"
Please give a link to that MATLAB Answers thread.
"You are of the oppinion I should concentrade on writing better loops then?"
No, I am of the opinion that every tool has its uses.
"as I'm trying to lean how to code with vectorization and do this magic with little lines and incredible performence there are a few hurdles for me to overcome. First I need to learn how to use cellfun correctly."
CELLFUN is not really relevant to code vectorization. Read the definition here:
Fabian Lürßen
Fabian Lürßen 2021년 6월 25일
Okay sorry I believe you. I am barely six weeks into MATLAB and think I got something a little wrong and confused vectorisation with those one-liners where you call an anonymous function. It is kind of hard to understand all the descriptions with all those specific foreign words memorize and implement them.
But do you have any advice on how to write clearer loops and avoid things that slow down the performance? I thing those calls on find in a loop are sure a bad idea for the performance. But I simply did the work with the tools I know.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by