sum in intervals from column!

9 views (last 30 days)
Shayma
Shayma on 11 Feb 2015
Commented: Star Strider on 12 Feb 2015
Hi, after a long search for an answer, i give up!
I have a double vector: Index=[-1:0.1:1]
and a csv file (2 columns, one with 1/0 and the second column with indexes values)the rows different from file to another, looks like that:
N=
1.0000 0.7810
1.0000 0.7810
1.0000 0.7810
1.0000 0.7810
1.0000 0.0240
1.0000 0.7810
1.0000 0.7810
0 -0.8210
0 -0.2620
0 -0.8210
0 0.0930
0 -0.0920
0 -0.8210
0 -0.8210
0 -0.0670
0 -0.3760
0 -0.8210
0 -0.8210
0 -0.8210
0 -0.8210
i want to sum for each interval i.e: <-0.9, <-0.8 and so on.. the indexes (2nd column) for positive(1) and negatives(0) separately. not an accumulation sum, for <-0.8 it will be only for those who has -0.9<x<-0.8 etc.. that's what i tried to do:
[N T D] = xlsread('file.csv');
Index=[-1:0.1:1];
[r c]= size(N);
pos_idx= find(N(:,1)==1);
neg_idx= find(N(:,1)==0);
%first try
pos_count=accumarray(N(N(pos_idx,2)<Index'),[N(pos_idx,2)])
Error using < Matrix dimensions must agree.
%second try
A = accumarray(Index,[N(pos_idx,2)]',[],@(x) sum(x,'native'))
Error using accumarray Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
%third try
count_p=sum([N(pos_idx,2)]'<Index)
Error using < Matrix dimensions must agree.
any idea where is the problem?? i tried to transpose the Index and the column, not success!!
thank's in advance :)

Accepted Answer

Star Strider
Star Strider on 11 Feb 2015
This seems to work:
binedg = [-1:0.1:1];
[Nh,edges,bin] = histcounts(N(:,2), binedg);
S = accumarray(bin, N(:,2));
If you don’t have histcounts, use:
[Nh,bin]= histc(N(:,2), binedg);
If all goes well, ‘S’ is your interval summaton vector corresponding to the intervals in ‘binedg’.
  2 Comments
Shayma
Shayma on 12 Feb 2015
histc works great!
thank you a lot :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!