Combine 2 char vectors

조회 수: 18 (최근 30일)
Nicolas Baduel
Nicolas Baduel 2018년 5월 28일
답변: Stephen23 2018년 5월 29일
Hi everyone, Here are two vectors :
A='IMDOINGATEST'
B='ABCDE'
I would like to get this : (a regular mix)
C='IAMBDCODIENAGBACTDEESATB'
Note that A can be smaller than B but A won't repeat in B. Only B repeats in A.
Thank you !

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 28일
This will mix them as described in question
C = repmat(' ', 1, 2*numel(A))
C(1:2:end) = A;
C(2:2:end) = [repmat(B, 1, floor(numel(A)/numel(B))) B(1:rem(numel(A), numel(B)))];
For example,
A='IMDOINGATEST'
B='ABCDE'
will produce
C =
'IAMBDCODIENAGBACTDEESATB'
and
A='IMDO'
B='ABCDE'
will mix to form
C =
'IAMBDCOD' % no repetition of A
  댓글 수: 2
Nicolas Baduel
Nicolas Baduel 2018년 5월 28일
I will try this tomorrow, thank you for the answer !
Ameer Hamza
Ameer Hamza 2018년 5월 29일
You are welcome.

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

추가 답변 (1개)

Stephen23
Stephen23 2018년 5월 29일
A slightly different approach:
>> A = 'IMDOINGATEST';
>> B = 'ABCDE';
>> C = repmat(B,1,ceil(numel(A)/numel(B)));
>> C = reshape([A;C(1:numel(A))],1,[])
C = IAMBDCODIENAGBACTDEESATB
Or
>> A = 'IMDO';
>> B = 'ABCDE';
>> C = repmat(B,1,ceil(numel(A)/numel(B)));
>> C = reshape([A;C(1:numel(A))],1,[])
C = IAMBDCOD

카테고리

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