Got Questions? Get Answers.
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:
attaching rows with 2 matrices

Subject: attaching rows with 2 matrices

From: ravi

Date: 17 Oct, 2010 04:01:04

Message: 1 of 3

Dear Matlab group,

I have matrix T :

T =
              1 5
              4 7
              6 3

I also have another one , 'U', defined as:

n =3
U = (1: n)'
     

I'd like to know how I can attach each row of U to each individual row in T ,which should be :

V =
                  1 1 5
                  2 1 5
                  3 1 5
                  1 4 7
                  2 4 7
                  3 4 7
                  1 6 3
                  2 6 3
                  3 6 3

need some help if possible my friends.

Subject: attaching rows with 2 matrices

From: Nasser M. Abbasi

Date: 17 Oct, 2010 04:23:52

Message: 2 of 3

On 10/16/2010 9:01 PM, ravi wrote:
> Dear Matlab group,
>
> I have matrix T :
>
> T =
> 1 5
> 4 7
> 6 3
>
> I also have another one , 'U', defined as:
>
> n =3
> U = (1: n)'
>
>
> I'd like to know how I can attach each row of U to each individual row in T ,which should be :
>
> V =
> 1 1 5
> 2 1 5
> 3 1 5
> 1 4 7
> 2 4 7
> 3 4 7
> 1 6 3
> 2 6 3
> 3 6 3
>
> need some help if possible my friends.


may be

T = [1 5;
      4 7;
      6 3
     ];
n =3;
U = (1: n)';

cell2mat(arrayfun(@(i) [U repmat(T(i,:),n,1)],1:size(T,1),...
          'UniformOutput',false)')

ans =

      1 1 5
      2 1 5
      3 1 5
      1 4 7
      2 4 7
      3 4 7
      1 6 3
      2 6 3
      3 6 3

--Nasser

Subject: attaching rows with 2 matrices

From: Roger Stafford

Date: 17 Oct, 2010 18:25:04

Message: 3 of 3

"ravi " <ravi_071@hotmail.com> wrote in message <i9dse0$r0d$1@fred.mathworks.com>...
> ........
> I'd like to know how I can attach each row of U to each individual row in T
> ........
- - - - - - - - - -
[p,q] = size(T);
V = [repmat(U,p,1),reshape(repmat(T(:).',n,1),[],q)];

Roger Stafford

Tags for this Thread

No tags are associated with 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