Keep elements of an array that only fall within a given closed shape
8 views (last 30 days)
Show older comments
Hello everyone
I have the following two vectors defined in my system:
space_a=linspace(0,168.3934962848858,500); % nm
space_b=linspace(0,250,500); % nm
In addition, I have a 500x500 matrix where each element contains a numerical value. The (1,1) element of this array corresponds to the value at space_a=0 and space_b=0, while the (500,500) element corresponds to the value at space_a=168.3934962848858 and space_b=250. If I plot my data, I have something like this:
My goal is, maintaining the fact that my matrix is a 500x500 array, only to preserve the original numerical value of those array elements that are inside the closed shape drawn on top of the figure through a solid black line. The coordinates of the four vertices of that shape are given by (first column: a-th dimension in nm, second column: b-th dimension in nm):
0*max(space_a) 0.45346624100816*max(space_b)
0.545581275830418*max(space_a) 1*max(space_b)
1*max(space_a) 0.54653375899184*max(space_b)
0.454418724169582*max(space_a) 0*max(space_b)
Any idea on how I can accomplish this while maintaining the fact that it is a 500x500 matrix, with the elements of the matrix outside the closed shape being equal to 0?
0 Comments
Accepted Answer
Chunru
on 20 Mar 2024
space_a=linspace(0,168.3934962848858,50); % nm
space_b=linspace(0,250,50); % nm
z = peaks(50); % generate some test data
% polyshape
coord = [0*max(space_a) 0.45346624100816*max(space_b)
0.545581275830418*max(space_a) 1*max(space_b)
1*max(space_a) 0.54653375899184*max(space_b)
0.454418724169582*max(space_a) 0*max(space_b)];
pgon = polyshape(coord(:, 1), coord(:, 2));
[xx, yy] = meshgrid(space_a, space_b);
% use inpolygon to find the data
in = inpolygon(xx, yy, coord(:, 1), coord(:, 2));
zin = nan(size(z));
zin(in) = z(in);
imagesc(space_a, space_b, zin);
axis xy
hold on
plot(pgon)
0 Comments
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!