Can I call index of max function out and before the function ?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
omar th
le 25 Fév 2023
Commenté : Walter Roberson
le 26 Fév 2023
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
0 commentaires
Réponse acceptée
Walter Roberson
le 25 Fév 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 commentaires
Walter Roberson
le 25 Fév 2023
Modifié(e) : Walter Roberson
le 25 Fév 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:
- use some kind of slight-of-hand when you collect the cards from them to move the high-valued card to the desired position
- use some kind of slight-of-hand in the shuffling process so that they themselves put the high card into the desired position
- 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
- have some equipment to secretly read the cards after they shuffle so that you can "predict" the order
- 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.
Plus de réponses (1)
Steven Lord
le 25 Fév 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];
for i =1:10
fprintf("Starting iteration %d\n", i)
[max idx] = max(option)
end
2 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!