How to avoid counting elements in each row of matrix multiple times?

I have written this function to calculate the location distribution in a form of a grid pattern(2D map of distribution).
x&y which are coordinates of each points are as follows:
X: is a matrix that each row contins a x position of points which creats an individual trajectory.(number of rows)
Y: is a matrix that each row contins a y position of points corresponding to x positions.
Note: each rows of X or Y contains a trajectory with a specified length and the rest of row is filled with NaNs:
X=[0.3 0.18 1.2 2.3 nan nan nan ,....,nan ;
0.1 0.12 0.5 2.6 nan nan nan ,....,nan ;
.......]
the same thing for Y.
=====================================
Question: the N matrix calculated in "for loop" below counts points multiple times if they are belong to one trajectory(one row of X) but pass through one grid multiple times. how can I modify the code to avoid counting points multiple times if they belong to the same trajectory but pass through the grid multiple times?
=====================================
function [binx,biny,dx,dy,N] = myfunc(X,Y,Nbin,[Xmin Xmax Ymin Ymax])
%Nbin=number of bins to create 2D matrix of N
lim(1)=Xmin;
lim(2)=Xmax;
lim(3)=Ymin;
lim(4)=Ymax;
% change to 1D arrays
X = X(:);
Y = Y(:);
%exclude nans
X = X(find(~isnan(X)));
Y = Y(find(~isnan(Y)));
%variable range
if isempty(lim)
in1 = min(X(:));
en1 = max(X(:));
in2 = min(Y(:));
en2 = max(Y(:));
else
in1 = lim(1);
en1 = lim(2);
in2 = lim(3);
en2 = lim(4);
end
%initialise bin sizes
dx = abs(en1-in1)/Nbin;
dy = abs(en2-in2)/Nbin;
binx = [in1+dx/2:dx:en1-dx/2];
biny = [in2+dy/2:dy:en2-dy/2];
%compute N matrix of 2D-map
N = zeros(Nbin,Nbin);
for ii = 1:length(X)
%check if variables fall within the domain
if X(ii) >= binx(1)-dx/2 && X(ii) <= binx(end)+dx/2 && Y(ii) >= biny(1)-dy/2 && Y(ii) <= biny(end)+dy/2
%find bin
indx = find(binx+dx/2 >= X(ii));
indx = indx(1);
indy = find(biny+dy/2 >= Y(ii));
indy = indy(1);
N(indy,indx) = N(indy,indx) + 1;
end
end

댓글 수: 12

Matt J
Matt J 2024년 3월 13일
편집: Matt J 2024년 3월 13일
What does it mean to "pass through the grid"? What is "the grid"?
Please provide sample inputs to myfunc, as well as the value of the output N you would like myfunc to return for those inputs.
Also, please provide a definition of myfunc that runs; e.g., lim is undefined, and this is incorrect function definition syntax:
% you can't use [ ] in the inputs of a function definition
function [binx,biny,dx,dy,N] = myfunc(X,Y,Nbin,[Xmin Xmax Ymin Ymax])
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
end
grids mean: each box of N matrix which is supposed to be filled with the number of locations.(e.g Nbin=50 generates a grid pattern of 50X50)
lim was defined in the code
Ham Man
Ham Man 2024년 3월 13일
이동: Voss 2024년 3월 13일
The file size in large to upload here:40MB (zip file)
%Nbin=number of bins to create 2D matrix of N
% lim(1)=Xmin
% lim(2)=Xmax
% lim(3)=Ymin
% lim(4)=Ymax
Those are comments. They do not define lim
if isempty(lim)
lim is used there without it having been defined.
Ham Man
Ham Man 2024년 3월 13일
이동: Voss 2024년 3월 13일
Is it possible that a single trajectory can land two (or more) sequential samples in the same bin without actually crossing? If so, are those counts to be included, or is a single trajectory to produce a map of strictly singular bin counts (i.e. a binary map of the bins that it occupies)?
many trajectories can pass through one bin and this is fine, but if one trajectory cross itself in a bin multiple times it should be counted one time. map is supposed to be created by many of such trajectories.
Matt J
Matt J 2024년 3월 14일
편집: Matt J 2024년 3월 14일
Yes, but that wasn't the question. The question was, what happens when one trajectory occupies the same bin multiple times, but doesn't cross itself, like what occurs in bins (4,2) and (5,3) in the drawing below? Are these bins to be counted only once, as well?
Ham Man
Ham Man 2024년 3월 14일
편집: Ham Man 2024년 3월 14일
Thank you Matt, yes that should be counted once as well.
Then see my answer below.

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2024년 3월 14일
편집: Matt J 2024년 3월 14일
Xedges=linspace(Xmin,Xmax, Nbin+1);
Yedges=linspace(Ymin,Ymax, Nbin+1);
N=0;
for i=1:height(X)
deltaN = histcounts2(X(i,:),Y(i,:),Xedges,Yedges);
N=N+logical(deltaN);
end

카테고리

도움말 센터File Exchange에서 NaNs에 대해 자세히 알아보기

질문:

2024년 3월 13일

댓글:

2024년 3월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by