copy 1st element of an array to 20 first indexes of second array in matlab

You can do this using simple indexing in Matlab. Here's how:

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

This will copy the first element of array1 into the first 20 elements of array2.

Note that if array2 is not already initialized, you will need to create it first:

main.m
array1 = [1 2 3 4 5];  % Example arrays
array2 = zeros(1, 20); % Initialize array2 with 20 elements, all set to 0

array2(1:20) = array1(1);
141 chars
5 lines

This will copy the value 1 from array1(1) into the first 20 elements of array2, resulting in array2 being:

main.m
array2 = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
52 chars
2 lines

related categories

gistlibby LogSnag