sum in intervals from column!
1 次查看(过去 30 天)
显示 更早的评论
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 :)
0 个评论
采纳的回答
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’.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!