フィルターのクリア

how to insert elements to get a new output

1 回表示 (過去 30 日間)
Elysi Cochin
Elysi Cochin 2017 年 10 月 4 日
回答済み: Andrei Bobrov 2017 年 10 月 4 日
i have a character variable with elements such as
cv = '000001010011100101111'
vect = [1 2 3];
Now i want to insert the vect elements to cv after every 3 elements. So that i get
new_cv = '0001001201030111100210131111'
for understandability i insert space after 3 elements
cv = '000 001 010 011 100 101 111'
new_cv = '000 1 001 2 010 3 011 1 100 2 101 3 111 1'
The elements of vect is repeated till the length of cv

採用された回答

Guillaume
Guillaume 2017 年 10 月 4 日
編集済み: Guillaume 2017 年 10 月 4 日
cv = '000001010011100101111'
vect = [1 2 3];
groupsize = 3;
assert(mod(numel(cv), groupsize) == 0, 'Input cannot be divided evenly into groups of %d elements', groupsize);
prefix = reshape(cv, groupsize, []); %reshape input into columns of groupsize elements
postfix = char(repmat(vect + '0', 1, ceil(size(prefix, 2)/groupsize))); %replicate vect to be at least as long as prefix, and convert to char
postfix = postfix(1:size(prefix, 2)); %truncate unneeded elements
result = reshape([prefix; postfix], 1, []) %concatenate and reshape into one row

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 10 月 4 日
cv = '000001010011100101111';
k = 3;
out = reshape([reshape(cv,k,[]);sprintf('%d',(rem(0:numel(cv)/k-1,k)+1)')],1,[]);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by