I want to calculate simulation time of only bigest length of A.
A={[1,2],[1,2,4,5,6,7],[4,8,2],[1,3,5,7]}
[~,index] = max(cellfun(@length,A)); % find index of maximum length
%
tic1 = tic;
[B]=cellfun(@(s)test(s),A )
elapsedTime = toc(tic1);
function[B]=test(A) %function
B=A+1
end
I want to calculate simulation time of A{index}.
I do not know how to use tic toc for this matter.

4 Kommentare

Walter Roberson
Walter Roberson am 8 Jul. 2019
Bearbeitet: Walter Roberson am 8 Jul. 2019
tic2 = tic;
B2 = test(A{index});
elapsedTime2 = toc(tic2);
Note: you shoud consider using timeit() instead of tic/toc
Bjorn Gustavsson
Bjorn Gustavsson am 8 Jul. 2019
Either scrap cellfun and loop over the elements of A, or plug tic-toc into your test-function.
NA
NA am 8 Jul. 2019
Bearbeitet: NA am 8 Jul. 2019
Thank you. I want to accept your answer (Walter Roberson), but you should answer it below.
If I want to use timeit(), this code have problem,
[B]=cellfun(@(s)test(s),A )
timeit([B{index}])
You cannot time an individual component of a cellfun.
timeit(@() test(A{index}), 0)

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Bjorn Gustavsson
Bjorn Gustavsson am 8 Jul. 2019

0 Stimmen

Either scrap cellfun and loop over the elements of A, or plug tic-toc into your test-function.

Weitere Antworten (0)

Kategorien

Mehr zu Performance and Memory finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by