How to concatenate string array to matrix?

I have a question regarding adding a string array to a matrix. i.e.
string array = {'A','B','C'}; B = eye(3); I want to concatenate those two in column, i.e. I want to get is eventually;
[1,0,0,A; 0,1,0,B; 0,0,1,C]
Thank you so much for your help in advance! Best,

Respuestas (1)

Adam
Adam el 1 de Mzo. de 2017
Editada: Adam el 1 de Mzo. de 2017
stringArray = {'A','B','C'};
B = eye(3);
result = [num2cell(B) stringArray'];
is the best you can do, resulting in a cell array.
[1,0,0,A; 0,1,0,B; 0,0,1,C]
is not a valid array in Matlab unless A, B and C are variables, but since you are talking about a cell array of chars I assume that is not the case.

4 comentarios

Hyon Kim
Hyon Kim el 1 de Mzo. de 2017
Thank you Adam Do you know why only cell array allows to mix char/string with numbers? Please let me know if you happen to know. Best,
Adam
Adam el 1 de Mzo. de 2017
Editada: Adam el 1 de Mzo. de 2017
Because that is their purpose. All other arrays are uni-type (ish).
Hyon Kim
Hyon Kim el 2 de Mzo. de 2017
I see. Thank you so much for your help!
Walter Roberson
Walter Roberson el 2 de Mzo. de 2017
tables can also mix strings with numbers, but they are pretty much implemented as cell arrays.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 1 de Mzo. de 2017

Comentada:

el 2 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by