having if/else statement go to previous input

I have a matrix, opt, that consists of 3 outputs, 1/2/3. Output 1 goes through a bunch of steps, output 2 goes through a bunch of different steps, and if opt is 3 there's a 50/50 chance it goes through either set of steps. How do I set this up? I want something like this:
if opt == 1
abc
elseif opt == 2
xyz
else
if rand() < 0.5
opt == 1 %go back up to where "if opt == 1" is and go through steps abc
else
opt == 2 %go back up to where "if opt == 2" is and go through steps xyz
end
end
instead of having to do this:
if opt == 1
abc
elseif opt == 2
xyz
else
if rand() < 0.5
abc
else
xyz
end
end
The code is already about 1000 lines and I don't want to double it. Is there anyway I can have it go back up to where those previous elseif statements are?

댓글 수: 3

Umar
Umar 2024년 11월 2일
편집: Umar 2024년 11월 2일

Hi @Kitt,

To achieve your goal without increasing the size of your code significantly, you can utilize function calls or a structured approach that avoids duplication while maintaining clarity. Below is a sample implementation in pseudocode that demonstrates how to encapsulate the logic for outputs 1 and 2 in functions:

//pseudo code
function processOutput1()
  // Steps for output 1
  abc
end function
function processOutput2()
  // Steps for output 2
  xyz
end function
if opt == 1 then
  processOutput1()
elseif opt == 2 then
  processOutput2()
else 
  if rand() < 0.5 then
      processOutput1() // Calls the function for output 1
  else
      processOutput2() // Calls the function for output 2
  end if
end if

By defining processOutput1 and processOutput2, you encapsulate the steps associated with each output. This avoids repetition in your main logic.The initial conditional checks (if opt == 1, elseif opt == 2) remain clear and straightforward, directing the flow appropriately based on the value of opt. So, in the case where opt is equal to 3, you simply call one of the two functions based on a random choice, maintaining clarity and preventing redundancy.

Hope this helps.

Umar
Umar 2024년 11월 2일
Recursive calls can lead to stack overflow if not managed carefully, especially with deep recursion.
or use a label with a conditional jump
I don't think I understand what you are saying there, @Umar . MATLAB does not have any concept of GOTO or labeled statements.

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

답변 (3개)

Torsten
Torsten 2024년 11월 1일
편집: Torsten 2024년 11월 1일

0 개 추천

r = rand();
if opt == 1 || (opt == 3 && r < 0.5)
abc
else
xyz
end
Anjaneyulu Bairi
Anjaneyulu Bairi 2024년 11월 1일
편집: Anjaneyulu Bairi 2024년 11월 1일

0 개 추천

Hi,
These are the points to be noted here:
  • If "opt" is equal to 1 then execute abc
  • If "opt" is equal to 2 then execute xyz
  • if "opt" is 3 or something else then based on rand() value execute either abc( if rand()<0.5) or xyz
Refer the below code for above logic implementation
if opt == 1 || (opt ~= 2 && rand() < 0.5)
abc
else
xyz
end
Bruno Luong
Bruno Luong 2024년 11월 2일
편집: Bruno Luong 2024년 11월 2일

0 개 추천

This is how I would do. It seems clearer to me.
if opt == 3
opt = randi([1 2]); % randi(2) is also fine
end
if opt == 1
abc
else % if opt == 2
xyz
end

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2024년 11월 1일

편집:

2024년 11월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by