How to concatenate a (REAL) string vector?

조회 수: 3 (최근 30일)
Csaba
Csaba 2021년 4월 22일
편집: Stephan 2021년 4월 22일
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?

채택된 답변

Stephan
Stephan 2021년 4월 22일
편집: Stephan 2021년 4월 22일
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
Stephen23
Stephen23 2021년 4월 22일
편집: Stephen23 2021년 4월 22일
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 2021년 4월 22일
편집: Stephan 2021년 4월 22일
Thank you @Stephen Cobeldick - i edited the incorrectness

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by