How to create a list of matrices??

82 visualizaciones (últimos 30 días)
Benoit
Benoit el 2 de Nov. de 2022
Comentada: Benoit el 2 de Nov. de 2022
I would like to store 5 matrices into one list. As I am used to Python I am quite surprised that I can't do it in a simple and obvious way.
Is there a way for me to create a list L so that L(i) would return me a Matrice n*n ?
L=((n*n),(n*n)...(n*n))
Thank you!

Respuesta aceptada

Matt J
Matt J el 2 de Nov. de 2022
Editada: Matt J el 2 de Nov. de 2022
As I am used to Python I am quite surprised that I can't do it in a simple and obvious way.
cell arrays are a close analogue of Python lists:
L={rand(2), ones(3), eye(4)}
L = 1×3 cell array
{2×2 double} {3×3 double} {4×4 double}
L{1}, L{2},L{3}
ans = 2×2
0.3577 0.6653 0.2272 0.3156
ans = 3×3
1 1 1 1 1 1 1 1 1
ans = 4×4
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
  3 comentarios
Matt J
Matt J el 2 de Nov. de 2022
Let's try it:
T(1:5)={zeros(2)};
T{1}=[1 2 ; 3 4];
T{:}
ans = 2×2
1 2 3 4
ans = 2×2
0 0 0 0
ans = 2×2
0 0 0 0
ans = 2×2
0 0 0 0
ans = 2×2
0 0 0 0
Benoit
Benoit el 2 de Nov. de 2022
eureka! thanks a lot! :)

Iniciar sesión para comentar.

Más respuestas (2)

Voss
Voss el 2 de Nov. de 2022
In MATLAB, use a cell array:
C = {magic(4) eye(5) rand(3)}
C = 1×3 cell array
{4×4 double} {5×5 double} {3×3 double}
C{1}
ans = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
C{2}
ans = 5×5
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
C{3}
ans = 3×3
0.2948 0.3827 0.6177 0.4998 0.2049 0.7628 0.5140 0.8962 0.8896
Or since all your matrices are the same size, use a 3d array:
M = cat(3,magic(4),eye(4),rand(4));
M(:,:,1)
ans = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
M(:,:,2)
ans = 4×4
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
M(:,:,3)
ans = 4×4
0.7979 0.3541 0.9245 0.6678 0.3092 0.4018 0.4950 0.1476 0.4253 0.5871 0.8594 0.9111 0.7304 0.1848 0.5376 0.0298

Walter Roberson
Walter Roberson el 2 de Nov. de 2022
You would have to construct a custom class for that, and have it define subsref() or use the newer facility to define a method specifically for () access.
You run into semantic problems. If you have defined such a list L already, so that L(2) [for example] returns a numeric matrix, then if you then have
L(2) = magic(7)
then what should happen? You have defined L(2) as returning a numeric matrix, so what is happening in that statement? Is the second entry in the list being replaced? Is L(2) on the left hand side returning the address in memory of the existing second matrix, and what is stored at that location is to be overwritten with the contents of magic(7), including changing any shared references to it?
A = L(2)
L(2) = magic(7)
A is what now? The numeric array that L(2) referred to before? Or did the memory get bashed so that now A also has magic(7) as its contents?
What should
L(2)(3,2)
do? MATLAB will reject that as invalid syntax, but if L(2) is returning a numeric array then does it not make sense that you might want to immediate refer to an element of that numeric array?
I would suggest to you that this is not an avenue worth pursuing. I would suggest to you that you instead use cell arrays such as
L = {A, B, C, D, E}
after which L(2) would be the cell array {B} and L{2} would be the numeric array B . L{2}(3,2) is well defined in MATLAB.
The difference here is that you wanted to be able to use L(2) to get to a complete array, whereas with cell arrays, L(2) is would be a scalar cell array, and L{2} would be what is stored at that scalar. L(2) versus L{2} . If you are willing to live with using L{2} then cell arrays do fine. If you need L(2) to refer to a complete array then you have... challenges.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by