The mod() function
We might all remember the days when we were taught to divide and then set the remainder aside if the numbers didn't divide equally.
In mathematics we call the the modulus or mod operator. It is often represented as the % operator, but in MATLAB we have the mod() function. So the above examples look like this:
mod(5,2) returns 1
mod(103, 20) returns 3
We can also use mod() on a matrix.
mod([5 103], 20) returns [5, 3]
In this problem, you'll use the mod() function to count the number of times a number is an even multiple of the elements of a random matrix of integers.
You'll receive 2 arguments
- vv = A random matrix of numbers
- nn = A number
function mn = multNumb(vv, nn)
mn = % the number of elements in vv that are multiples of nn
end
For example
multNumb([1 2 3 4 5 6], 2) % returns 3 (2, 4, and 6)
Write multNumb.
Hint: The numel() function returns the number of elements in a matrix.
Solution Stats
Solution Comments
Show comments
Loading...
Problem Recent Solvers31
Suggested Problems
-
2347 Solvers
-
Project Euler: Problem 6, Natural numbers, squares and sums.
2573 Solvers
-
Given a matrix, swap the 2nd & 3rd columns
1270 Solvers
-
Can the given sides form a triangle?
141 Solvers
-
Sum of unique multiples of 3 and 5
62 Solvers
More from this Author3
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!