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

2 views (last 30 days)

Show older comments

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 Comments

### Answers (4)

SALAH ALRABEEI
on 12 Jun 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
on 12 Jun 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 Comments

John D'Errico
on 12 Jun 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 Comment

Walter Roberson
on 12 Jun 2021

Image Analyst
on 12 Jun 2021

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!