How to calculate the sum of the numbers that can be divided with 12 for example in a matrix?

2 views (last 30 days)
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 Comments
James Tursa
James Tursa on 3 Oct 2017
Edited: James Tursa on 3 Oct 2017
You should use a mod() or rem() with logical indexing and a sum(). Were you instructed to use a loop for this exercise?

Sign in to comment.

Answers (2)

KSSV
KSSV on 4 Oct 2017
R=randi([107 167],1,57) ;
K = mod(R,12) ;
iwant = sum(R(K==0)) ;

Linus Michelin
Linus Michelin on 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

Products

Community Treasure Hunt

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

Start Hunting!