How do I sum the count between two values in an array corresponding to the values in another array of the same length?

10 views (last 30 days)
Hi,
I have very long text files of data that when loaded into matlab give me two arrays - each bin corresponding to a value in another:
e.g
x-data y-data
1392.6 8000
1392.9 8500
1393.1 8900
1393.4 9500
1394.2 10000
1394.6 10002
1394.9 15000
1395.2 17500
1395.5 15000
1396.4 10000
1397.1 8000
And I want to know what the sum of the y values between x = 1393 to 1396 is. E.g
some funtion that searches x array for values between 1393 and 1396 and sums the corresponding y values
therefore answer for above data would be 85,902.
Any ideas? I'm new to matlab so bear with me!
Thank you,
PC

Accepted Answer

Guillaume
Guillaume on 17 Aug 2014
Use find to get the indices of x that are within the range you want, and use these indices to get the elements of y:
sum(y(find(x >= 1393 & x <= 1396)))

More Answers (0)

Community Treasure Hunt

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

Start Hunting!