How to extract 1st digit of each number in an array

5 views (last 30 days)
Can anyone pls tell me what if x is an array of numbers can we seperate the 1st digit of each entry ..Like if x = [12 34 56 999 430 2]
and i want output out= [1 3 5 9 4 2]

Accepted Answer

madhan ravi
madhan ravi on 1 Feb 2019
out=cellfun(@(c)c(1),""+x)-'0'

More Answers (1)

Stephen23
Stephen23 on 1 Feb 2019
Edited: Stephen23 on 1 Feb 2019
Simpler efficient numeric operations, without multiple type conversions:
>> x = [12,34,56,999,430,2]
x =
12 34 56 999 430 2
>> fix(x./10.^fix(log10(x)))
ans =
1 3 5 9 4 2

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!