a={'10','7','5','9','11','12','12','4'}
I want to put 0 at the starting of all those whose length is 1. How can I do this easily?

 Respuesta aceptada

KSSV
KSSV el 1 de Nov. de 2017
Editada: KSSV el 1 de Nov. de 2017

1 voto

a={'10','7','5','9','11','12','12','4'} ;
L = cellfun(@length,a) ;
idx = L==1 ;
a(idx) = strcat('0',a(idx)) ;

3 comentarios

ANKUR KUMAR
ANKUR KUMAR el 1 de Nov. de 2017
Could you please help me to explain, when to use @cellfun and when to use @arrayfun
KSSV
KSSV el 1 de Nov. de 2017
cellfun works on the cells.....it is nothing but a for loop on each cell. arrayfun works on rows/ cols of matrices.
Stephen23
Stephen23 el 1 de Nov. de 2017
Editada: Stephen23 el 1 de Nov. de 2017
More accurately:
  • cellfun applies the function to the contents of each cell of a cell array.
  • arrayfun applies the function to each element of any array (which could be numeric, cell, etc).
Use whichever suits your purpose.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 1 de Nov. de 2017

Editada:

el 1 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by