Using hist3 and sorting
2 次查看(过去 30 天)
显示 更早的评论
I'm simulating event positions and times, and histogramming then in the XY plane. I want to find for each event, the time difference to the next event in the same bin. I feel liek this should be a ~4 liner or so...
posXY = rand(nEv,2)
times = rand(nEv,1)
posBin = [2 2]; % put the events on a 2x2 mesh in X-Y plane
[hitMatrix, ~] = hist3(posXY, posBin);
then I want to find (for each event, in a given bin--I made four bins!) the time the next event occurs in that position bin. So i thought about sorting the events by their times, and using the indices to pick out the correct events that also occur in the same bin... making a logical and looping over events.. but I can't quite nail the whole sequence down. Do you have any tips?
I appreciate your input, Michael B.
0 个评论
采纳的回答
Stephen23
2016-1-20
编辑:Stephen23
2016-1-20
The trick is to get the indices from the histogram, but unfortunately hist3 does not return the indices of the input values. Introduced in 2015b is the function histcounts2, which does return these indices. However I do not have 2015b, so I found histcn on FEX, which seems to do what you require.
I tried it like this, is three lines okay?:
nEv = 10;
posXY = rand(nEv,2);
times = rand(nEv,1);
%
[idx,~,~,loc] = histcn(posXY,2,2);
A = accumarray(loc,times,[],@(n){n});
B = cellfun(@(v)diff(sort(v)),A,'UniformOutput',false)
Where B is a cell array where the cells correspond to the bins of the histogram and contain the differences in the sorted times values corresponding to the X and Y values that were sorted into the corresponding bin. The variable A contains the time values themselves, so you can check this by hand if you want.
4 个评论
Guillaume
2016-1-21
+1 about length. That should be purged from every single matlab example and the function deprecated.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!