Data points in an imaginary grid cell

3 views (last 30 days)
Ricardo Duarte
Ricardo Duarte on 29 Nov 2022
Answered: Tushar on 5 Sep 2023
Dear all,
I have a list of data points in two dimensions that correspond to the coordinates (longitude and latitude) of my vessels. Then there is also a third variable, connected to each vessel position, that corresponds to time.
Now I want to define an imaginary grid and plot those vessels into a plot, but instead of plotting all points/vessels I just want identify the cells that have vessels and consider one equivalent vessel in those grid cells during a specific period of time (let’s say one hour).
I'm a bit lost on this. Thank you in advance for your help.
  2 Comments
Voss
Voss on 29 Nov 2022
Here's one way to read the data and plot the lat/lon for all times:
fid = fopen('Pasta1.csv');
data = fread(fid,'*char').';
fclose(fid);
data = strsplit(data,{';' sprintf('\r\n')});
if isempty(data{end})
data(end) = [];
end
data = reshape(data,3,[]).';
data(1,:) = [];
data(:,[1 2]) = num2cell(str2double(data(:,[1 2])));
% Note the warning below: ambiguous format, i.e., does "01/07" mean "Jan 7" or "Jul 1"?
% (There's no way for me to know the answer to that.)
data(:,3) = num2cell(datetime(data(:,3)));
Warning: Successfully converted the text to datetime using the format 'MM/dd/uuuu HH:mm', but the format is ambiguous and could also be 'dd/MM/uuuu HH:mm'. To create datetimes from text with a specific format call:

datetime(textinput,'InputFormat',infmt)
plot([data{:,1}],[data{:,2}],'.')
Can you explain more about what grid you want to impose on these points, and for what purpose? In particular, it's not clear to me what you mean by, "consider one equivalent vessel".
Ricardo Duarte
Ricardo Duarte on 29 Nov 2022
Dear Voss thank you for your help in this issue.
In fact the data I've attached before was just a short part of my dataset and thats why the plot is so "empty" (the entire dataset has +/- 50000 lines)
Well, regarding your question. I want to create a rectangular grid in order to identify how many vessels will "fall inside" each cell. When I have the information about how many vessels exists in each cell I want to condensate those that have similar time.
Hope this help to turn my problem a bit more clear.
Thank you.

Sign in to comment.

Answers (1)

Tushar
Tushar on 5 Sep 2023
Hi Ricardo,
I understand that you are trying to find out the number of vessels in a rectangular grid area.
Looking at your comment, I have added to the code shared by @Voss:
%converting to an array
data_array = [cell2mat(data(:,1)),cell2mat(data(:,2))];
%define shape of the rectangular grid box that you want
length=5; width=10;
rect_number=grid_func(length,width,data_array(1,:));
num_points=points_func([0 6],length,width,data_array);
function rectangle_number = grid_func(length, width, x)
rectangle_number= [fix(x(1)/width) fix(x(2)/length)];
end
function number_of_points = points_func(rectangle_number,length, width, data_array)
len = [rectangle_number(1)*length, (rectangle_number(1) + 1)*length];
wid = [rectangle_number(2)*width, (rectangle_number(2) + 1)*width];
x_indices = find(data_array(:,1)>len(1) & data_array(:,1)<len(2));
y_indices = find(data_array(:,2)>wid(1) & data_array(:,2)<wid(2));
number_of_points = size(intersect(x_indices,y_indices));
number_of_points=number_of_points(1);
end
The rectangular grid that I have assumed-
  • Rectangle number will have two coordinates: [X Y]
  • X=0 and Y=0 for the first rectangle on the first quadrant
You can even calculate the rectangle number for any point. I hope these functions will be helpful for you.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!