Info

This question is closed. Reopen it to edit or answer.

Identify integers in a matrix

1 view (last 30 days)
Krish Desai
Krish Desai on 27 Sep 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
Task: Write a function sumDiv5 which receives one input argument and returns how many elements of the input variable are divisible by 5.
When I have a row vector I am able to calculate this, but when I input a matrix my answer does not display correctly, what is wrong with my code?
function output= sumDiv5 (numbers)
A= floor(rem(numbers,5));
count=sum(A==0);
output=count;
Example of what happens:
>> sumDiv5(4:9)
ans =
1
>> sumDiv5([0 1 2 3 4; 2 5 8 13 15])
ans =
1 1 0 0 1
>>
  1 Comment
Andrei Bobrov
Andrei Bobrov on 29 Sep 2015
sumDiv5 = @(numbers)sum(rem(numbers(:),5)==0)

Answers (1)

Image Analyst
Image Analyst on 27 Sep 2015
You were told how to do this in your duplicate question: http://www.mathworks.com/matlabcentral/answers/245287#comment_312443
You didn't use (:) like you were told, so it's not doing it on the whole 2D matrix but on each column one column at a time.

Community Treasure Hunt

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

Start Hunting!