MATLAB To concatinate multiple like matrices in MATLAB

AI Thread Summary
Concatenating multiple matrices in MATLAB can be challenging, especially when dealing with a large number of them, such as 100 matrices of n*n dimensions. The user initially struggled with the tediousness of manually concatenating each matrix and sought a method to automate the process using loops. Suggestions included utilizing cell arrays or multidimensional matrices to store the matrices, allowing for easier access and manipulation. The use of the "squeeze" function was also mentioned to convert 3D matrices into 2D for concatenation. Ultimately, the user successfully implemented the solution by using cell arrays, which resolved their issue with matrix representation.
therash09
Messages
12
Reaction score
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
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?
 
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.
 
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.
 
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.
 
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,:,:))
 
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:
 
Sure man, I am happy if I was any help. Have a good one.
 

Similar threads

Back
Top