MATLAB: Sum of Digits

digits

Hi,
If I have the following vector: [1 9 11 3 7 8 14] then how can I add the double digits (when the number is greater than 9)to get a single digit? i.e. to get: [1 9 2 3 7 8 5]

Best Answer

a=[1 9 11 3 7 8 14]
a(a>9)=a(a>9)-9
Edit: Like John said the code fails for numbers bigger than 18 and we can repeat the process, the following code works for every number but it will be slow for bigger values and vectors.
while max(a)>9
a(a>9)=a(a>9)-9
end