Repeat the value of a variable to match the length of another variable

1 回表示 (過去 30 日間)
James Knight
James Knight 2019 年 11 月 7 日
回答済み: Joshua Taylor 2019 年 11 月 7 日
Hi
I have two variables
X = TESTING
Y = WORLD
I want to repeat Y until its the same length as X EG
X = TESTING
Y = WORLDWO
Thanks in advance

回答 (2 件)

Joshua Taylor
Joshua Taylor 2019 年 11 月 7 日
So, when you say X = TESTING, I assume you mean a variable X = 'testing'
One thing you could do is copy the variable and then truncate it to the desired size you want.
From your example:
X = 'testing';
Y = 'world';
y1 = repmat(Y,1,2);
y1 = y1(1:length(X))
Do you know anything about X or Y? Like how long they'll be, at least relative to each other?

Stephen23
Stephen23 2019 年 11 月 7 日
>> X = 'TESTING';
>> Y = 'WORLD';
>> Z = Y(1+mod(0:numel(X)-1,numel(Y)))
Z = WORLDWO

カテゴリ

Help Center および File ExchangeTesting Frameworks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by