dpb
Posts:
6,693
Registered:
6/7/07
|
|
Re: Insert row in an array and delete last row
Posted:
Jan 20, 2013 10:45 AM
|
|
On 1/20/2013 8:28 AM, Michael Doukas wrote: > Hello everybody, > > I have a script that performs some actions and generates a row vector > [1x13]. > In a for loop i want to insert this generated row vector as the first > row in a matrix L. > L has a preallocated size [150x13]. But i do not want to replace the > pre-existing first row. The new row vector must be inserted before the > existing first row. > > So in that way all rows will be moved downwards one row. > > Finally, once this is done, a want to remove the last row, in order to > maintain the size of the matrix L.
That's likely going to be highly costly in terms of temporaries if you're doing this often to do that, specifically. Instead of moving L, just create N as
N=[R; L(1:end-1,:)]; clear L
In place you can try
L=circshift(L,1); L(1,:)=R;
or permutations of the above idea but I suspect they'll be slower than the previous although you can test it out...
--
|
|