Azzera filtri
Azzera filtri

How to concatenate string array to matrix?

23 visualizzazioni (ultimi 30 giorni)
Hyon Kim
Hyon Kim il 1 Mar 2017
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,

Risposte (1)

Adam
Adam il 1 Mar 2017
Modificato: Adam il 1 Mar 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 Commenti
Hyon Kim
Hyon Kim il 2 Mar 2017
I see. Thank you so much for your help!
Walter Roberson
Walter Roberson il 2 Mar 2017
tables can also mix strings with numbers, but they are pretty much implemented as cell arrays.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by