create a cell array of numbers

9 vues (au cours des 30 derniers jours)
noa
noa le 16 Juin 2013
Commenté : Walter Roberson le 28 Mai 2020
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 commentaires
Tobias Müller
Tobias Müller le 25 Avr 2019
cell(linspace(1,100,1000))
;)
Walter Roberson
Walter Roberson le 25 Avr 2019
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.

Connectez-vous pour commenter.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 16 Juin 2013
Modifié(e) : Azzi Abdelmalek le 16 Juin 2013
str = sprintf('%04d\n',0:1000)
  1 commentaire
Walter Roberson
Walter Roberson le 28 Mai 2020
This does not create a cell array.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 16 Juin 2013
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 commentaires
Walter Roberson
Walter Roberson le 28 Mai 2020
Thank you, Xiangrui Li, you are correct.
Walter Roberson
Walter Roberson le 28 Mai 2020
There is also the undocumented
sprintfc('%04d', 0:1000)
Also since R2016b, there has also been
compose('%04d', 0:1000)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by