user1205030
user1205030

Reputation: 225

Matlab: How to combine different matrices?

For example, I have 6 similar Matrices (Matrix A1 to A6) but with different data.

Matrix A1:

2011/1/1 23.00 33.00 23.00 35.00 ...

2011/1/2 19.00 20.00 22.00 22.00 ... ...

I want to combine these 6 Matrices and create a new Matrix B.

The Matrix B will have: 6 columns and the length of the row equals (length(column of A)-1).

The first column of Matrix B is Matrix A1's first row (exclude date).

The second column of Matrix B is Matrix A2's first row (exclude date)...and so on...

I am looking for a script. Thanks!

Upvotes: 0

Views: 2335

Answers (1)

yuk
yuk

Reputation: 19870

B = [A1(1,2:end); A2(1,2:end); A3(1,2:end); A4(1,2:end); A5(1,2:end); A6(1,2:end)]';

or

B = [A1(1,2:end)' A2(1,2:end)' A3(1,2:end)' A4(1,2:end)' A5(1,2:end)' A6(1,2:end)'];

UPDATE:

Instead of having multiple matrices with different names consider using cell array where each cell contains a matrix. If A is a cell array,

A = {A1, A2, A3, A4, A5, A6};

then you can access k-th matrix as A{k}. In this case you may have different number of matrices in the cell array and get B in a vectorized manner using, for example, this code:

B = cell2mat(cellfun(@(x) x(1,2:end)', A, 'uniformoutput',0));

Upvotes: 2

Related Questions