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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by