How to concatenate a (REAL) string vector?

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?

 Respuesta aceptada

Stephan
Stephan el 22 de Abr. de 2021
Editada: Stephan el 22 de Abr. de 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 comentarios

Csaba
Csaba el 22 de Abr. de 2021
Editada: Csaba el 22 de Abr. de 2021
That is really easy, however if you have n columns that would make the life more difficult. I do not want a special case, I would like to have more universal. Also I know that a for cycle would do it, but I want to do without for cycle. In numerical matrices sum(A) would do it, but it does not work for string vectors (BTW why not?? Formally it is the same, the '+' sign does the thing).
I have edited my question to make it clear! Sorry about wrongly (I mean not universally) put question!
Csaba
Csaba el 22 de Abr. de 2021
Yeah! Thank you. Join was the command I was looking for!
Please make it an answer to accept it!
Stephan
Stephan el 22 de Abr. de 2021
i edited the answer so that it is inside my answer
Stephen23
Stephen23 el 22 de Abr. de 2021
Editada: Stephen23 el 22 de Abr. de 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 el 22 de Abr. de 2021
Editada: Stephan el 22 de Abr. de 2021
Thank you @Stephen Cobeldick - i edited the incorrectness

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos

Etiquetas

Preguntada:

el 22 de Abr. de 2021

Editada:

el 22 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by