Thread Subject: horzcat

Subject: horzcat

From: Vanesa

Date: 15 Sep, 2009 07:05:04

Message: 1 of 5

Hi

I am trying to horizantally concatenate the rows on the same matrix, i tried this code
[z,e]=size(TT);

for p=1:z-1
    G(p,:)=horzcat(TT(1,:),TT(1+p,:));
end

but what this does is concatenate row 1 with row p, what i want to do is concatenate row 1 with row 2, then row 1 with row 2 with row 3 and so on.

how could i do that with a for loop or is there any other way to do it with a matlab function?

Subject: horzcat

From: Darren Rowland

Date: 15 Sep, 2009 07:24:02

Message: 2 of 5


To concatenate the first p rows together horizontally try this
[z,e]=size(TT);
row2p = reshape(TT(1:p,:)',1,p*z);

Hth,
Darren

Subject: horzcat

From: James Tursa

Date: 15 Sep, 2009 07:32:03

Message: 3 of 5

"Vanesa " <vanesa2455@HOTMAIL.com> wrote in message <h8neb0$1s8$1@fred.mathworks.com>...
> Hi
>
> I am trying to horizantally concatenate the rows on the same matrix, i tried this code
> [z,e]=size(TT);
>
> for p=1:z-1
> G(p,:)=horzcat(TT(1,:),TT(1+p,:));
> end
>
> but what this does is concatenate row 1 with row p, what i want to do is concatenate row 1 with row 2, then row 1 with row 2 with row 3 and so on.
>
> how could i do that with a for loop or is there any other way to do it with a matlab function?

I *think* this is what you are asking for:

[z,e] = size(TT);
TTC = reshape(TT',[],1);
G = zeros(z*e,z);
for k=1:z
    G(1:k*e,k) = TTC(1:k*e);
end
G = G';

James Tursa

Subject: horzcat

From: Vanesa

Date: 15 Sep, 2009 07:46:02

Message: 4 of 5

"Vanesa " <vanesa2455@HOTMAIL.com> wrote in message <h8neb0$1s8$1@fred.mathworks.com>...
> Hi
>
> I am trying to horizantally concatenate the rows on the same matrix, i tried this code
> [z,e]=size(TT);
>
> for p=1:z-1
> G(p,:)=horzcat(TT(1,:),TT(1+p,:));
> end
>
> but what this does is concatenate row 1 with row p, what i want to do is concatenate row 1 with row 2, then row 1 with row 2 with row 3 and so on.
>
> how could i do that with a for loop or is there any other way to do it with a matlab function?


Thank you so much that worked wonderfully!

Subject: horzcat

From: Vanesa

Date: 15 Sep, 2009 07:47:03

Message: 5 of 5

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h8nftj$r4k$1@fred.mathworks.com>...
> "Vanesa " <vanesa2455@HOTMAIL.com> wrote in message <h8neb0$1s8$1@fred.mathworks.com>...
> > Hi
> >
> > I am trying to horizantally concatenate the rows on the same matrix, i tried this code
> > [z,e]=size(TT);
> >
> > for p=1:z-1
> > G(p,:)=horzcat(TT(1,:),TT(1+p,:));
> > end
> >
> > but what this does is concatenate row 1 with row p, what i want to do is concatenate row 1 with row 2, then row 1 with row 2 with row 3 and so on.
> >
> > how could i do that with a for loop or is there any other way to do it with a matlab function?
>
> I *think* this is what you are asking for:
>
> [z,e] = size(TT);
> TTC = reshape(TT',[],1);
> G = zeros(z*e,z);
> for k=1:z
> G(1:k*e,k) = TTC(1:k*e);
> end
> G = G';
>
> James Tursa

that worked pretty well too! thank you!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
code Darren Rowland 15 Sep, 2009 03:29:04
reshape Darren Rowland 15 Sep, 2009 03:29:04
horzcat Vanesa 15 Sep, 2009 03:09:02
rssFeed for this Thread

Contact us at files@mathworks.com