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

4 visualizaciones (últimos 30 días)
Mazhar Ali
Mazhar Ali el 1 de Feb. de 2019
Editada: Stephen23 el 1 de Feb. de 2019
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]

Respuesta aceptada

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

Más respuestas (1)

Stephen23
Stephen23 el 1 de Feb. de 2019
Editada: Stephen23 el 1 de Feb. de 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

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by