sum in intervals from column!
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Star Strider
le 11 Fév 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!