How do I add sequential groups of numbers using a "for end" loop
显示 更早的评论
How can I use for end loops to add together sequential groups of numbers from one pool of data?
2 个评论
Image Analyst
2016-1-21
编辑:Image Analyst
2016-1-21
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
2016-1-21
回答(2 个)
Star Strider
2016-1-21
0 个投票
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 个评论
Nick Johnson
2016-1-21
Star Strider
2016-1-21
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
2016-1-21
"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))
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!