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일

1 개 추천

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

댓글 수: 3

ANKUR KUMAR
ANKUR KUMAR 2017년 11월 1일
Could you please help me to explain, when to use @cellfun and when to use @arrayfun
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개)

카테고리

도움말 센터 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품

태그

질문:

2017년 11월 1일

편집:

2017년 11월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by