MATLAB: Fill a zeros matrix with another matrix until it is full

arrayfillproblemzero

I want to fill the array essai with the value in the array key but my code return zeros
k= 1:length(key);
yr=reshape(y.',1,[]);
essai=zeros(1,length(yr));
essai=uint8(essai);
for n= 1:length(essai)
if k <length(key)
essai(n)=essai(n)+key(k)
else if k== length(key)
essai(n)=essai(n)+key(k);
k=1;
end
end
end

Best Answer

MATLAB is not an ugly low-level language like C++ and does not need loops to solve all tasks:
idx = 1+mod(0:numel(y)-1,numel(key));
essai = uint8(key(idx));
And tested on some random data:
>> y = 0:9;
>> key = 2:2:8;
>> idx = 1+mod(0:numel(y)-1,numel(key));
>> essai = uint8(key(idx))
essai =
2 4 6 8 2 4 6 8 2 4