How to concatenate a (REAL) string vector?

3 visualizzazioni (ultimi 30 giorni)
Csaba
Csaba il 22 Apr 2021
Modificato: Stephan il 22 Apr 2021
I have a string matrix i.e.
A=["abcd",.....,"efghijk";...
. ...
. ...
. ...
"lm",......, "opkq"];
How to concatenate easily the rows of this matrix to get the result:
"abcdefghijk"
.
.
.
"lmopkq"
as a column vector?

Risposta accettata

Stephan
Stephan il 22 Apr 2021
Modificato: Stephan il 22 Apr 2021
A=["abcd","efghijk"; "lm", "opkq"]
A = 2×2 string array
"abcd" "efghijk" "lm" "opkq"
B = A(:,1) + A(:,2)
B = 2×1 string array
"abcdefghijk" "lmopkq"
The more general solution (independent from number of rows or columns of your input is:
A=["abcd","efghijk", "1111"; "lm", "opkq", "2222"; "fdds<jgf", "dfkjf", "ldfkj"; "<ksajfjf", "fjjf", "fkkgfkdsw43"]
A = 4×3 string array
"abcd" "efghijk" "1111" "lm" "opkq" "2222" "fdds<jgf" "dfkjf" "ldfkj" "<ksajfjf" "fjjf" "fkkgfkdsw43"
B = join(A,'',2)
B = 4×1 string array
"abcdefghijk1111" "lmopkq2222" "fdds<jgfdfkjfldfkj" "<ksajfjffjjffkkgfkdsw43"
  5 Commenti
Stephen23
Stephen23 il 22 Apr 2021
Modificato: Stephen23 il 22 Apr 2021
ERASE is superfluous**, simply specify both the delimiter and dimension:
A=["abcd","efghijk", "1111"; "lm", "opkq", "2222"; "fdds<jgf", "dfkjf", "ldfkj"; "<ksajfjf", "fjjf", "fkkgfkdsw43"];
B = join(A,'',2)
B = 4×1 string array
"abcdefghijk1111" "lmopkq2222" "fdds<jgfdfkjfldfkj" "<ksajfjffjjffkkgfkdsw43"
** and incorrect: consider what would happen if the strings themselves contain spaces.
Stephan
Stephan il 22 Apr 2021
Modificato: Stephan il 22 Apr 2021
Thank you @Stephen Cobeldick - i edited the incorrectness

Accedi per commentare.

Più risposte (0)

Categorie

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

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by