Using nested loops, write a Matlab program that goes through all elements of a matrix (2D array) and replaces every element that is either a multiple of 5 or 7 with -1.
Afficher commentaires plus anciens
This is what I have so far...
A=input('enter an array');
num_rows = size(A,1);
num_cols = size(A,2);
for i=1:1:num_rows
for j=1:1:num_cols
if A(i,j)/ 5 or 7 = integer
fprintf('-1')
end
end
end
I'm not sure how to identify the multiples of 5 and 7 and make them replace that with the -1
Réponses (1)
Roger Stafford
le 2 Avr 2018
Your test on a number x from your array can be:
if round(x/5)==x/5 | round(x/7)==x/7
(Doing fprintf('-1') just prints out "-1" and is totally useless for your purposes.)
2 commentaires
Ciara Gornoski
le 2 Avr 2018
Modifié(e) : Walter Roberson
le 2 Avr 2018
Walter Roberson
le 2 Avr 2018
if ...
A(i,j) = -1;
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!