Function: counting occurrences using 1 vector to count in another
Show older comments
Hi!
I have 2 vectors.
Beta = 0.4, 0.5, 0.6 1.2, 1.9, 2, 5.3, 6.7
wins = 0.4, 1.2;
1.3, 6.7.
Beta represents hit points in seconds - something occurred at 0.4 seconds, 0.5 seconds etc. Wins represents timeframes identified as important - I need to look at the timeframe between 0.4 and 1.2. I'm creating a function where there are likely to be more beta hits and more time frames. I need to examine how many hit points took place in a specific timeframe and then average it. The numbers are presented as seen and does not have time stamps.
So;
Hits = sum((beta>=wins(1,1) & beta<=wins(1,2))) = 4
timescale=wins(1,2)-wins(1,1) = 0.8 seconds
hits/timescale (4/0.8) = 5 hits average.
What amendment to the code do I need to make it run through all the time frames identified by wins and provide all the averages?
Answers (1)
Ameer Hamza
on 9 Jun 2020
Try this
Beta = [0.4, 0.5, 0.6 1.2, 1.9, 2, 5.3, 6.7];
wins = [0.4, 1.2;
1.3, 6.7];
avg_vals = zeros(size(wins,1), 1);
for i=1:size(wins, 1)
avg_vals(i) = sum(discretize(Beta, wins(i,:)), 'omitnan')/diff(wins(i,:));
end
2 Comments
Nick Storr
on 9 Jun 2020
Edited: Nick Storr
on 9 Jun 2020
Ameer Hamza
on 9 Jun 2020
Can you tell what the expected output for the given input vectors is?
Categories
Find more on Resizing and Reshaping Matrices 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!