# How to set which part of the code to comment using a variable?

조회 수: 1(최근 30일)
Francesco Lisi 2021년 6월 12일
답변: Image Analyst 2021년 6월 12일
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

댓글을 달려면 로그인하십시오.

### 답변(4개)

SALAH ALRABEEI 2021년 6월 12일
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)'])
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
SALAH ALRABEEI 2021년 6월 12일
@Walter yes, i forgot about it.

댓글을 달려면 로그인하십시오.

Walter Roberson 2021년 6월 12일
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표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

John D'Errico 2021년 6월 12일
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표시숨기기 없음
Walter Roberson 2021년 6월 12일
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 2021년 6월 12일
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().

댓글을 달려면 로그인하십시오.

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by