How do I organize my code so it works?
Show older comments
Hello, I have to write a function that takes a vector of single-digit integers and counts the occurrences of each digit, from 0 to 9, in the vector using a single loop. Your function should take a vector in_vector of arbitrary length, which contains single-digit integers, and return a row vector num_of_digits of length 10. Here, the k-th entry in num_of_digits should contain the number of occurrences in in_vector of digit k-1.
For example, the number of zeros in in_vector would be listed as the first entry in num_of_digits, and so on. If in_vector is [0,0,1,3,2] then num_of_digits should be [2,1,1,1,0,0,0,0,0,0].
BUT I have written it already its just not working and Im not sure why. I keep getting weird errors like: Unrecognized function or variable 'num_of_digits' or function script needs an end.

Answers (1)
Voss
on 1 Mar 2023
"Code to call your function" should be something like:
in_vector = [0,0,1,2,3];
num_of_digits = count_digits(in_vector);
disp(num_of_digits)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!