How do you create a string from two strings by intersecting them char by char to form a new word using a loop?

1 回表示 (過去 30 日間)
str1 = 'Hello'
str2 = 'Canada'
strNew = 'CHaenlaldoa' %This is how the new word should look like.
Thank you in advance!
  4 件のコメント
Kevin Chng
Kevin Chng 2018 年 10 月 27 日
for i = [1:numel(str2)]
try to look at the link below:
Do you notice any problem with your for-loop?
Alfred Ofosu
Alfred Ofosu 2018 年 10 月 27 日
It's the iteration. it ends at 5, ever the dimension of str1 is 1x4, which is shorter than str2.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2018 年 10 月 27 日
編集済み: Stephen23 2018 年 10 月 27 日
Loop not required, just use indexing:
Method one:
>> str1 = 'Hello';
>> str2 = 'Canada';
>> strN = [str1,str2];
>> strN([2:2:end,1:2:end]) = strN
strN = CHaenlaldoa
Method two:
>> strN = str2([1,1],:);
>> strN(1,2:end) = str1;
>> strN = strN(2:end)
strN = CHaenlaldoa
  1 件のコメント
Alfred Ofosu
Alfred Ofosu 2018 年 10 月 27 日
Thanks, Stephen. I may be able to build a loop from yours. Awesome. Its an assignment from school, hence need to be in a loop.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by