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.

 채택된 답변

Stephen23
Stephen23 2016년 1월 20일
편집: Stephen23 2016년 1월 20일

1 개 추천

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

Michael
Michael 2016년 1월 20일
Thanks for your help, but I'm not sure why length(A) is a number but the size of it indicates it's a matrix. I'm looking through the help, but I haven't quite figured out how get what I want from this. When I figure it out I'll mark you answer as correct (I'm sorry it's not more immediate!). Again, thank you!
Stephen23
Stephen23 2016년 1월 21일
편집: Stephen23 2016년 1월 22일
The value of length(A) does not tell you anything about how many non-singular dimensions A has (i.e. if A is a matrix). When you read the length help, it says on the first line "Length of largest array dimension", for example:
>> X = [1,2,3;4,5,6]
X =
1 2 3
4 5 6
>> length(X)
ans = 3
In this example it happens to be the second dimension, but note that which the longest dimension is depends on that particular array. If you want to know the size of the array (i.e. all non-singular dimensions), then use size:
>> size(X)
ans =
2 3
and numel for the total number of elements:
>> numel(X)
ans = 6
The function length causes so much confusion for beginners I would recommend to avoid using it altogether, and only using size and numel.
Guillaume
Guillaume 2016년 1월 21일
+1 about length. That should be purged from every single matlab example and the function deprecated.
Michael
Michael 2016년 1월 21일
Thanks for all your help! I'm still parsing the original solution but your input is very much appreciated (I never looked at length help--thinking I had mastered its use... jeez I feel like a newb now) Again, your time and help are much appreciated!

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

추가 답변 (0개)

카테고리

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

질문:

2016년 1월 20일

편집:

2016년 1월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by