Join strings together with '_' between them

조회 수: 15 (최근 30일)
Smithy
Smithy 2022년 9월 20일
댓글: Smithy 2022년 9월 21일
Hello everybody,
I have a two vectors and I would like to join together with a and b below with some repetition.
The output I want is c1 as below. I made the characters before _ using repmat function.
and made the characters after _ using repelem function.
However, I failed to join them togerther with '_' between them.
Please give me some helps to join them togerther ?
clc; close all; clear;
a = {'A11';'A12';'A21';'A22';'A23';'A24'};
b = {'A300';'A5300';'A291';'A5291'};
c1 = [repmat(a,length(b),1) , '_' , repelem(b,length(a),1)];
% c1 = ['A11_A300';'A12_A300';'A21_A300';'A22_A300';'A23_A300';'A24_A300';...
% 'A11_A5300';'A12_A5300';'A21_A5300';'A22_A5300';'A23_A5300';'A24_A5300';...
% 'A11_A291';'A12_A291';'A21_A291';'A22_A291';'A23_A291';'A24_A291';...
% 'A11_A5291';'A12_A5291';'A21_A5291';'A22_A5291';'A23_A5291';'A24_A5291']

채택된 답변

KSSV
KSSV 2022년 9월 20일
a = {'A11';'A12';'A21';'A22';'A23';'A24'};
b = {'A300';'A5300';'A291';'A5291'};
a1 = repmat(a,length(b),1) ;
b1 = repelem(b,length(a),1);
m = repelem('_',length(a1),1) ;
ab = strcat(a1,m,b1)
ab = 24×1 cell array
{'A11_A300' } {'A12_A300' } {'A21_A300' } {'A22_A300' } {'A23_A300' } {'A24_A300' } {'A11_A5300'} {'A12_A5300'} {'A21_A5300'} {'A22_A5300'} {'A23_A5300'} {'A24_A5300'} {'A11_A291' } {'A12_A291' } {'A21_A291' } {'A22_A291' } {'A23_A291' } {'A24_A291' } {'A11_A5291'} {'A12_A5291'} {'A21_A5291'} {'A22_A5291'} {'A23_A5291'} {'A24_A5291'}
  댓글 수: 1
Smithy
Smithy 2022년 9월 21일
Wow... thank you very mcuh. I really really appreciate with it. It works perfect.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2022년 9월 20일
Use the string append operator +.
a = {'A11';'A12';'A21';'A22';'A23';'A24'};
b = {'A300';'A5300';'A291';'A5291'};
C1 = a + "_" + b.'
C1 = 6×4 string array
"A11_A300" "A11_A5300" "A11_A291" "A11_A5291" "A12_A300" "A12_A5300" "A12_A291" "A12_A5291" "A21_A300" "A21_A5300" "A21_A291" "A21_A5291" "A22_A300" "A22_A5300" "A22_A291" "A22_A5291" "A23_A300" "A23_A5300" "A23_A291" "A23_A5291" "A24_A300" "A24_A5300" "A24_A291" "A24_A5291"
C2 = a.' + "_" + b
C2 = 4×6 string array
"A11_A300" "A12_A300" "A21_A300" "A22_A300" "A23_A300" "A24_A300" "A11_A5300" "A12_A5300" "A21_A5300" "A22_A5300" "A23_A5300" "A24_A5300" "A11_A291" "A12_A291" "A21_A291" "A22_A291" "A23_A291" "A24_A291" "A11_A5291" "A12_A5291" "A21_A5291" "A22_A5291" "A23_A5291" "A24_A5291"
  댓글 수: 1
Smithy
Smithy 2022년 9월 21일
Thank you very mcuh. It works really well.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by