editing in string array

10 次查看(过去 30 天)
ANKUR KUMAR
ANKUR KUMAR 2017-11-1
编辑: KSSV 2017-11-1
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?

采纳的回答

KSSV
KSSV 2017-11-1
编辑:KSSV 2017-11-1
a={'10','7','5','9','11','12','12','4'} ;
L = cellfun(@length,a) ;
idx = L==1 ;
a(idx) = strcat('0',a(idx)) ;
  3 个评论
KSSV
KSSV 2017-11-1
cellfun works on the cells.....it is nothing but a for loop on each cell. arrayfun works on rows/ cols of matrices.
Stephen23
Stephen23 2017-11-1
编辑:Stephen23 2017-11-1
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by