How to set which part of the code to comment using a variable?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Good morning,
I have a code that uses different algorithms on the same data and I have to compare them.
I want to do the following thing
Flag1=1;
Flag2=0;
Flag3=1;
if Flag1
DataOut1=zeros(1e9,1);
end
if Flag2
DataOut2=zeros(1e9,1);
end
if Flag3
DataOut3=zeros(1e9,1);
end
ParamIn=1:1e9;
for n=1:1e9
DataIn=randn(1);
if Flag1
DataOut1(n)=Algorithm1(ParamIn(n),DataIn);
end
if Flag2
DataOut2(n)=Algorithm2(ParamIn(n),DataIn);
end
if Flag3
DataOut3(n)=Algorithm3(ParamIn(n),DataIn);
end
end
Is there a way to avoid using the if statement and to use the flags to comment the different parts of the code instead. That would save the time to check the if condition since the number of iterations in the for cycle can be really high. I cannot split the for cycles since they have to work on the same DataIn.
Thank you for your help.
Francesco
0 comentarios
Respuestas (4)
SALAH ALRABEEI
el 12 de Jun. de 2021
See this example
clear
clc
a=2;
b=3
eval(['c',num2str(a),'=',','A',num2str(a),'(',num2str(b),')'])
function y=A1(x)
y=sin(x);
end
function y=A2(x)
y=cos(x);
end
In ur case a=1 or 2 or 3 which is the flag,
DataOut1(n)= eval(['DataOut',num2str(a),'(n)=',
'Algorithm',num2str(a),'(ParamIn(n),DataIn)'])
Walter Roberson
el 12 de Jun. de 2021
If practical, vectorize the Algorithm* routines, and combine them into one routine that also takes the flags.
DataIn = randn(1e9,1);
[Dataout1, Dataout2, Dataout3] = Algorithm(DataIn, Flag1, Flag2, Flag3);
You would not need ParamIm as it would be implied by the row number of DataIn.
0 comentarios
John D'Errico
el 12 de Jun. de 2021
NO. You do NOT want to comment out code programmatically. That will NOT be faster, even if you can find a way to do it. Your promatically modified code will now become incredibly slow. (And probably buggy too.) And you want to do this a billion times? Sorry, but you need to learn MATLAB better, learning to write efficient code. But this approach you think you want is NOT the solution.
For example, a simple re-arrangement of your code will perform the test on flag only once for each flag.
Flag1=1;
Flag2=0;
Flag3=1;
if Flag1
DataOut1=zeros(1e9,1);
for n=1:1e9
DataIn=randn(1);
DataOut1(n)=Algorithm1(ParamIn(n),DataIn);
end
end
if Flag2
DataOut2=zeros(1e9,1);
for n=1:1e9
DataIn=randn(1);
DataOut1(n)=Algorithm2(ParamIn(n),DataIn);
end
end
if Flag3
DataOut2=zeros(1e9,1);
for n=1:1e9
DataIn=randn(1);
DataOut3(n)=Algorithm3(ParamIn(n),DataIn);
end
end
It is possible that better code yet (that understands what the various algorithms are) may be eble to completely replace the loops, or at least partially replace them.
1 comentario
Walter Roberson
el 12 de Jun. de 2021
The user's current code uses the same randn() value for all active algorithms; the modified version uses different values. You would need to generate the 1e9 randn ahead of time and feed the appropriate entry into the function. (For space reasons you might want to run batches instead of storing all 1e9 random numbers.)
Image Analyst
el 12 de Jun. de 2021
Once you've got that figured out then, to COMPARE the algorithms, you might want to look into an ROC analysis using the MATLAB function perfcurve().
0 comentarios
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!