Path: news.mathworks.com!newsfeed-00.mathworks.com!kanaga.switch.ch!switch.ch!feeder.news-service.com!feeder.news-service.com!94.75.214.39.MISMATCH!aioe.org!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: attaching rows with 2 matrices
Date: Sat, 16 Oct 2010 21:23:52 -0700
Organization: Aioe.org NNTP Server
Lines: 59
Message-ID: <i9dtoq$be9$1@speranza.aioe.org>
References: <i9dse0$r0d$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: tUYQ4Ty9mMw9Pdc8TJRFQA.user.speranza.aioe.org
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Complaints-To: abuse@aioe.org
X-Notice: Filtered by postfilter v. 0.8.2
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.9) Gecko/20100915 Thunderbird/3.1.4
Xref: news.mathworks.com comp.soft-sys.matlab:679064

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