If 'variable' is an indexed variable, performance can be improved using logical indexing instead of FIND

3 views (last 30 days)
I get why it'd be "faster" using
A = find(C==4);
instead of
A = C==4;
Thing is, what if I NEED the number of the row/column ONLY instead of the logical array the latter gives?
Code in question:
if mpc.gen(17,8) ~= 0
elseif mpc.gen(16,8)~=0
C = find(mpc.bus(:,2)==3);
mpc.bus(C,2)=1;
B = mpc.gen(16,1);
D = find(mpc.bus(:,1)==B);
mpc.bus(D,2)=3;
end
I'm using two matrices:
Wherever there's a "3" in the mpc.bus array I need to change such number to a 1.
Then I need to find the row where the number located in "mpc.gen(16,1)" is, and change the number "next to it" (the column to the right of it) to a 3.
  1 Comment
Steven Lord
Steven Lord on 20 Oct 2015
You can mix logical and subscripted indexing, so there's no need for your two FIND calls.
A = magic(5)
col3GT10 = A(:, 3) > 10
A(col3GT10, 5) = A(col3GT10, 4)-A(col3GT10, 5)
Note that only rows 3, 4, and 5 of column 5 in A have been modified.
There are circumstances when you explicitly want to compute the numeric indices. In those cases, go ahead and compute them. But you may need more memory to do so. In this small a case it doesn't make much of a difference; in a larger case it might.
F = find(col3GT10);
whos col3GT10 F

Sign in to comment.

Accepted Answer

Thorsten
Thorsten on 20 Oct 2015
mpc.bus(mpc.bus(:,2)==3, 2) = 1;
mpc.bus(mpc.bus(:,1)==mpc.gen(16,1), 2)=3;

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!