필터 지우기
필터 지우기

Find submatrix given two corners of matrix

조회 수: 1 (최근 30일)
GreenEye
GreenEye 2020년 10월 27일
답변: GreenEye 2020년 10월 27일
Given a matrix
{'A'} {'B'} {'C'} {'D'} {'E'}
{'F'} {'G'} {'H'} {'I'} {'J'}
{'K'} {'L'} {'M'} {'N'} {'O'}
{'P'} {'Q'} {'R'} {'S'} {'T'}
{'U'} {'V'} {'W'} {'X'} {'Y'}
we can get that using
square=cell(5);
alpha='A':'Y';
for i=1:25
square{i}=alpha(i)
end
square=square'
using the matrix either
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
or
0 0 0 0 0
0 0 0 0 1
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
for corners
extract
{'G'} {'H'} {'I'} {'J'}
{'L'} {'M'} {'N'} {'O'}
{'Q'} {'R'} {'S'} {'T'}

채택된 답변

GreenEye
GreenEye 2020년 10월 27일
For anyone in the future, I solved it. Here is the code. Replace S and A with any letters
%this part constructs the matrix
square=cell(5);
alpha='A':'Y';
for i=1:25
square{i}=alpha(i);
end
square=square';
[row,col]=find(ismember(reshape(alpha,5,[])',['S' 'A'])==1); %find the corners
%of submatrix given two letters
submat=square(row(1):row(2),col(1):col(2)); %find indices of corners and construct a submatrix
if isempty(submat) %if matrix is empty, reverse rows
submat=square(row(2):row(1),col(1):col(2));
end
disp(submat)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by