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