Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9296523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:50:57+00:00 2026-06-18T21:50:57+00:00

I have three looped operations O1 O2 O3 each with an IF statement and

  • 0

I have three looped operations O1 O2 O3 each with an IF statement and the operation with the largest flag=[F1 F2 F3] value has a higher priority to run.

How can I switch between operations depending on the value of that flag ? The flag value for each operation varies with time.

For simplicity, operation 1 is going to run first, and by the end of it’s loop the flag value will be the lowest, hence operation 2 or 3 should run next. So for this example, at t=0 : F1=5 F2=3 and F3=1.

The over-simplified pseudo code for what im trying to achieve :

while 1
  find largest flag value using [v index]=max(flag)
  Run operation with highest flag value
   ..loop back..
end

I am not sure how the value of flag will be compared in between operations, and hence why I hope for someone to shed some light on the issue here.

EDIT

Currently, all operations are written in one matlab file, and each is triggered with an IF statement. The operations run systematically one after the other (depending on which one is written first in matlab). I want to avoid that and trigger them depending on the flag value instead.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:50:59+00:00Added an answer on June 18, 2026 at 9:50 pm

    If your operations are functions (a little hard to tell from the question), then make a cell array of function handles, there fun1 is the name of one of your actual functions.

    handles = {@fun1, @fun2, @fun3}
    

    Then you can just use the index returned from your max term to get the correct function from the array. You can pass any arguments to the function using the following syntax.

    handles{index}(args)
    

    Using the style above makes the solution scalable, so you don’t need a stack of if statements that require maintenance when the number of operations expands. If the functions are really simple you can always use lambdas (or anonymous functions in Matlab speak).

    However, if you have a limited number of simple operations that are not likely to expand, you may choose to use a switch statement in your while loop instead. It conveys your intention better than a stack of if statements.

    while 1
      [~, index]=max(flag);
      switch index
          case 1
              operation1
              flag = [x y z]
          case 2
              operation2
              flag = [x y z]
          otherwise
              operation3
              flag = [x y z]
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three operations. Each operation is in a loop and has a flag
i have three lists with the same number of elements in each other, i
I have three tables. I have to retrieve the data using Linq statement. My
In my table I have looked manually and found that the top three idle
Have three classes User, Group and Field. Many to many relationship on User /
Have three divs in a container that I want to float over a large
I have three separate table variables in my Function, 1 of them is not
I have three files: lib.c lib.h => They should be built as a .so
i have three java based web application app1,app2 and app3 at production. All 3
I have three divs, within a content div. Container width 70%. Within that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.