copy 1st element of an array to 20 first indexes of second array in matlab and then do it for rest of the indexes of 1st array in matlab

Assuming you have two arrays named array1 and array2, you can copy the first element of array1 to the first 20 indexes of array2 using the following code:

main.m
array2(1:20) = array1(1);
26 chars
2 lines

To do this for the rest of the elements of array1, you can use a for loop:

main.m
for i = 2:length(array1)
    start_index = (i-1) * 20 + 1;
    end_index = i * 20;
    array2(start_index:end_index) = array1(i);
end
134 chars
6 lines

This will loop through each element of array1 (starting from the second element), and copy it 20 times to the next 20 indexes of array2.

Note that if the length of array1 is not evenly divisible by 20, the last few indexes of array2 may not be filled with a copied value.

related categories

gistlibby LogSnag