How can I extract a submatrix based on rectangles drawn on a map?
조회 수: 2 (최근 30일)
이전 댓글 표시
I have plotted a map (401x401 matrix) and I drew four rectangles on it. I would like to extract 4 submatrices that correposond to the data inside the four rectangles. I'm also trying to extract a fifth submatrix that correpospond to all values outside the four rectangles (I assume this matrix will be 401x401 with zeros at indices already extracted). I tried extracting and plotting many times to get a close submatrix to what I am looking for (below the third comment in the code).
Can anyone please help me with this?
x = 0:25:10000;
y = 0:25:10000;
% map = 401x401 matrix (double)
figure;
imagesc(x,y,map); colormap jet; c=colorbar;
hold on; contour(x,y, map,'k','ShowText','on'); axis xy; xlabel('X (m)'); ylabel('Y (m)');
% Rectangles drawn
rectangle('Position', [3325 3300 3325 4175],'EdgeColor','y','LineWidth',4)
rectangle('Position', [0 0 4100 3300], 'EdgeColor','y', 'LineWidth', 4);
rectangle('Position', [4100 0 5900 3300], 'EdgeColor','y', 'LineWidth', 4);
rectangle('Position', [2525 7475 6000 2525], 'EdgeColor','y', 'LineWidth', 4);
% I tried extracting as follows but I couldn't extract the fifth submatrix
map_seg1 = map(133:301, 138:265);
map_seg2 = map(1:133, 1:164);
map_seg3 = map(1:133, 165:end);
map_seg4 = map(301:end, 102:340);
댓글 수: 0
채택된 답변
Chunru
2021년 8월 30일
% The fifth matrix
map_seg5 = map;
map_seg5(133:301, 138:265) = 0;
map_seg5(1:133, 1:164) = 0;
map_seg5(1:133, 165:end) = 0;
map_seg5(301:end, 102:340) = 0;
댓글 수: 3
Chunru
2021년 8월 30일
편집: Chunru
2021년 8월 30일
It seems that your index is not correct (wrong interperation of the position vector?). Here is code for finding one submatrix. You can repeat it for other submatrices.
x = 0:25:10000;
y = 0:25:10000;
r1 = [3325 3300 3325 4175]; % [x y w h]
idx_x = find(x>= r1(1) & x<=r1(1)+r1(3)); % find the index within the box
idx_y = find(x>= r1(2) & x<=r1(2)+r1(4)); % find the index within the box
%map_seg1 = map(idx_y, 1idx_x); % uncomment this
idx_x([1 end])
idx_y([1 end])
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Geodesy and Mapping에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!