Azzera filtri
Azzera filtri

Find submatrix given two corners of matrix

1 visualizzazione (ultimi 30 giorni)
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'}

Risposta accettata

GreenEye
GreenEye il 27 Ott 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)

Più risposte (0)

Categorie

Scopri di più su Sparse Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by