How do I only show 1 output instead of all the outputs as a result of the matrix
16 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Danny Allen
le 23 Fév 2021
Commenté : Danny Allen
le 23 Fév 2021
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.
0 commentaires
Réponse acceptée
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
if ~foundit
fprintf('Better luck next time!\n');
end
3 commentaires
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.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!