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