count even numbers of a matrix

5 views (last 30 days)
Zisan Rahman
Zisan Rahman on 28 Dec 2021
Commented: Rik on 29 Dec 2021
Found how many even numbers are present in a 3×3 matrix. And find the value of multiple of those even numbers.
  1 Comment
Rik on 29 Dec 2021
Why did you remove your comment ("n=3; A=magic(n) for i=1:n*n for p=A(i) if rem(p,2)==0 p; end end end i tried this. but could not find the proper answer")?

Sign in to comment.

Answers (2)

Rik on 28 Dec 2021
This sounds a lot like homework. What did you try? I think you will be able to do it yourself if you read the documentation for the mod function.

Image Analyst
Image Analyst on 28 Dec 2021
No double for loop needed. Try this:
remainders = rem(A, 2) == 0
then use sum() to count the 1's.

Community Treasure Hunt

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

Start Hunting!

Translated by