View Full Version : To concatinate multiple like matrices in MATLAB
therash09
Apr21-11, 09:33 AM
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...
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?
therash09
Apr21-11, 01:35 PM
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.
therash09
Apr21-11, 03:57 PM
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 gotta use command "squeeze" to convert them into 2 dimensional, i.e.,
B = squeeze(A(1,:,:))
therash09
Apr23-11, 05:52 AM
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 gotta 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.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.