Speed up logical operations

I am working on a problem: an array of sub-systems(over hundreds) , each has two states [0 1].
The pre-defined probability transition between are p1(0->1) and p2 (1->0)
The whole system is evolving with time, I am trying to use the logical operations to update the system state:
given an array of random number generated (pn)
SubSysState(SubSysState==0)=(p1(SubSysState==0)>pn(SubSysState==0))
SubSysState(SubSysState~=0)=(p2(SubSysState~=0)<pn(SubSysState~=0))
I did the 'Profiler' on the code, the above operations are lines where the most time was spent.
I am just wondering if there any other way to speed up it or anyone has better idea to do it?
Thanks a lot

Answers (1)

Compute the logical conditions only once:
SubSysStateeq0 = SubSysState==0;
SubSysStateneq0 = ~SubSysStateeq0;
SubSysState(SubSysStateeq0)=(p1(SubSysStateeq0)>pn(SubSysStateeq0))
SubSysState(SubSysStateneq0)=(p2(SubSysStateneq0)<pn(SubSysStateneq0))

1 Comment

Thank you, Sean
It did speed up a little. I should have thought about it.

Sign in to comment.

Categories

Find more on Elementary Math in Help Center and File Exchange

Asked:

on 2 May 2011

Community Treasure Hunt

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

Start Hunting!