Accessing elements of the matrices from an array of matrices
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Jhanzaib Humayun
am 9 Apr. 2020
Beantwortet: Walter Roberson
am 9 Apr. 2020
Hi!
I have an array of matrices and i want to access individual element from one of those matrices. How do i do that? I have tried:
matrix = [A B C D]; % A, B, C and D are all matrices
m = matrix(1);
m(1,2);
But this gives the error "Index in position 2 exceeds array bounds (must not exceed 1)." How can i access elements in the matrices?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 9 Apr. 2020
You cannot do that. Once you construct matrix = [A B C D] then MATLAB throws away all information about how the matrix was constructed. It does not know afterwards, for example, whether matrix = [1,2,3] was used, or matrix = [1, [2 3]] or matrix = [[1,2],3] or matrix = [[1,2,3]] . There is no (realistic) way to take matrix and ask MATLAB "what was the first parameter of the horzcat() that was used to create this matrix?"
If you need to be able to decompose later, then use cell arrays:
matrix = {A, B, C, D}
but this will not act like a numeric array. In places where you need it to act like a numeric array you could use cell2mat(matrix) to get it to construct [A, B, C, D] but that's a nuisance.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!