How to string 1 and 0 into array

3 visualizaciones (últimos 30 días)
Yu-Kuan Chang
Yu-Kuan Chang el 8 de Sept. de 2016
Comentada: Guillaume el 12 de Sept. de 2016
Hello, i have a question
(1) a string, '1101111000111101' (length 16)
how to into array [1 1 0 1 1 1 1 0 0 0 1 1 1 1 0 1]
if i use str2num, the result is 1.101111000111101e+15
Please if anyone knows the answer help me
(2) a array [1 1 0 1 1 1 1 0 0 0 1 1 1 1 0 1]
how to trans to string '1101111000111101'
Please if anyone knows the answer help me
thanks very much!!

Respuestas (3)

Fangjun Jiang
Fangjun Jiang el 8 de Sept. de 2016
'1101111000111101'-48
sprintf('%d',[1 1 0 1 1 1 1 0 0 0 1 1 1 1 0 1])
  2 comentarios
Andrei Bobrov
Andrei Bobrov el 9 de Sept. de 2016
+1
Guillaume
Guillaume el 12 de Sept. de 2016
Another option to get back a string is to simply revert the previous operation:
v = [1 1 0 1 1 1 1 0 0 0 1 1 1 1 0 1];
char(v + '0')

Iniciar sesión para comentar.


dpb
dpb el 8 de Sept. de 2016
Editada: dpb el 8 de Sept. de 2016
More generic for the numeric conversion is
sscanf('1101111000111101','%1d')
as can set the field width; the subtraction "trick" works for single digits, correct; however, I'd write
'1101111000111101'-48
as
'1101111000111101'-'0'
instead to remove the "magic number" 48 from the expression just as a stylistic change.

Yu-Kuan Chang
Yu-Kuan Chang el 9 de Sept. de 2016
thanks! the question 1 has been solve.
the question 2
a array [1 1 0 1 1 1 1 0 0 0 1 1 1 1 0 1]
how to trans to string '1101111000111101'
Please if anyone knows the answer help me
  1 comentario
dpb
dpb el 9 de Sept. de 2016
Editada: Fangjun Jiang el 12 de Sept. de 2016
Fangjun already did; didn't you try it to see, even?

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by