COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

• Email
• Follow

```I have two matrices:

h1=[1 -5; 5 4;5 8;9 8; 7 7;0 9]

h2=[4 6;3 8;4 6;2 7;7 7;6 7]

The number of columns in both h1 and h2 will variate, but the number of columns will always be the same for h1 and h2 and the number of rows will always be 6. I want to make a loop that does the following:

h=[h1(:,1) h2(:,1) h1(:,2) h2(:,2)]

h =

1     4    -5     6
5     3     4     8
5     4     8     6
9     2     8     7
7     7     7     7
0     6     9     7

Best Regards

Hans
```
 0

See related articles to this posting

```"Hans " <jyde_6@msn.com> wrote in message <hh7iov\$dik\$1@fred.mathworks.com>...
> I have two matrices:
>
>  h1=[1 -5; 5 4;5 8;9 8; 7 7;0 9]
>
> h2=[4 6;3 8;4 6;2 7;7 7;6 7]
>
> The number of columns in both h1 and h2 will variate, but the number of columns will always be the same for h1 and h2 and the number of rows will always be 6. I want to make a loop that does the following:
>
> h=[h1(:,1) h2(:,1) h1(:,2) h2(:,2)]
>
> h =
>
>      1     4    -5     6
>      5     3     4     8
>      5     4     8     6
>      9     2     8     7
>      7     7     7     7
>      0     6     9     7

Why a loop? Learn to think in matlab without
loops. Assume that n is the number of columns.

reshape(permute(reshape([h1,h2],[6,n,2]),[1 3 2]),6,[]);

If you don't see what the above code fragment
does, take it apart. Look at each step separately,
and understand how matlab stores the result in
memory. This will offer great dividends in your
future use of matlab.

John
```
 0

```"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hh7juf\$qm3\$1@fred.mathworks.com>...
> "Hans " <jyde_6@msn.com> wrote in message <hh7iov\$dik\$1@fred.mathworks.com>...
> > I have two matrices:
> >
> >  h1=[1 -5; 5 4;5 8;9 8; 7 7;0 9]
> >
> > h2=[4 6;3 8;4 6;2 7;7 7;6 7]
> >
> > The number of columns in both h1 and h2 will variate, but the number of columns will always be the same for h1 and h2 and the number of rows will always be 6. I want to make a loop that does the following:
> >
> > h=[h1(:,1) h2(:,1) h1(:,2) h2(:,2)]
> >
> > h =
> >
> >      1     4    -5     6
> >      5     3     4     8
> >      5     4     8     6
> >      9     2     8     7
> >      7     7     7     7
> >      0     6     9     7
>
> Why a loop? Learn to think in matlab without
> loops. Assume that n is the number of columns.
>
> reshape(permute(reshape([h1,h2],[6,n,2]),[1 3 2]),6,[]);
>
> If you don't see what the above code fragment
> does, take it apart. Look at each step separately,
> and understand how matlab stores the result in
> memory. This will offer great dividends in your
> future use of matlab.
>
> John

Seems like this also works:
h=[h1(:,1),h2(:,1),h1(:,2),h2(:,2)]
Regards Matt
```
 0

```"Matt Fetterman" <mattinjersey@yahoo.com> wrote in message <hh8797\$dt1\$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <hh7juf\$qm3\$1@fred.mathworks.com>...
> > "Hans " <jyde_6@msn.com> wrote in message <hh7iov\$dik\$1@fred.mathworks.com>...
> > > I have two matrices:
> > >
> > >  h1=[1 -5; 5 4;5 8;9 8; 7 7;0 9]
> > >
> > > h2=[4 6;3 8;4 6;2 7;7 7;6 7]
> > >
> > > The number of columns in both h1 and h2 will variate, but the number of columns will always be the same for h1 and h2 and the number of rows will always be 6. I want to make a loop that does the following:
> > >
> > > h=[h1(:,1) h2(:,1) h1(:,2) h2(:,2)]
> > >
> > > h =
> > >
> > >      1     4    -5     6
> > >      5     3     4     8
> > >      5     4     8     6
> > >      9     2     8     7
> > >      7     7     7     7
> > >      0     6     9     7
> >
> > Why a loop? Learn to think in matlab without
> > loops. Assume that n is the number of columns.
> >
> > reshape(permute(reshape([h1,h2],[6,n,2]),[1 3 2]),6,[]);
> >
> > If you don't see what the above code fragment
> > does, take it apart. Look at each step separately,
> > and understand how matlab stores the result in
> > memory. This will offer great dividends in your
> > future use of matlab.
> >
> > John
>
>
> Seems like this also works:
> h=[h1(:,1),h2(:,1),h1(:,2),h2(:,2)]
> Regards Matt

Thank very much Matt and John.

Best Regards

Hans
```
 0