To concatinate multiple like matrices in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around the problem of concatenating multiple matrices in MATLAB, particularly when dealing with a large number of matrices (e.g., 100 matrices of n*n dimensions). Participants explore various methods for achieving this concatenation efficiently, including the use of loops and specific MATLAB functions.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant describes the challenge of concatenating a large number of matrices and suggests using a loop to automate the process.
  • Another participant mentions the function "D = horzcat(A,B,C)" as an alternative for concatenation but seeks clarification on what is meant by "tedious."
  • There is a discussion about the necessity of having a common naming convention for the matrices to facilitate their concatenation.
  • One participant proposes using cell arrays (e.g., A{1}, A{2}, A{3}) or multidimensional matrices (e.g., A(1,:,:)) as potential solutions for managing multiple matrices.
  • Another participant expresses gratitude for the suggestion of using cell arrays, indicating it helped them successfully complete their task.
  • Concerns are raised about syntax errors encountered when trying to implement these solutions, with a suggestion that restarting MATLAB might resolve such issues.

Areas of Agreement / Disagreement

Participants generally agree on the need for a systematic approach to concatenate multiple matrices, but there are differing views on the best methods to achieve this, and no consensus is reached on a single solution.

Contextual Notes

Participants mention potential issues with syntax errors and the need for a common naming convention, but these points remain unresolved and are dependent on individual setups and definitions.

Who May Find This Useful

This discussion may be useful for MATLAB users facing challenges with matrix concatenation, particularly in scenarios involving multiple matrices and the need for efficient coding practices.

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

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
43K
Replies
11
Views
6K