how to count the number of consecutive rows less than a given sum?

2 views (last 30 days)
I have the following kind of data:
Data=[1; 2; 3; 8; 9; 10; 20; 30]
I want to count the number of consecutive rows which the difference between the first and the last element is less than or equal to 5.
For the example above, it would result in 3 counts:
-rows 1, 2 and 3 = 3
-rows 3 and 8 = 2
-rows 8, 9 and 10 = 3
In the end, I’d like to create a matrix in which the first column indicates the first row of the counting and the second column indicates the sum achieved:
Result =[1 3; 3 2; 8 3]
Many thanks in advance.
Flavio
  5 Comments
Flavio Silva
Flavio Silva on 27 Oct 2015
Thank you all for your supporting.
Indeed, my wording led to a misunderstanding... Instead of "row", I meant "element". So, the idea is to get an output such as:
-Rows 1-3: there are 3 elements which the difference between the first (1) and the last (3) is =<5
-Rows 3-4: there are 2 elements which the difference between the first (3) and the last (8) is =<5
-Rows 4-6: there are 3 elements which the difference between the first (8) and the last (10) is =<5
The code provided by Stephen works smoothly for the example above. However, when I increase the data set, for example: Data = [1; 2; 3; 8; 9; 10; 20; 30; 33; 38; 39; 65; 89; 90; 1054]; it gets an error: "Subscripted assignment dimension mismatch.
Error in i30a (line 7) out(:,2) = 1+C-R % count elements"
Actually my data set is much larger (c.a. 20000 rows) and consists of timestamps (DD-MM-YYYY HH:MM)
Any ideas?
Again, thank you very much for your kind attention, and apologies for my poor code wording...
Stephen23
Stephen23 on 27 Oct 2015
Edited: Stephen23 on 27 Oct 2015
The problem is that the variable out still exists from the last time that you ran my code. You should clear the variables from the MATLAB workspace and then run the code again.
Alternatively change the last two rows of code to this:
out = Data(R); % first element
out(:,2) = 1+C-R; % count elements

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 26 Oct 2015
Edited: Stephen23 on 27 Oct 2015
Assuming that the data is monotonically increasing then this is easy using vectorized code:
Data = [1; 2; 3; 8; 9; 10; 20; 30];
X = bsxfun(@minus,Data(:).',Data(:));
Y = fliplr(0<X & X<=5);
Z = fliplr(Y & cumsum(Y,1)==1 & cumsum(Y,2)==1);
[R,C] = find(Z);
out = Data(R); % first element
out(:,2) = 1+C-R; % count elements
The code produces this output:
>> out
out =
1 3
3 2
8 3
EIDT to make out allocation replace existing out.
  2 Comments
Stephen23
Stephen23 on 27 Oct 2015
Edited: Stephen23 on 27 Oct 2015
Your new Data also works fine with my code:
>> clear
>> Data = [1; 2; 3; 8; 9; 10; 20; 30; 33; 38; 39; 65; 89; 90; 1054];
and run the code again:
>> out
out =
1 3
3 2
8 3
30 2
33 2
38 2
89 2

Sign in to comment.

More Answers (0)

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!