sum in intervals from column!

조회 수: 1 (최근 30일)
Shayma
Shayma 2015년 2월 11일
댓글: Star Strider 2015년 2월 12일
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 :)

채택된 답변

Star Strider
Star Strider 2015년 2월 11일
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
Shayma
Shayma 2015년 2월 12일
histc works great!
thank you a lot :)
Star Strider
Star Strider 2015년 2월 12일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by