MATLAB: In a set up that provides guesses at a random number: how to get Matlab to display the guess that was correct (e.g. 4th)

forguesseswhile

I've set up a loop including while, if, and else functions so that five guesses are made before a result is given whether these include the correct or incorrect answer but, in the event of correct, how to get it to display which guess was correct?

Best Answer

If you want to guess sequentially, you could have the guess be the index of a for loop.
If you want to guess randomly, you could use a while loop and get the guess by using the randi() function.
Note: guessing will only work if the guesses and correct answer are all integers, otherwise there is virtually no chance you would guess it for a floating point number.
Related Question