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

4 views (last 30 days)
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 Comments
Romain
Romain on 22 May 2014
Edited: Romain on 22 May 2014
Can you specify the class, and size of ii, jj, nn and N1 ?
Patty
Patty on 24 May 2014
yes, and actually the numbers are not big.
ii=1:13 jj=1:13
nn=1:10
N1=1:5

Sign in to comment.

Answers (1)

George Papazafeiropoulos
George Papazafeiropoulos on 24 May 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.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!