How to generate a vector of character values?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
JING JIAO
el 9 de Oct. de 2018
Respondida: Steven Lord
el 9 de Oct. de 2018
I want to generate a vector A with all character values 1, 2, 3...N; For example, A=['1','2','3'...], N is changeable and can be defined beforehand. How can I do that?
0 comentarios
Respuesta aceptada
Guillaume
el 9 de Oct. de 2018
Editada: Guillaume
el 9 de Oct. de 2018
Your question is not very clear. Note that character '1' has a value of 49. Also note that there are only 10 characters which represents numerical digits, the characters '0123456789'. Finally also note that A=['1','2','3','4'] is exactly the same as A='1234'. If you want separate char arrays, each a textual representation of number you need to put the array in a cell array, A={'1','2','3','4'}, or a string array, A=["1","2","3","4"].
So, if you wanted to generate all characters with values between 1 and N, you'd use
char(1:N)
Be aware that characters with value below 32 are non-printable characters.
If you wanted to generate the digit characters starting at '1' up to maximum of 9, then
char((1:N) + '0')
Of course, if N is greater than 9, this will generate non-digit characters since as stated there's only 10 of them (and '0' is before '1')
If you wanted to generate a cell array of char array, each the numeric representation of an integer from 1 to N (with N potentially greater than 9):
compose('%d', 1:N) %for a cell array of char arrays
compose("%d", 1:N) %for a string array
Did I mention that the question was not clear?
Más respuestas (1)
Steven Lord
el 9 de Oct. de 2018
Does it have to be a char vector, or is a string vector acceptable?
N = 42;
S = string(1:N)
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!