Adding data to intervals in Matlab

Hi, I have data in MATLAB:

F =

1.0000    1.0000
2.0000    1.0000
3.0000    1.0000
3.1416    9.0000
4.0000    1.0000
5.0000    1.0000
6.0000    1.0000
6.2832    9.0000
7.0000    1.0000
8.0000    1.0000
9.0000    1.0000
9.4248    9.0000
10.0000    1.0000

I am looking for a way to summarize data for specific periods of time. Example, if I want my sampling interval to be 1, the final result should be:

F =

1.0000    1.0000
2.0000    1.0000
3.0000    10.0000
4.0000    1.0000
5.0000    1.0000
6.0000    10.0000
7.0000    1.0000
8.0000    1.0000
9.0000    10.0000
10.0000    1.0000

ie data is accumulated in the second column based on the selection of the first row. Is there a function in MATLAB for this?

+4
source share
2 answers

Yes, combining histc()and accumarray():

F =[1.0000    1.0000;...
2.0000    1.0000;...
3.0000    1.0000;...
3.1416    9.0000;...
4.0000    1.0000;...
5.0000    1.0000;...
6.0000    1.0000;...
6.2832    9.0000;...
7.0000    1.0000;...
8.0000    1.0000;...
9.0000    1.0000;...
9.4248    9.0000;...
10.0000    1.0000];

range=1:0.5:10;

[~,bin]=histc(F(:,1),range);

result= [range.' accumarray(bin,F(:,2),[])]

If you run the code, remember that I changed the sampling interval ( range) to 0.5.

This code works for all sampling intervals, just define the required interval as range.

+3
source

Yes and this is work for accumarray :

1 F ( accumarray) .

s ( ):

S = accumarray(round(F(:,1)/s),F(:,2),[]); %// or you can use "floor" instead of "round".

S =

     1
     1
    10
     1
     1
    10
     1
     1
    10
     1

, :

 NewF = [unique(round(F(:,1)/s)) S]

NewF =

     1     1
     2     1
     3    10
     4     1
     5     1
     6    10
     7     1
     8     1
     9    10
    10     1

Yay!!

+3

All Articles