Accessing elements of the matrices from an array of matrices
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2020-4-9
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!