I am looking for an elegant solution to this very simple problem in MATLAB. Suppose I have a matrix
>> M = magic(5) M = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
and boolean form variable
I = 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
If I try to extract the elements of M associated with the values 1 in I , I get a column vector
>> M(I) ans = 5 6 7 13
What will be the easiest way to get the matrix [5 7 ; 6 13] [5 7 ; 6 13] from this logical indexing?
If I know the form of non-zero elements of I , I can use the transformation after indexing, but this is not a general case.
Also, I know that the default behavior for this type of indexing in MATLAB provides consistency with respect to the case where non-zero values ββin I do not form a matrix, but I wonder if there is a simple solution for this particular case.
matrix indexing matlab submatrix
foglerit
source share