How to calculate the sum of the numbers that can be divided with 12 for example in a matrix?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
How to calculate the sum of the numbers that can be divided with 12 for example in a matrix? Should I use a while or for loop?
R=randi([107 167],1,57)
2 Kommentare
James Tursa
am 3 Okt. 2017
Bearbeitet: James Tursa
am 3 Okt. 2017
You should use a mod() or rem() with logical indexing and a sum(). Were you instructed to use a loop for this exercise?
Image Analyst
am 3 Okt. 2017
All numbers can be divided by 12. I assume you mean with no remainder, so James answer should work.
Antworten (2)
Linus Michelin
am 27 Sep. 2021
I did it like this:
S=0;
for R=randi([107 167],1,57)
if mod(R,12)==0
S=S+R;
else S=S;
end
end
S
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!