Average of every n elements of two separate vector

1 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2015-11-11
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?

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2015-11-11
编辑:Stephen23 2015-11-11
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 个评论
Amin Gan
Amin Gan 2015-11-11
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
Stephen23
Stephen23 2015-11-13
"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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by