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?

채택된 답변

Walter Roberson
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개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by