Accessing elements of the matrices from an array of matrices
1 view (last 30 days)
Show older comments
Jhanzaib Humayun
on 9 Apr 2020
Answered: Walter Roberson
on 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 Comments
Accepted Answer
Walter Roberson
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!