Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Insert a row in a matrix

Subject: Insert a row in a matrix

From: Diego Zegarra

Date: 7 Mar, 2009 22:47:01

Message: 1 of 5

Hey guys,

What ways are there to insert a row at the beginning of a matrix,

Assume,

a=[
3 2 1;
2 4 5;
9 6 2];

b=[3 8 2];

Now I would want,

a=[
3 8 2;
3 2 1;
2 4 5;
9 6 2];

I know it can be done by, a= [b;a];

Is there a faster way?

Subject: Insert a row in a matrix

From: John D'Errico

Date: 7 Mar, 2009 22:59:02

Message: 2 of 5

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gouth5$k40$1@fred.mathworks.com>...
> Hey guys,
>
> What ways are there to insert a row at the beginning of a matrix,
>
> Assume,
>
> a=[
> 3 2 1;
> 2 4 5;
> 9 6 2];
>
> b=[3 8 2];
>
> Now I would want,
>
> a=[
> 3 8 2;
> 3 2 1;
> 2 4 5;
> 9 6 2];
>
> I know it can be done by, a= [b;a];
>
> Is there a faster way?

Faster? What was slow about that? It took all of
a half dozen characters to type. You want a 3
character solution? Do you mean in terms of
execution speed? A single concatenation will
take what, a millisecond?

Are you doing this extremely often, so that
it is expensive? If so, then learn to preallocate
arrays that will grow in size.

john

Subject: Insert a row in a matrix

From: Husam Aldahiyat

Date: 8 Mar, 2009 04:52:01

Message: 3 of 5

"Diego Zegarra" <diegozbb@gmail.com> wrote in message <gouth5$k40$1@fred.mathworks.com>...
> Hey guys,
>
> What ways are there to insert a row at the beginning of a matrix,
>
> Assume,
>
> a=[
> 3 2 1;
> 2 4 5;
> 9 6 2];
>
> b=[3 8 2];
>
> Now I would want,
>
> a=[
> 3 8 2;
> 3 2 1;
> 2 4 5;
> 9 6 2];
>
> I know it can be done by, a= [b;a];
>
> Is there a faster way?

mex it!

Subject: Insert a row in a matrix

From: Steven Lord

Date: 9 Mar, 2009 02:35:23

Message: 4 of 5


"Husam Aldahiyat" <numandina@gmail.com> wrote in message
news:govith$75r$1@fred.mathworks.com...
> "Diego Zegarra" <diegozbb@gmail.com> wrote in message
> <gouth5$k40$1@fred.mathworks.com>...
>> Hey guys,
>>
>> What ways are there to insert a row at the beginning of a matrix,

*snip*

> mex it!

The MEX-file will need to do the same type of memory manipulation that the
built-in code behind [b; a] does, so I'm not sure you'll get a lot of extra
performance for MEXing an "insert a new row at the beginning" function.

--
Steve Lord
slord@mathworks.com

Subject: Insert a row in a matrix

From: Husam Aldahiyat

Date: 9 Mar, 2009 05:22:01

Message: 5 of 5

"Steven Lord" <slord@mathworks.com> wrote in message <gp1v9b$nt2$1@fred.mathworks.com>...
>
> "Husam Aldahiyat" <numandina@gmail.com> wrote in message
> news:govith$75r$1@fred.mathworks.com...
> > "Diego Zegarra" <diegozbb@gmail.com> wrote in message
> > <gouth5$k40$1@fred.mathworks.com>...
> >> Hey guys,
> >>
> >> What ways are there to insert a row at the beginning of a matrix,
>
> *snip*
>
> > mex it!
>
> The MEX-file will need to do the same type of memory manipulation that the
> built-in code behind [b; a] does, so I'm not sure you'll get a lot of extra
> performance for MEXing an "insert a new row at the beginning" function.
>
> --
> Steve Lord
> slord@mathworks.com
>

It was a joke, sheesh.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us