简体   繁体   中英

Matlab, values over specific y-range

In MATLAB, I would like to know what the best way is to get the x -range values over an specific y -range.

For example, if I have a graph of fluctuating temperature ( y -axis) with respect to time ( x -axis), I want to know how many times the temperature is above 550°C but below 600°C.
The temperature fluctuates over time, so many intervals between these temperatures can be found along the graph.

I think you're looking for something like this:

T = rand(100,1); %Your temperature variable
c = (T > 0.2) & (T < 0.8); %Your threshholds, in your case switch the 0.2 for 550 and the 0.8 for 600
sum(diff([0; c]) == 1)

What's happening here is this: c = (T > 0.2) & (T < 0.8) creates a mask where points that are between your threshholds are 1 and points that are outside are 0 . Now diff finds the difference between each adjacent point so for example diff([1 0 0 1 1 1 0]) will return -1 0 1 0 0 -1 but we only want to count each time our c vector goes from 0 to 1 (or we could count it going from 1 to 0 , so long as we don't count both) hence the == 1 . Finally we don't want to miss a gorup of ones starting at the beginning hence we add a 0 to the beginning.

EDIT:

Based on your comment and assuming you have a time vector called x and a temperature vector called y :

dx = x(2)-x(1) %I'm assuming this remains constant throughout x, i.e. x(n) - x(n-1) is constant
mask = (y >= 550) & (y <= 600)
totalTime = sum(mask)*dx

If the time differences in x are not constant then do this:

mask = (y >= 550) & (y <= 600)
dx = x(end) - x(end-1);
d = -diff([0;mask;0]).*[x; x(end)+dx];
totalTime = sum(d)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM