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

7 visualizaciones (últimos 30 días)
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 comentario
Steven Lord
Steven Lord el 20 de Oct. de 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

Iniciar sesión para comentar.

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Explicit MPC Design en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by