How do I add sequential groups of numbers using a "for end" loop
Show older comments
How can I use for end loops to add together sequential groups of numbers from one pool of data?
2 Comments
Image Analyst
on 21 Jan 2016
Edited: Image Analyst
on 21 Jan 2016
Do you mean like the 1,2,3,4 and 80,81,82 in this:
m = [44, 1, 2, 3, 4, 55, 80, 81, 82, 333]
Nick Johnson
on 21 Jan 2016
Answers (2)
Star Strider
on 21 Jan 2016
0 votes
You may be able to do that without a loop, depending on what your ‘sequential groups of numbers’ are, how they are organised, and how you want to add them. Without more information, this is as specific as I can get.
2 Comments
Nick Johnson
on 21 Jan 2016
Star Strider
on 21 Jan 2016
If your data are regularly sampled, I would put them into a column vector and then use the reshape function to create columns of one hour each (a (4xN) matrix). Then use the (default) column-wise operations to aggregate the hourly data however you want.
For example, to simulate one day of data sampled at 15-minute intervals:
X = randi(9, 96, 1); % Create Data (Column Vector)
XR = reshape(X, 4, []); % Reshape
SumX = sum(XR);
MeanX = mean(XR);
You can easily avoid the loop altogether. Loops have their places, but using a loop when you can avoid it makes for much more efficient code.
Guillaume
on 21 Jan 2016
"So my entire problem is this; I have a year's worth of data for a job I have to do. This data is in 15 minute increments. I need to compact this data into 1 hour increments so it is compatible to use with a different computer program. Am I on the right track with this for end loop?"
data = [0 1; 15 1.2; 30 1.4; 45 1.2; 60 1.8; 75 1.1; 90 1; 105 1.3; 120 1.3; 145 1.9]; %tw columns time, value
bins = discretize(data(:, 1), 0:60:180);
hourlydata = accumarray(bins, data(:, 2))
1 Comment
Nick Johnson
on 21 Jan 2016
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!