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:
To compile several matrices in a matrix

Subject: To compile several matrices in a matrix

From: Sheri

Date: 5 Mar, 2012 22:10:13

Message: 1 of 9

There are 300 matrices with same size (30,129). I need to compile all these matrices in a matrix (9000,129). How can I do that?

Thanks,

Sheri

Subject: To compile several matrices in a matrix

From: Nasser M. Abbasi

Date: 5 Mar, 2012 22:40:38

Message: 2 of 9

On 3/5/2012 4:10 PM, Sheri wrote:
> There are 300 matrices with same size (30,129). I need to compile all
>these matrices in a matrix (9000,129). How can I do that?
>


try repmat

EDU>> a=[1 2;3 4]
      1 2
      3 4

EDU>> N=3;
EDU>> C=repmat(a,[N,1])
      1 2
      3 4
      1 2
      3 4
      1 2
      3 4

in your case N will be 9000/30 or 300 and 'a' will be your 30 by 120 matrix

--Nasser

Subject: To compile several matrices in a matrix

From: Sheri

Date: 5 Mar, 2012 22:55:13

Message: 3 of 9

The size of matrices are the same but, the content of matrices are not the same, for example:

A1=[1 2; 3 4];
A2=[7 9; 5 1];
.
.
A300=[8 6; 7 3];

Thanks,

Sheri

Subject: To compile several matrices in a matrix

From: Nasser M. Abbasi

Date: 5 Mar, 2012 23:10:13

Message: 4 of 9

On 3/5/2012 4:55 PM, Sheri wrote:
> The size of matrices are the same but, the content of matrices are not the same,
>for example:
>
> A1=[1 2; 3 4];
> A2=[7 9; 5 1];
> .
> .
> A300=[8 6; 7 3];

oh no, not another one of those zillion different variables problem :)

Why not put all your data in one matrix A? why A1,A2,....A5000 ?

it would make life so much easier for you. You can then
use the A matrix index to locate your different data segments and
manage them more easily.

I guess you have now to use the eval() command and muck
around with variable names to compose the expressions 'A1','A2' etc..

I'll let someone else help on this, I do not like this type of
programming as I think it is bad design.

You should rewrite your program so you do not have to many little
matrices around in the first place, and structure your data
better.

good luck!

--Nasser

Subject: To compile several matrices in a matrix

From: Sheri

Date: 6 Mar, 2012 13:46:12

Message: 5 of 9

Let me play with numbers which are more formal statements.

A1=rand (3,4);
A2=rand (3,4);
A3=rand (3,4);
.
.
A300=rand (3,4);

CompileA= [A1; A2; A3; .....A300];
in that the size CompileA is (900x4).

How could I make the CompileA?

Thanks,

Sheri

Subject: To compile several matrices in a matrix

From: Nasser M. Abbasi

Date: 6 Mar, 2012 14:23:12

Message: 6 of 9

On 3/6/2012 7:46 AM, Sheri wrote:
> Let me play with numbers which are more formal statements.
>
> A1=rand (3,4);
> A2=rand (3,4);
> A3=rand (3,4);
> .
> .
> A300=rand (3,4);
>
> CompileA= [A1; A2; A3; .....A300];
> in that the size CompileA is (900x4).
>
> How could I make the CompileA?
>
> Thanks,
>
> Sheri

Sheri;

as I mentioned before, you can use eval() to build up the name
of the matrices 'A1', 'A2', etc. in the fly and then build
your compileA in say a simple loop.

Here is an example:

--------------------
N=4,M=1;
A1=rand (N,M);
A2=rand (N,M);
A3=rand (N,M);

numOfLittleMatrices = 3; (*how many matrices to combine*)

A=zeros(numOfLittleMatrices*N,M);

for i=1:numOfLittleMatrices
     from=(i-1)*N+1;
     to=from+N-1;
     A(from:to,:)=eval(['A' num2str(i)])
end
----------------------

again, the above is bad way to write software and I would
not recommend it. You could design your program so that,
from the start, all your A_i matrices are build into the
large A matrix.

--Nasser

Subject: To compile several matrices in a matrix

From: someone

Date: 6 Mar, 2012 16:11:15

Message: 7 of 9

"Sheri " <sghaumipour@gmail.com> wrote in message <jj54f4$ekg$1@newscl01ah.mathworks.com>...
> Let me play with numbers which are more formal statements.
>
> A1=rand (3,4);
> A2=rand (3,4);
> A3=rand (3,4);
> .
> .
> A300=rand (3,4);
>
> CompileA= [A1; A2; A3; .....A300];
> in that the size CompileA is (900x4).
>
> How could I make the CompileA?
>
> Thanks,
>
> Sheri

As Nasser said, this is a VERY BAD programming technique in MATLAB.
If it is not too late, use cell arrays instead. For example:

A{1}=rand (3,4);
A{2}=rand (3,4);
A{3}=rand (3,4);
.
.
A{300}=rand (3,4);

% Note the the use of the curley brackets above.
% Now all you need to do is:

CompileA= cell2mat(A);

% or simply use A as is.

Subject: To compile several matrices in a matrix

From: Sheri

Date: 6 Mar, 2012 17:19:12

Message: 8 of 9

OK, but how could I write a loop to take all A1, A2, A3,...A300 to create a cell called "A", which is A{1}, A{2}, A{3},...A{300}?

Thanks,

Sheri

Subject: To compile several matrices in a matrix

From: someone

Date: 6 Mar, 2012 19:08:14

Message: 9 of 9

"Sheri " <sghaumipour@gmail.com> wrote in message <jj5gug$g0$1@newscl01ah.mathworks.com>...
> OK, but how could I write a loop to take all A1, A2, A3,...A300 to create a cell called "A", which is A{1}, A{2}, A{3},...A{300}?
>
> Thanks,
>
> Sheri

% You will have to do something like this:

for idx = 1:300
   eval(['A{' num2str(idx) '} = A' num2str(idx) ';'])
end

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