Average of every n elements of two separate vector
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have two different vectors A and B (each has 1000 numbers). Each number of A has a specific value in B (A(i,1)=B(i,1)). Some values of A are repeated in 1000 numbers. I want to make an average of every 10 elements in vector A and also find the average of vector B as well, for example:
A=[4 3 1 0 2 3 5 6]
B=[1 3 2 1 0 2 5 6]
I want to make an average every two elements of A, 0-2, 2-4, 4-6 and then find the average values of B .
Get something like:
A(new)=[1 3.33 5.5] ..........1=(0+1+2)/3 ,,,, 3.33=(3+3+4)/3,,,,,,5.5=(5+6)/2 >>>from vector A
B(new)=[1 2 5.5] ............ 1=(1+2+0)/3 ,,,,, 2=(3+2+1)/3 ,,,,,, 5.5=(5+6)/2 >>>>> from vector B
Thank you
1 commentaire
Image Analyst
le 11 Nov 2015
Well obviously A(i) does not equal B(i) because A(1) = 4 which is not equal to B(1) which is 1, and so on. So I'm not sure what that statement means.
Another question, are all the numbers integers? Or can you have numbers like 0.123 and 4.835?
When you say "average every two elements of A, 0-2, 2-4, 4-6" do you mean elements (indexes) of A, or values of A? Because, if you're talking about integers, 0,1,2 is 3 numbers, as is 2,3,4, not two numbers like you said. Or if A can be floating point numbers and you're talking about averaging all values in the continuous range 0 to 2, then you might have, say 5 numbers in that range, like 0.1,0.3, 1.3, 1.5, and 1.9, especially if A is 1000 elements long, and not just two numbers. So do you want to average all elements in that range regardless of where they appear and if they're next to each other or not?
Réponse acceptée
Stephen23
le 11 Nov 2015
Modifié(e) : Stephen23
le 11 Nov 2015
A = [4,3,1,0,2,3,5,6];
B = [1,3,2,1,0,2,5,6];
V = [-Inf,3:2:5,Inf];
[~,idx] = histc(A,V);
Anew = accumarray(idx(:),A,[],@mean);
Bnew = accumarray(idx(:),B,[],@mean);
Which creates these values:
>> Anew
Anew =
1.0000
3.3333
5.5000
>> Bnew
Bnew =
1.0000
2.0000
5.5000
The only thing you need to do is to define vector V of bin edges used in histc:
5 commentaires
Stephen23
le 13 Nov 2015
"Do you think averaging is a good solution for my case?" It depends on what you are trying to do with this data. It may be appropriate, or it may not. Unless you tell us something about "your case", then it is impossible to say if averaging might be appropriate.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!