Effacer les filtres
Effacer les filtres

Find submatrix given two corners of matrix

1 vue (au cours des 30 derniers jours)
GreenEye
GreenEye le 27 Oct 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'}

Réponse acceptée

GreenEye
GreenEye le 27 Oct 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)

Plus de réponses (0)

Catégories

En savoir plus sur Sparse Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by