필터 지우기
필터 지우기

How to Put a Specific Function Inside a For Loop Depending on the Result of an If Statement?

조회 수: 1 (최근 30일)
I am currently using something like below. The problem is that for each iteration the if statement is checked and it slows down the execution
function myfun(in1)
for i = 1 : 1000
for j = 1 : 1000
if in1 = '1'
[out2] = myfun2(in2);
elseif in1 = '2'
[out3] = myfun3(in3);
end
end
end
How can i change it to the following where it once first checks whether it is ‘1’ or ‘2’ and then always put myfun2 or myfun3 in the for loop.
function myfun(in1)
if in1 = '1'
[out2] = myfun2(in2);
elseif in1 = '2'
[out3] = myfun3(in3);
end
for i = 1 : 1000
for j = 1 : 1000
EXECUTE myfun2 or myfun3, DEPENDING ON THE IF STATEMENT
end
end

채택된 답변

Jan
Jan 2017년 3월 26일
function myfun(in1)
if in1 = '1'
fcn = @myfun2;
arg = in2;
else
fcn = @myfun3;
arg = in3;
end
for i = 1 : 1000
for j = 1 : 1000
out = fcn(arg);
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by