Home > Mobile >  MATLAB - repmat values into cell array where individual cell elements have unequal size
MATLAB - repmat values into cell array where individual cell elements have unequal size

Time:10-29

I am trying to repeat values from an array (values) to a cell array where the individual elements have unequal sizes (specified by array_height and array_length).

I hope to apply this to a larger data set (containing ~100 x ~100 values) and my current solution is to have a line of code for each value (code example below). Surely there is a better way... Please could someone offer an alternative solution?

C = cell(3,2);

values = rand(3,2);
array_height = randi(10,3,2);
array_length = randi(10,3,2);

C{1,1} = repmat((values(1,1)),[array_height(1,1),array_length(1,1)]);
C{2,1} = repmat((values(2,1)),[array_height(2,1),array_length(2,1)]);
C{3,1} = repmat((values(3,1)),[array_height(3,1),array_length(3,1)]);
C{1,2} = repmat((values(1,2)),[array_height(1,2),array_length(1,2)]);
C{2,2} = repmat((values(2,2)),[array_height(2,2),array_length(2,2)]);
C{3,2} = repmat((values(3,2)),[array_height(3,2),array_length(3,2)]);

CodePudding user response:

If you did this in a for loop, it might look something like this:

for i = 1:size(C,1)
    for j = 1:size(C,2)
         C{i,j} = repmat(values(i,j),[array_height(i,j),array_length(i,j)]);
    end
end

However, if you are trying to generate or use this with a larger dataset, this code snippet likely will take forever! I suspect whatever your overall objective is can be better served by matlab's many optimizations for matrices and vectors, but without more information I can't help more than that.

  • Related