MATLAB: Concatenation of row vectors in a for loop

concatenatefor looprow vector

I have a for loop that calculates a line vector at each iteration. What I want as a final result is a single line vector with the result of each of the iterations one next to the other.
This is the code that I have right now and it gives me the expected result, but in this case my vector r changes size on every iteration and I want to avoid that (I know I have to preallocate values to my vector but I don't know how to implant it in my for loop) :
X = [40 24 36 12 8];
dr =[10 6 9 3 2];
r=[];
startVal= Rint;
for i=1:length(X)
stepSize= dr(i);
endVal= startVal+X(i);
r=[r startVal:stepSize:endVal];
startVal= endVal;
end
r=unique(r);
Thank you

Best Answer

I would just use a cell array,
r=cell(1,length(X));
startVal= Rint;
for i=1:length(X)
stepSize= dr(i);
endVal= startVal+X(i);
r{i}=startVal:stepSize:endVal;
startVal= endVal;
end
r=unique(cell2mat(r));