copy 8 element array to 160 elements with 20 duplicate values in matlab

You can use repmat function to replicate the elements of the array and then reshape the resulting array to the desired size.

Here's an example code:

main.m
% define original array with 8 elements
originalArray = [1 2 3 4 5 6 7 8];

% how many times to duplicate each element
numberOfDuplicates = 20;

% replicate the elements using repmat
replicatedArray = repmat(originalArray, numberOfDuplicates, 1);

% reshape the array to desired size
finalArray = reshape(replicatedArray', [], numberOfDuplicates)';
349 chars
12 lines

Explanation:

  • repmat(originalArray, numberOfDuplicates, 1) replicates the originalArray numberOfDuplicates times along the rows.
  • ' is the transpose operator.
  • reshape(replicatedArray', [], numberOfDuplicates)' reshapes the replicated array to have 160 elements, with 20 duplicates of each element in the original array.

Note that the resulting finalArray will be a 160-by-1 array. If you want it to be a row or column vector instead, you can use the transpose or reshape function.

related categories

gistlibby LogSnag