how can i find the slope for every six rows of my data

1 view (last 30 days)
I have data with 57.000 rows [Windspeed and time] and I want to calculate the rate of change or the slope of the windspeed for every six rows because 6 rows completes 1 hour. What fuction is possible to use for the rate of change or the slope? my final answer will be 57000/6=9500 rows or columns.

Accepted Answer

Star Strider
Star Strider on 8 Jan 2018
One approach:
WindTime = [(0:(1/6):5-(1/6))' rand(30,1)]; % Time (Col#1), Velocity (Col#2)
WindReshape = reshape(WindTime(:,2), 6, []); % Reshape Velocity Data Only
B = [WindTime(1:6,1) ones(6,1)]\WindReshape; % Estimate Slope & Intercept
SlopeVector = B(1,:); % Save Slopes
The code reshapes your velocity data only into a 6-row, ‘N’-column matrix. It then calculates the slope and intercept (for uniformity using the first hour only for the time vector, rather than each successive hour), and then calculates the slope and intercept in matrix ‘B’. As I coded it, the slope is the first row, so extracting that gives the slopes for each hour segment. (It assumes that there are no missing or non-finite data, and does not check for those possibilities.)
If you copy your data array to ‘WindTime’, you can likely use my code without further changes.
  2 Comments
Olger Papa
Olger Papa on 9 Jan 2018
Edited: Olger Papa on 9 Jan 2018
I don't understand why rand(30,1), i think the final answer of the slope will be 9500 rows or columns because 57000/6=9500. Thank sou for your effort. I want something like that
but for every six rows
Star Strider
Star Strider on 9 Jan 2018
As always, my pleasure.
The rand call was to create a matrix to test my code. (If possible, I always test my code before posting it.)
Note that in my ‘WindTime’ matrix, I have time in the first column and wind velocity in the second column. You will need to change the column references in my code to work with the Excel spreadsheet you read into your workspace.

Sign in to comment.

More Answers (1)

John Harris
John Harris on 8 Jan 2018
Your formula will depend on what kind of answer you want, but I think simple math operators will do most of the work for you.
Rate of change is (windspeed2-windspeed1)/(time2-time1) ... but if you apply this to every 6th entry, you're throwing away a lot of data. What if the wind is still at the top 'o the hour, but 10 knots in opposite directions every half hour?
Perhaps you want to calculate the rate for each measurement, then take an average for each 1-hour period?
  1 Comment
Olger Papa
Olger Papa on 9 Jan 2018
I thing you're right my question it was not clearly enough. I want the final answer to be slope=57000/6=9500 rows or columns.
Something like that but for for every six rows.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!