How do I only show 1 output instead of all the outputs as a result of the matrix

5 vues (au cours des 30 derniers jours)
I'm working a variation of this code and I don't understand how to supppress the output where I don't get all 6 elements every time I run the code. Basically, I just want to see the solution once and not various times in the command window (I just don't know what to add to do that). I've tried variations with display, fft (although I tried this, I am not familiar with the command), and some other commands that just made my solutions disapear. I'm completely stumped.
num=rand(3,2);
[row1,col1]=size(num);
for a=1:row1
for b=1:col1
disp('You chose an appropriate matrix')
end
end
Any advice, or what video/resouce I shoud look at, is appreciated.

Réponse acceptée

Walter Roberson
Walter Roberson le 23 Fév 2021
num = randi(10,3,2);
[row1,col1]=size(num);
foundit = false;
for a=1:row1
for b=1:col1
if num(a,b) == 7
fprintf('You chose an appropriate matrix at (%d,%d)\n', a, b);
foundit = true;
break
end
end
if foundit; break; end
end
You chose an appropriate matrix at (3,2)
if ~foundit
fprintf('Better luck next time!\n');
end
  3 commentaires
Walter Roberson
Walter Roberson le 23 Fév 2021
yes the %d is just formatting the output, which I did in order to emphasize that the solution could be found at any point in the matrix
if num(a,b) == 7
is not an assignment, it is a test. It is an arbitrary test as it is not clear what you want to do.
Danny Allen
Danny Allen le 23 Fév 2021
Thank you that makes it much clearer! Thank you for your help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by