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

4 visualizaciones (últimos 30 días)
Patty
Patty el 22 de Mayo de 2014
Respondida: George Papazafeiropoulos el 24 de Mayo de 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 comentarios
Romain
Romain el 22 de Mayo de 2014
Editada: Romain el 22 de Mayo de 2014
Can you specify the class, and size of ii, jj, nn and N1 ?
Patty
Patty el 24 de Mayo de 2014
yes, and actually the numbers are not big.
ii=1:13 jj=1:13
nn=1:10
N1=1:5

Iniciar sesión para comentar.

Respuestas (1)

George Papazafeiropoulos
George Papazafeiropoulos el 24 de Mayo de 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.

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by