How to Loop Through Multiple Matrices in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter TimFys
  • Start date Start date
  • Tags Tags
    Matlab Matrices
Click For Summary

Discussion Overview

The discussion revolves around how to efficiently loop through multiple matrices in MATLAB for the purpose of performing LU factorization. Participants explore various programming techniques and data structures to automate the process of applying a routine to multiple matrices.

Discussion Character

  • Technical explanation
  • Exploratory
  • Mathematical reasoning

Main Points Raised

  • One participant suggests using a loop to automate the processing of matrices A1 through A6, expressing difficulty with the notation A(i) which does not function as intended.
  • Another participant recommends using the sprintf() function to dynamically create variable names and suggests initializing matrices with zeros for efficiency.
  • A different approach is proposed involving the use of a three-dimensional array A(l,m,n), where one index could represent different matrices, allowing for looping through them.
  • Another participant mentions the potential utility of using a 'struct' in combination with 'sprintf' to manage the matrices more effectively.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to handle the looping through matrices, with no consensus reached on the best approach. Each suggestion offers a different method, indicating a variety of perspectives on the problem.

Contextual Notes

Some suggestions depend on specific MATLAB functions and may require further clarification on their implementation. The discussion does not resolve which method is superior or more efficient.

Who May Find This Useful

Readers interested in MATLAB programming, particularly those working with matrix operations and seeking to automate processes in their code.

TimFys
Messages
2
Reaction score
0
I have created a program in MatLab doing LU factorization and need to implement a routine, so that MatLab automatically runs the program on all of my matrices.

I have 6 matrices A1, A2, A3, A4, A5 and A6. But for the time being I can only run the program for one matrix, write the result down and then change the input matrix.

Is there a smart way of making a loop so that MatLab automatically changes the input matrix?

The notation A(i) does not work, since it refers to the elements in the matrix.
 
Physics news on Phys.org
look at sprintf() to write the variables then allocate A1 = zeros(x,x);

Matlab is faster if you do not use loops, but write out the code as a list

A1 = ...
A2 = ...

etc...
 
You can define a three-dimensional array A(l,m,n) so that one of your indices (I forget which one it is) points to the different matrices in a loop.
 
you might want to check out 'struct' (in conjunction with 'sprintf').
 

Similar threads

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