Improving efficiency and runtime to use a binary mask to sort another set of XY coordinates?

1 view (last 30 days)
This piece of code's purpose is to use a binary mask matrix I create earlier from an image (binaryMask) to filter another set of XY data read into the program (bigData). This code works well by rounding any decimals to check to the nearest integer of the binary mask. However, since it is looping through a very large data set (can be over a few dozen million data points), this takes quite some time. Is there any way to speed this up using a matlab function? I also would like to possibly not have to round my values to the nearest integer if possible, but my code doesn't produce the right outputs if I don't round.
I have searched all throughout the internet and cannot find anything, or I am not searching for the right terms. Any insight is greatly appreciated.
I have included a sample data set and binary mask to help that are generated in the code (first section of the code).
Thanks!
%Create the Binary Mask (This is for demonstration purposes only, normally this is created using other methods but functions the same)
%Make the binary mask matrix
binaryMask = zeros(1868, 1868);
%determine center of the circle and radius
y_center = 921
x_center = 921
radius = 150
[xx, yy] = meshgrid(1:1868, 1:1868);
%create the circle
circle = (xx-x_center).^2 + (yy-y_center).^2 <= radius.^2;
%apply the circle to the mask
binaryMask(circle) = 1;
% Generate random X and Y coordinates and combine into matrix (In actual
% code not random, shown here as such.
x_coords = 1868*rand(1000, 1);
y_coords = 1868*rand(1000, 1);
bigData = [x_coords, y_coords];
%% This is the part I need help on
%Find size of bigData
maxbigData = length(bigData)
%Loop through bigData
for i = 1:maxbigData
%assign the X and Y coordinates from bigData
X = bigData(i,1);
Y = bigData(i,2);
%round the values to nearest integer
Xround = round(bigData(i,1));
Yround = round(bigData(i,2));
%Check to see if the mask has a value of 1 for this value in bigData
if binaryMask(Xround,Yround) == 1
%If so, save coordinates to be separated
rowToadd = [X,Y];
DataHoldTarget = [DataHoldTarget;rowToadd];
elseif binaryMask(Xround,Yround) == 0
%If not, save coordinates too, separate from the other data points
rowToadd = [X,Y];
DataHoldElse = [DataHoldElse;rowToadd];
end
disp(i);
end

Accepted Answer

Matt J
Matt J on 27 Mar 2023
Edited: Matt J on 27 Mar 2023
binaryMask = zeros(1868);
%determine center of the circle and radius
y_center = 921;
x_center = 921;
radius = 150;
[xx, yy] = meshgrid(1:1868, 1:1868);
%create the circle
circle = (xx-x_center).^2 + (yy-y_center).^2 <= radius.^2;
%apply the circle to the mask
binaryMask(circle) = 1;
% Generate random X and Y coordinates and combine into matrix (In actual
% code not random, shown here as such.
x_coords = (1868-1)*rand(1000, 1)+1;
y_coords = (1868-1)*rand(1000, 1)+1;
tic;
vals=interp2(binaryMask, x_coords,y_coords,'linear',0);
bigData = [x_coords, y_coords];
DataHoldTarget=bigData(vals==1,:);
DataHoldElse=bigData(vals~=1,:);
toc;
Elapsed time is 0.049372 seconds.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!