How to find indices of a rectangular region inside big matrix? | Efficiently
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
JAI PRAKASH
le 2 Nov 2018
Modifié(e) : JAI PRAKASH
le 24 Nov 2018
How can indices of elements belong to a rectangular region can be found?
A = [4 4 4 4 4 4 4
4 1 1 1 1 3 0
4 1 3 3 1 3 0
4 1 3 3 1 3 0
4 1 1 1 1 3 0
4 4 4 4 4 4 4];
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/193806/image.png)
Input: Matrix's size[height, width] , [Row_start Row_end], [Col_start Col_end]
Output: [21 22 23 27 28 29 33 34 35]
Why efficiently : to do same for multiple combinations of rows & columns
Thank you
4 commentaires
Caglar
le 2 Nov 2018
I am not sure If I understood what you are asking for. If you want to get a part of a matrix you can do it like A(3:5,4:6) .
Réponse acceptée
Bruno Luong
le 2 Nov 2018
Modifié(e) : Bruno Luong
le 2 Nov 2018
recidx = (Row_start:Row_End)' + height*(Col_start-1:Col_end-1)
6 commentaires
Plus de réponses (2)
Geoffrey Schivre
le 2 Nov 2018
Hello,
I'm not sure if it's efficient enough but try :
p = nchoosek([Row_start:Row_end,Col_start:Col_end],2);
idx = unique(sub2ind(size(A),p(:,1),p(:,2)));
Geoffrey
5 commentaires
Geoffrey Schivre
le 2 Nov 2018
Sorry, I didn't refresh this page so I didn't see that your question was answered. I'm glad you find what you ask for !
Caglar
le 2 Nov 2018
Modifié(e) : Caglar
le 2 Nov 2018
function result = stack (A,row_start,row_end,col_start,col_end)
% A = [4 4 4 4 4 4 4
% 4 1 1 1 1 3 0
% 4 1 3 3 1 3 0
% 4 1 3 3 1 3 0
% 4 1 1 1 1 3 0
% 4 4 4 4 4 4 4];
% row_start=3; col_start=4;
% row_end=5; col_end=6;
height=(size(A,1));
result=(row_start:row_end)+(height)*((col_start:col_end)'-1);
result=transpose(result); result=result(:);
end
0 commentaires
Voir également
Catégories
En savoir plus sur Get Started with MATLAB 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!