MATLAB: Create vector from each element it had

functionmatlab functionvectorvectors

Hi
Lets say I have a vector that goes: [x y z]
Im trying to create, if possible without a loop, the following vec: [1:x, 1:y, 1:z]
only I don't know who x y or z will ne, or how many elemnts will the first vec have

Best Answer

Method one: nonzeros:
vec = [x,y,z];
tmp = 1:max(vec);
new = nonzeros((tmp(:)<=vec).*tmp(:)).' % requires >=R2016b
Method two: arrayfun:
new = cell2mat(arrayfun(@(n)1:n,vec,'uni',0))