フィルターのクリア

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 Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by