Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Insert row in an array and delete last row
Replies: 7   Last Post: Jan 21, 2013 2:38 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
dpb

Posts: 7,873
Registered: 6/7/07
Re: Insert row in an array and delete last row
Posted: Jan 20, 2013 10:45 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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...

--





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.