Extract the first two digits from a cell number

7 visualizaciones (últimos 30 días)
PEDRO ALEXANDRE Fernandes
PEDRO ALEXANDRE Fernandes el 24 de Mzo. de 2022
Comentada: Voss el 24 de Mzo. de 2022
Hello!
I have a cell array, and i need to extract the first two digits.
946210.887239541 5834.79659950033 2331.48462622471 1082.03333024461 555.948997161636 386.857494592061
163514355.623239 86011.7585899832 33427.7515152879 15281.0376072647 7801.56366753683 4742.13225247842
example:
94 58 23 ..
16 86 33 ..
i try this, cellfun(@(v)v(2),""+vnc)-'0'; but it return the 2 digit..
Any ideia?
thanks

Respuesta aceptada

Voss
Voss el 24 de Mzo. de 2022
Editada: Voss el 24 de Mzo. de 2022
vnc = { ...
946210.887239541 5834.79659950033 2331.48462622471 1082.03333024461 555.948997161636 386.857494592061
163514355.623239 86011.7585899832 33427.7515152879 15281.0376072647 7801.56366753683 4742.13225247842 };
% maybe this is what you had in mind:
cellfun(@(v)v(1:2)-'0',""+vnc,'UniformOutput',false)
ans = 2×6 cell array
{[9 4]} {[5 8]} {[2 3]} {[1 0]} {[5 5]} {[3 8]} {[1 6]} {[8 6]} {[3 3]} {[1 5]} {[7 8]} {[4 7]}
% or maybe this:
cellfun(@(v)v(1:2),""+vnc,'UniformOutput',false)
ans = 2×6 cell array
{'94'} {'58'} {'23'} {'10'} {'55'} {'38'} {'16'} {'86'} {'33'} {'15'} {'78'} {'47'}
% alternatively, returning a matrix of 2-digit numbers:
vnc_mat = cell2mat(vnc);
floor(vnc_mat.*10.^(1-floor(log10(vnc_mat))))
ans = 2×6
94 58 23 10 55 38 16 86 33 15 78 47
  2 comentarios
PEDRO ALEXANDRE Fernandes
PEDRO ALEXANDRE Fernandes el 24 de Mzo. de 2022
Amazing!
Thank you so much
Voss
Voss el 24 de Mzo. de 2022
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming 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