How to extract matrix elements corresponding to a logical matrix?

Suppose I've a matrix A - (204 x 245) which has values (0-3) at certain locations. Value 2 is located in this matrix in the shape of a rectangle (see attached image). I want to extract the portion of MATRIX where 2 appears. First I tried using logical indexing which generates correct logical matrix (containing 0/1), let's call it 'P'. When I pass that logical matrix (P) as an argument to the original matrix (A), it gives me a (double) matrix of order (80X1). I wanted the output (of order: 8 x 10 as apparent in figure) in the form of (double) matrix, not as vector.
P = A == 2; % Logical indexing (output: 204 x 245)
Q = A(P); % Gives vector off type double (80 x 1)
% Required?
% A(37:44 , 76:85)
I also tried find() to extract row & column indices but still couldn't get the required matrix. Perhaps I'm doing something wrong :( Kindly guide me in this regard.
P.S: The coding is dynamic so a solution like A(37:44 , 76:85) is useless

 采纳的回答

更多回答(1 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by