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

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2023-2-25
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 个评论
Walter Roberson
Walter Roberson 2023-2-25
编辑:Walter Roberson 2023-2-25
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 2023-2-25
@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

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2023-2-25
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 个评论
omar th
omar th 2023-2-25
@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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by