Finding durations between 1's in a matrix
조회 수: 2(최근 30일)
표시 이전 댓글
I am creating a matrix of ones
I = binornd(1,a,T(j),n);
What i need to find next is the durations between the 1 values for each column, that is to say that for each column i will need a vector with the durations between values of 1 in the columns of the matrix.
the duration is simply the difference between the occurences, so for example if there is a 1 in (1,1) of the matrix and (3,1). Then the first number in the duration vector would be 3-1=2
Thanks! I am trying to avoid doing a for loop on this (in part because i want to get better at avoiding them in general!)
채택된 답변
추가 답변(1개)
Cedric Wannaz
2013년 3월 27일
편집: Cedric Wannaz
2013년 3월 27일
If you had the following matrix I for example:
>> I = rand(10,5) > 0.6
I =
0 1 1 0 0
0 1 0 0 1
1 0 1 0 0
0 0 0 0 0
1 0 0 1 1
0 0 1 0 1
0 0 1 0 0
1 0 0 0 0
1 1 1 0 0
0 1 0 0 0
you could get a 'per column' statistics of the duration/rowID difference between consecutive non-zero elements, as follows:
>> stat = arrayfun(@(cId) prctile(diff(find(I(:,cId))).', [25,50,75]), ...
1:size(I,2), 'UniformOutput', false)
stat =
[1x3 double] [1x3 double] [1x3 double] [1x3 double] [1x3 double]
where
>> stat{1}
ans =
1.2500 2.0000 2.7500
for example is a vector containing the 25th, 50th, and 75th percentile of "duration" differences for column 1.
The idea here can be illustrated with column 1 for example: the following
>> find(I(:,1))
ans =
3
5
8
9
provides you with row IDs of non-zero elements (durations in your context). You can then compute differences between consecutive elements:
>> diff(find(I(:,1)))
ans =
2
3
1
and make a stat of these for example:
>> prctile(diff(find(I(:,1))), [25,50,75])
ans =
1.2500 2.0000 2.7500
you could have taken the mean/min/max as well.
Now ARRAYFUN repeats this for all column IDs and outputs one vector of stats (resulting from PRCTILE) per column ID in a cell array.
댓글 수: 0
참고 항목
범주
Find more on Matrix Indexing in Help Center and File Exchange
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!