How to find indices of a rectangular region inside big matrix? | Efficiently

조회 수: 10 (최근 30일)
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];
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
Caglar
Caglar 2018년 11월 2일
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) .
JAI PRAKASH
JAI PRAKASH 2018년 11월 2일
Sorry if I am not clear in my question.
I want linear indices of all the elements present in rectangular region.
e.g

댓글을 달려면 로그인하십시오.

채택된 답변

Bruno Luong
Bruno Luong 2018년 11월 2일
편집: Bruno Luong 2018년 11월 2일
recidx = (Row_start:Row_End)' + height*(Col_start-1:Col_end-1)
  댓글 수: 6
JAI PRAKASH
JAI PRAKASH 2018년 11월 3일
Hi @Bruno
Do you have some idea for below question also.
Which is nothing but continuation of the problem, that you solved just now.
Bruno Luong
Bruno Luong 2018년 11월 3일
James's solution is very well. I doubt I can do better.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Geoffrey Schivre
Geoffrey Schivre 2018년 11월 2일
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
Geoffrey Schivre
Geoffrey Schivre 2018년 11월 2일
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 !
JAI PRAKASH
JAI PRAKASH 2018년 11월 2일
Ya, But I learn interesting things from your comments.
'nchoosek' is new to me
Thanks

댓글을 달려면 로그인하십시오.


Caglar
Caglar 2018년 11월 2일
편집: Caglar 2018년 11월 2일
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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by