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

1 visualización (últimos 30 días)
str1 = 'Hello'
str2 = 'Canada'
strNew = 'CHaenlaldoa' %This is how the new word should look like.
Thank you in advance!
  4 comentarios
Alfred Ofosu
Alfred Ofosu el 27 de Oct. de 2018
It's the iteration. it ends at 5, ever the dimension of str1 is 1x4, which is shorter than str2.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 27 de Oct. de 2018
Editada: Stephen23 el 27 de Oct. de 2018
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 comentario
Alfred Ofosu
Alfred Ofosu el 27 de Oct. de 2018
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by