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

7 次查看(过去 30 天)
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 个评论
Steven Lord
Steven Lord 2015-10-20
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

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Explicit MPC Design 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by