To concatinate multiple like matrices in MATLAB

In summary, MATLAB does not seem to support concatenation of multiple matrices in a row-wise manner. There is a function called "horzcat" which can be used to accomplish the task, but it is equivalent to using a loop. The problem is that if the matrices do not have any common elements, the loop will not work.
  • #1
therash09
12
0
I have a problem regarding concatination of multiple matrices in MATLAB. For finite number of matrices there exists a command called cat or we may even put the matrices directly in a matrix representation format to get the desired concatenation.

Like, for A and B to form a matrix C in row-wise concatenation, we may simply write it as C=[A B].

However it becomes extremely tedious when the number of matrices grows. Say I have 100 matrices of n*n dimensions that are to be concatinated in MATLAB. For this I need to design a loop using which this may be carried out in one command.

I have not been able to successfully implement this part. Even the slightest of hints will be appreciated. Please help...
 
Physics news on Phys.org
  • #2
You can also use a function called "D = horzcat(A,B,C)" for that purpose, though it should be equivalent to "D = [A B C]".

I did not exactly understand what you mean by tedious. Could you be more specific?
 
  • #3
Togli said:
You can also use a function called "D = horzcat(A,B,C)" for that purpose, though it should be equivalent to "D = [A B C]".

I did not exactly understand what you mean by tedious. Could you be more specific?

Thanks for considering my problem, Togli. My problem is regarding multiple matrices. If I have 100 different matrices, how will I go about concatenating them? Obviously writing them the way I know and the way you've suggested would be an impossible task.

I was thinking of running some loop such that every matrix could be approached using a variable which is incremented from 1 to 100. But I am unable to implement this.
 
  • #4
I guess that depends whether your matrices have anything in common: What are their names for example? There should be a relation between them for that.
 
  • #5
Togli said:
I guess that depends whether your matrices have anything in common: What are their names for example? There should be a relation between them for that.

Well, that is obvious. Without having anything common among themselves, they'd not fit into a loop.

So I keep like names. Now this is where I get stuck. How do I proceed from here? What way should I assign the matrices names and how should I access them?

Using arrays like A(5,4,1) is no solution since it would point to the element of the 3D matrix A. How to add the parameter to the name and access it is the overall problem.


I am also thinking of it in a two step method involving 2D->3D packing followed by 3D->2D concatenation. However no significant headway could be made here too.
 
  • #6
You can either keep the matrices in cell arrays : A{1}, A{2}, A{3}...

OR you can keep'em in multidimensional matrices

A(1,:,:), A(2,:,:)

and in the latter case, you got to use command "squeeze" to convert them into 2 dimensional, i.e.,

B = squeeze(A(1,:,:))
 
  • #7
Togli said:
You can either keep the matrices in cell arrays : A{1}, A{2}, A{3}...

OR you can keep'em in multidimensional matrices

A(1,:,:), A(2,:,:)

and in the latter case, you got to use command "squeeze" to convert them into 2 dimensional, i.e.,

B = squeeze(A(1,:,:))

Thanks a lot mate! The A{1} part was the one that was searching for. Thankyou so much! I've done the task I wanted to successfully.
I had tried this syntax myself but then my setup showed error in syntax. I don't understand why. Possibly restarting MATLAB might have solved it then.

I was stuck at the representation of multiple matrices using the same variable name, and this is exactly what you've addressed in your post.

Thanks once again! :approve:
 
  • #8
Sure man, I am happy if I was any help. Have a good one.
 

Related to To concatinate multiple like matrices in MATLAB

1. What is the syntax for concatenating multiple matrices in MATLAB?

The syntax for concatenating multiple matrices in MATLAB is A = [M1, M2, M3, ...], where A is the output concatenated matrix and M1, M2, M3, ... are the matrices to be concatenated.

2. Can matrices with different dimensions be concatenated in MATLAB?

No, matrices with different dimensions cannot be concatenated in MATLAB. The matrices must have the same number of rows and columns in order to be concatenated.

3. How do I concatenate matrices along a specific dimension in MATLAB?

You can use the cat function in MATLAB to concatenate matrices along a specific dimension. The syntax is A = cat(dim, M1, M2, ...), where dim is the dimension along which the matrices will be concatenated and M1, M2, ... are the matrices to be concatenated.

4. Can I concatenate more than two matrices at a time in MATLAB?

Yes, you can concatenate more than two matrices at a time in MATLAB. Simply include all the matrices you want to concatenate within the square brackets, separated by commas.

5. How can I check if two matrices can be concatenated in MATLAB?

You can use the size function in MATLAB to check if two matrices can be concatenated. If the number of rows and columns of both matrices are the same, then they can be concatenated.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • Quantum Physics
Replies
1
Views
517
  • Precalculus Mathematics Homework Help
Replies
25
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
6K
Back
Top