Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Identify integers in a matrix

조회 수: 1 (최근 30일)
Krish Desai
Krish Desai 2015년 9월 27일
마감: MATLAB Answer Bot 2021년 8월 20일
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
Andrei Bobrov
Andrei Bobrov 2015년 9월 29일
sumDiv5 = @(numbers)sum(rem(numbers(:),5)==0)

답변 (1개)

Image Analyst
Image Analyst 2015년 9월 27일
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