How to deal with error "Input #3 expected to be a cell array, was double instead." using cellfun?
이전 댓글 표시
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
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
Fabian Lürßen
2021년 6월 25일
"They just don't bring the performance I need"
CELLFUN is generally slower than the equivalent well-written loop:
etc.etc.
Fabian Lürßen
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
2021년 6월 25일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!