Assign same number of elements to the column data based on number of elements (each row) of the other column

41 Views Asked by At

I have two column data, where first column contains "Number of Elements" and the second column contains "Stages" data. I want to assign the same number of data elements of "Stages" data to the new column ("Required") column based on the "Number of Elements" (first column) data.

enter image description here

Sample data:

% Random data
data = ceil(10*rand(15,1));
data(:,2) = round(3*rand(15,1));
data =

     2     0
    10     3
     1     2
     8     2
     9     0
     9     3
     1     2
     4     1
     3     2
     9     1
     5     0
    10     1
     2     0
     3     1
     2     1

I'd like to create a cell array containing an array of No of Elements times the value in Stages. How can I create this cell array?

1

There are 1 best solutions below

0
Adriaan On

Wrap a for loop around repmat to populate your cell array

m = 15;  % your number of rows

% Random data
data = ceil(10*rand(m,1));
data(:,2) = round(3*rand(m,1));

% Initialise output
out = cell(m,1);
for ii = 1:m
    % store the required number of elements
    out{ii} = repmat(data(ii,2),data(ii,2),1)
end