Filter löschen
Filter löschen

Find submatrix given two corners of matrix

1 Ansicht (letzte 30 Tage)
GreenEye
GreenEye am 27 Okt. 2020
Beantwortet: GreenEye am 27 Okt. 2020
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'}

Akzeptierte Antwort

GreenEye
GreenEye am 27 Okt. 2020
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)

Weitere Antworten (0)

Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by