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:
"Expanding matrices"

Subject: "Expanding matrices"

From: Jono

Date: 8 Apr, 2013 16:53:09

Message: 1 of 4

There may be some very simple function for this I'm unaware of but I'm trying to multiply out matrices as if they were brackets whilst retaining all the terms.

For example

Phase1=[A ;
             B]
Phase2=[C , D ;
             E, F]
Phase3=[ G;
              H;
              I]

I'm trying to multiply each of these out so the end result would be
Phases=[A , C, D, G;
             A , C, D, H;
             A , C, D, I;
             A , E, F, G;
             A , E, F, H;
             A , E, F, I;
             B , C, D, G;
             B , C, D, H;
             B , C, D, I;
             B , E, F, G;
             B , E, F, H;
             B , E, F, I;]

However, there will be an unknown number of 'phases' (matrices) and these will be an unknown number of rows and columns.

Firstly I'm asking is there a simple function to combine two matrices together row by row as I'm sure I can work from there. If not does anyone have any recommendations about how to go about this as my initial thought was it needs an unknown number of 'for' loops. Also some matrices may be getting on for 10^6 in length so any hints on speeding up the code would be appreciated.

If I haven't explained this clearly please let me know!

Thanks,

Jon

Subject: "Expanding matrices"

From: Jennifer

Date: 8 Apr, 2013 17:31:09

Message: 2 of 4

Have you tried bsxfun?

"Jono" wrote in message <kjusll$kgc$1@newscl01ah.mathworks.com>...
> There may be some very simple function for this I'm unaware of but I'm trying to multiply out matrices as if they were brackets whilst retaining all the terms.
>
> For example
>
> Phase1=[A ;
> B]
> Phase2=[C , D ;
> E, F]
> Phase3=[ G;
> H;
> I]
>
> I'm trying to multiply each of these out so the end result would be
> Phases=[A , C, D, G;
> A , C, D, H;
> A , C, D, I;
> A , E, F, G;
> A , E, F, H;
> A , E, F, I;
> B , C, D, G;
> B , C, D, H;
> B , C, D, I;
> B , E, F, G;
> B , E, F, H;
> B , E, F, I;]
>
> However, there will be an unknown number of 'phases' (matrices) and these will be an unknown number of rows and columns.
>
> Firstly I'm asking is there a simple function to combine two matrices together row by row as I'm sure I can work from there. If not does anyone have any recommendations about how to go about this as my initial thought was it needs an unknown number of 'for' loops. Also some matrices may be getting on for 10^6 in length so any hints on speeding up the code would be appreciated.
>
> If I haven't explained this clearly please let me know!
>
> Thanks,
>
> Jon

Subject: "Expanding matrices"

From: Bruno Luong

Date: 8 Apr, 2013 17:52:09

Message: 3 of 4

Phase1 = [1; 2];
Phase2 = [3 5;
          4 6];
Phase3 = [7;
          8;
          9];
      
PhaseIn = { Phase1 Phase2 Phase3 }; % add more arrays if you like

% Engine
n = cellfun(@(x) 1:size(x,1), PhaseIn, 'Unif', 0);
[n{:}] = ndgrid( n{end:-1:1});
m = length(n);
n = cat(m+1,n{end:-1:1});
n = reshape(n,[],m);
Phase = arrayfun(@(j) PhaseIn{j}(n(:,j),:), 1:m, 'Unif', 0);
Phase = cat(2, Phase{:});
disp(Phase)

% Bruno

Subject: "Expanding matrices"

From: Jono

Date: 9 Apr, 2013 10:41:05

Message: 4 of 4

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kjv049$33s$1@newscl01ah.mathworks.com>...
> Phase1 = [1; 2];
> Phase2 = [3 5;
> 4 6];
> Phase3 = [7;
> 8;
> 9];
>
> PhaseIn = { Phase1 Phase2 Phase3 }; % add more arrays if you like
>
> % Engine
> n = cellfun(@(x) 1:size(x,1), PhaseIn, 'Unif', 0);
> [n{:}] = ndgrid( n{end:-1:1});
> m = length(n);
> n = cat(m+1,n{end:-1:1});
> n = reshape(n,[],m);
> Phase = arrayfun(@(j) PhaseIn{j}(n(:,j),:), 1:m, 'Unif', 0);
> Phase = cat(2, Phase{:});
> disp(Phase)
>
> % Bruno

Bruno,

Thanks very much for the help, with a bit of jigging around to conform to my problem this method worked perfectly!

Thank you again,

Jon

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