Average of every n elements of two separate vector

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 Comment

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?

Sign in to comment.

 Accepted Answer

You can use histc and accumarray:
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 Comments

Thanks for your reply. What does 3:2:5 meaning in V. because I have 1000 data, how should I deal with that?
If you don't know what 3:2:5 does then you need to learn basic MATLAB operations and syntax. These tutorials are an excellent place to start:
Your question was not clear about how you want to group the values of A: it seems that you want to group them based on value, where that value increments is steps of two. This is how we make a vector starting from three, incrementing in steps of two, until five:
>> 3:2:5
ans =
3 5
>> [-Inf,3:2:5,Inf]
ans =
-Inf 3 5 Inf
histc uses this vector to define its bin edges (and as is described in the documentation. Read it).
"how should I deal with that?" how do you want to split the data values up? The general format will be like this:
V = [-Inf, lowest_bin_edge:increment:highest_bin_edge, Inf]
Try it, do some experimentation:
>> [-Inf,10:10:50,Inf]
ans =
-Inf 10 20 30 40 50 Inf
NOTE: this is certainly not the only solution, it is just the one that I came up with to match your incomplete description of how the data should be split up. If you could tell us the range of the values in A then we might have a better solution.
If the lowest values in A is zero or one, then you could try this:
N = 2; % increment per group
idx = max(1,ceil(A/N))
Anew = accumarray(idx(:),A,[],@mean)
Bnew = accumarray(idx(:),B,[],@mean)
Thank you so much for your explanation.
In my case, A and B come from two separate data and when I plot it (please find attached), for each value of A there might be 1, 2 ,3 or 4 value of B.
As shown in figure, A starts from 173, and has decrease/increase/decrease/increase trend.
The problem is that, for A=173, there should be 4 values of B, but from my data I have only two exact 173, and the others are close to 173. and then sum up the values of B for A=173. (At A=173, Bnew=(B1+B2+B3+B4), for example)
So I asked you about how to make average of that vector. Do you think averaging is a good solution for my case?
any suggestion? regards
"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.

Sign in to comment.

More Answers (0)

Asked:

on 11 Nov 2015

Commented:

on 13 Nov 2015

Community Treasure Hunt

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

Start Hunting!