MATLAB: Insert rows in a matrix

matrixrows

Hi, I have the following problem: matrix
AA=[NaN 1 2 3 4 5
NaN 10 20 30 40 50
...............
NaN 1E8 2E8 3E8 4E8 5E8 ];
There is possible to insert every 10 row the following row:
[NaN NaN 99 Nan 77 NaN];
without a loop?
Thank you

Best Answer

One way of doing this:
%work out how to split AA
rowdist = ones(1, ceil(size(AA, 1) / 10)) * 10;
rowdist(end) = 10 + mod(size(AA, 1), -10);
%do the splitting and transpose into a row
splitAA = mat2cell(AA, rowdist, size(AA, 2))';
%add another row with the data to insert
splitAA(2, :) = {[NaN NaN 99 NaN 77 NaN]};
%avoid insertion of the new row at the end if the height
%of the matrix is not a multiple of ten
if mod(size(AA, 1), 10)
splitAA{2, end} = [];
end
%join it all together
newAA = vertcat(splitAA{:})