Function: counting occurrences using 1 vector to count in another

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)

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

I'm afraid this didn't work. I'll keep looking
Can you tell what the expected output for the given input vectors is?

Sign in to comment.

Categories

Asked:

on 9 Jun 2020

Commented:

on 9 Jun 2020

Community Treasure Hunt

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

Start Hunting!