In each iteration, I am inserting one row into a matrix whose row number is predetermined. My current implementation is
seqToBePutIntoMatrix = [1 2 3 4 5 6 7 8 9];
col_no = 3; % hence the matrix is 3x3
myMatrix = [];
for i = 1:col_no:length(seqToBePutIntoMatrix)
myMatrix = [myMatrix; seqToBePutIntoMatrix(i:i+col_no-1)];
end
MATLAB is suggesting me that
The variable
myMatrix
appears to change size in every loop iteration. Consider preallocation for speed.
I am seriously considering its advice, and wish to preallocate such a 3-row empty matrix and insert one row in each iteration. I have tried this:
seqToBePutIntoMatrix = [1 2 3 4 5 6 7 8 9];
col_no = 3; % hence the matrix is 3x3
myMatrix = [];
j = 1;
for i = 1:col_no:length(seqToBePutIntoMatrix)
myMatrix(j) = seqToBePutIntoMatrix(i:i+col_no-1);
j = j+1;
end
However, this is not working. How may I make it work?