MATLAB: How to store vectors from each for loop in a matrix

for looploopmatrixstorevector

Hi I have a for loop. The result of every iteration is a rowvector. I want all rowvectors of the for loop stored as one matrix. Each row is the result of one iteration.
Simplified example:
if true
X= 1:0.1:2;
for i = 1:length(X)
A=4
B= 1:5
Y= A.*X(i)+B
end
end
The end result shoud be an 11 x 5 matrix. Indexing will give errors, so will starting with an empty array.

Best Answer

if true
X= 1:0.1:2;
A = 4;
B = 1:5;
Y = nan(length(X),length(B));
for i = 1:length(X)
Y(i,:) = A.*X(i)+B;
end
end