Generate vectors from single values in Matlab

arrays, matlab

Solution

You could use `mat2cell` to accomplish this. We first create an array from `1` to `sum(c)` and then use `mat2cell` to group the array into pieces where each piece is the size of each element of `c`.

out = mat2cell(1:sum(c), 1, c);

This reduces the need for intermediate variables and gives you your cell array directly.

out{1} =

     1     2

out{2} =

     3     4     5     6     7

out{3} =

     8     9    10

Problem

I have vector c: ``` c = [2 5 3]; ``` I want to generate vectors with their lengths equal to each value in `c` in a consecutive order. So, I should obtain 3 vectors: ``` c1 = [1 2]; c2 = [3 4 5 6 7]; c3 = [8 9 10]; ``` Next, I want to align these vectors in a 1x3 cell array: ``` out = {c1 c2 c3}; ``` This may seem straightforward, but I can't figure how to do it automatically. Any ideas?

Original source