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 Comments

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]
so you'd get 2 numbers, 10 and 243? Is this homework?
Not quite. With your example I would get two numbers: 50 and 220 (then whatever two numbers came after 333 would be added to 82 and 333). This is for a job I am working, sorry for being so vague with my original question.

Sign in to comment.

Answers (2)

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

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?
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.

Sign in to comment.

"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?"
Not at all. It sounds like a typical job for accumarray and possibly discretize:
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

I am very new to Matlab, could you explain to me what each of these operations do? Thanks.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 21 Jan 2016

Commented:

on 21 Jan 2016

Community Treasure Hunt

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

Start Hunting!