Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Identify integers in a matrix

1 Ansicht (letzte 30 Tage)
Krish Desai
Krish Desai am 27 Sep. 2015
Geschlossen: MATLAB Answer Bot am 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 Kommentar
Andrei Bobrov
Andrei Bobrov am 29 Sep. 2015
sumDiv5 = @(numbers)sum(rem(numbers(:),5)==0)

Antworten (1)

Image Analyst
Image Analyst am 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!

Translated by