How to reduce execution time? "If" and "ismember" functions

5 vues (au cours des 30 derniers jours)
Patty
Patty le 22 Mai 2014
I have a big code, and when analyze profiler results one of the consuming steps is the comparisson "if ii== jj" and "if nn==1"... Is there any other way to do this so I can reduce time?
if true
if ii == jj
...
else
if nn==1
...
else
...
end
end
end
The other function that is consuming a lot of time is "ismember". Also anyone knows a better way?
if true
if ismember(nn,N1)
...
else
...
end
end
Thanks!!
  3 commentaires
Romain
Romain le 22 Mai 2014
Modifié(e) : Romain le 22 Mai 2014
Can you specify the class, and size of ii, jj, nn and N1 ?
Patty
Patty le 24 Mai 2014
yes, and actually the numbers are not big.
ii=1:13 jj=1:13
nn=1:10
N1=1:5

Connectez-vous pour commenter.

Réponses (1)

George Papazafeiropoulos
George Papazafeiropoulos le 24 Mai 2014
To make ismember function faster, replace it with either ismembc or ismembc2 functions. ismembc returns an array of logical values while ismembc2 returns the index locations of the found members. More information about these can be found in the following:
https://www.mathworks.cn/matlabcentral/newsreader/view_thread/257728
Except for this, the "if ismember(nn,N1)" statement evaluates to true only if nn is a subset of N1. Since nn=1:10 and N1=1:5 it will always evaluate to false (the code after the above if statement will not be executed)
Since ii=1:13 and jj=1:13, the "if ii == jj" statement will evaluate always to true, preventing the next else section from execution.

Catégories

En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by