Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Identify integers in a matrix

1 visualización (últimos 30 días)
Krish Desai
Krish Desai el 27 de Sept. de 2015
Cerrada: MATLAB Answer Bot el 20 de Ag. de 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 comentario
Andrei Bobrov
Andrei Bobrov el 29 de Sept. de 2015
sumDiv5 = @(numbers)sum(rem(numbers(:),5)==0)

Respuestas (1)

Image Analyst
Image Analyst el 27 de Sept. de 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