Can I call index of max function out and before the function ?

4 views (last 30 days)
Below is an example of my question, can I call the index of max function which is located inside for loop before the the loop ? I appreciate any help
option = [1 2 5 7 8]
g = option(idx) % Can I call the index in this way ?
for i =1:10
[max idx] = max(somthing)
end

Accepted Answer

Walter Roberson
Walter Roberson on 25 Feb 2023
Only if all of the "something" are already known at the time you need the index for g, in which case you max() before the loop.
  3 Comments
Walter Roberson
Walter Roberson on 25 Feb 2023
Edited: Walter Roberson on 25 Feb 2023
Suppose you have 10 people lined up. You have each of them select 5 "Clubs" cards for a deck of cards and shuffle the minideck of 5 Clubs, and for each of them you go through and find out what the highest card value is and which position it occurred in. And what you are asking of us at this time is whether there is a way to arrange the position of the maximum card ahead of time for each of the 10 people who are each acting independently. It is not clear at the moment but it sounds like you would like it to be the same position for each of the 10 people.
As long as the people are acting independently, there are a few ways to proceed:
  1. use some kind of slight-of-hand when you collect the cards from them to move the high-valued card to the desired position
  2. use some kind of slight-of-hand in the shuffling process so that they themselves put the high card into the desired position
  3. use some kind of slight of hand in revealing the "prediction" of the position that you made so that it changes to match the actual position
  4. have some equipment to secretly read the cards after they shuffle so that you can "predict" the order
  5. have the predictions supposedly placed in your pockets or in your collar or behind your ear, but in reality have multiple copies of each possible prediction and pull the appropriate one out when needed.
Of course if they are not acting independently, then you have other options as well.
omar th
omar th on 25 Feb 2023
@Walter Roberson thank you for your calirification. I am sure my question wasn't make sense. My real problem is explained below. I have a robot and I want that robot walk in straight line according to a condition that's why I tried to not change the index of max function due to the angle is a function of that index. any way I want to fix the index to fix the same angle in the next iterations. This is the reason that I asked for fixing index despite its not make sense. If you have any advice I would appreciate your help.
option = [20 25 50 75 87]; % angles option
v=2; % m/s velocity of robot
OldPosition = [-25 25]; % position of robot
for i =1:10
for j=1:5
payoff(j) = do some calculation
end
[max idx] = max(payoff)
angle = option(idx);
displacement = [cos(angle) .* v4 ,sin(angle) .* v4]
newPosition = OldPosition + displacement;
end

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 25 Feb 2023
There's another problem with your code above and beyond what Walter described. If you want to call the max function inside your loop you cannot name the first output of that function call max. In a function this code will error at the first iteration, since MATLAB will associate the identifier max with the variable not the function but the variable has not been created when you try to index into it the first time. In a script or at the command line the first iteration will call the function but the second and subsequent iterations will attempt to index into the variable.
Since parentheses indexing can't return two outputs you get one error with your code as written. Even if you modified that line to be max = max(option) you'd still get an error, because the max variable is a scalar and so doesn't have an (in this example) 2nd, 5th, 7th, or 8th element.
option = [1 2 5 7 8];
option = 1×5
1 2 5 7 8
for i =1:10
fprintf("Starting iteration %d\n", i)
[max idx] = max(option)
end
Starting iteration 1
max = 8
idx = 5
Starting iteration 2
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
  2 Comments
omar th
omar th on 25 Feb 2023
@Steven Lord thank you for your response, actually I explained the real reason to Walter behind asking about fixing the index despite itsn't make sense. I will share the reason and I hope to advise me about it.
My real problem is explained below. I have a robot and I want that robot walk in straight line according to a condition that's why I tried to not change the index of max function due to the angle is a function of that index. any way I want to fix the index to fix the same angle in the next iterations. This is the reason that I asked for fixing index despite its not make sense. If you have any advice I would appreciate your help.
option = [20 25 50 75 87]; % angles option
v=2; % m/s velocity of robot
OldPosition = [-25 25]; % position of robot
for i =1:10
for j=1:5
payoff(j) = do some calculation
end
[max idx] = max(payoff)
angle = option(idx);
displacement = [cos(angle) .* v4 ,sin(angle) .* v4]
newPosition = OldPosition + displacement;
end

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!