필터 지우기
필터 지우기

create a cell array of numbers

조회 수: 6 (최근 30일)
noa
noa 2013년 6월 16일
댓글: Walter Roberson 2020년 5월 28일
hi
i need to create a cell array with numbers from 0000-1000
notice that numbers must be in a 4-digit format
how can i do this?
thanks
  댓글 수: 3
Tobias Müller
Tobias Müller 2019년 4월 25일
cell(linspace(1,100,1000))
;)
Walter Roberson
Walter Roberson 2019년 4월 25일
linspace(1,100,1000) would contain non-integer values, which cell would not be able to use a dimension sizes to construct a 1000-dimensional cell array.

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 16일
편집: Azzi Abdelmalek 2013년 6월 16일
str = sprintf('%04d\n',0:1000)
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 5월 28일
This does not create a cell array.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 6월 16일
No. In MATLAB, numbers automatically drop leading 0's when displayed.
What is possible is to create a cell array of strings from '0000' to '1000'.
cellstr(num2str(0:1000).', '%04d')
Note: the .' is required there; without it the strings would come out wrong.
  댓글 수: 5
Walter Roberson
Walter Roberson 2020년 5월 28일
Thank you, Xiangrui Li, you are correct.
Walter Roberson
Walter Roberson 2020년 5월 28일
There is also the undocumented
sprintfc('%04d', 0:1000)
Also since R2016b, there has also been
compose('%04d', 0:1000)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by