Clear Filters
Clear Filters

How can I extract a submatrix based on rectangles drawn on a map?

2 views (last 30 days)
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);

Accepted Answer

Chunru
Chunru on 30 Aug 2021
% 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 Comments
Chunru
Chunru on 30 Aug 2021
Edited: Chunru on 30 Aug 2021
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])
ans = 1×2
134 267
idx_y([1 end])
ans = 1×2
133 300

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!