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

14 ビュー (過去 30 日間)
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

採用された回答

Guillaume
Guillaume 2014 年 8 月 17 日
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)))

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by