mean calculation by comparison
显示 更早的评论
Hi, In the following vectors, I need to get the "mean" of y values for each set of similar x values. What I mean is I need the avg of Y for each group of x. I also need to save those mean Y values.
x=[3 3 3 4 4 5 5 5 5 3 11]; y=[1 2 4 5 7 1 1 8 10 10 1];
采纳的回答
Star Strider
2015-4-25
编辑:Star Strider
2015-4-25
This looks like an application for accumarray:
x=[3 3 3 4 4 5 5 5 5 3 11];
y=[1 2 4 5 7 1 1 8 10 10 1];
y_means = accumarray(x', y, [], @mean);
EDIT:
To get a summary of the x-values (first column) and the corresponding y_means values (second column:
xu = unique(x);
ys_means = [xu' y_means(xu)]
produces:
ys_means =
3 4.25
4 6
5 5
11 1
6 个评论
Thanks a lot. But what I meant is, I cannot combine the 1st set of number 3s with number 3 before 11. Also it is best if I can have just the mean values I get, rather also having zeros.
Also, without an "accumarray" command, how to do it. Assume I am not that familiar with MATLAB and X values can also be negative.
Thanks a lot. But still, you have gotten the averages of all 1,2,4 and 10 which are y values corresponds to 3s in x. But, you should not combine 10 relates to x at the end. Average of any ys corresponds to 3s at that place, should appear separately at there without combining with 3s at the beginning.
My pleasure.
I must have misunderstood your intent with ‘group’. To me, that meant grouping them even if they were not contiguous. The alternative to accumarray would likely be a series of loops. If x-values are negative, it would not be possible to use accumarray, without offsetting them temporarily to make them all positive integers.
If single isolated values are to be counted separately, they would have to be identified as such and then given separate designations in x, such as their corresponding index values. I did that here.
I don’t know how robust this would be (it works here), but it solves the isolated non-grouped index problem by assigning that value by its index (here, 10):
x=[3 3 3 4 4 5 5 5 5 3 11];
y=[1 2 4 5 7 1 1 8 10 10 1];
[xu,ia,ic] = unique(x);
xm = arrayfun(@isequal, repmat(unique(ic),1,length(ic))', repmat(ic,1,length(xu)));
xd = diff([zeros(1,size(xm,2)); xm]); % Find Transitions
iv = 1:size(y,2); % Index Vector
tpos = xd>0; % Positive Transitions
tneg = xd<0; % Negative Transitions
cntg = sum(tpos + circshift(tneg, [-1 0]),2); % Isolated Values = 2
x(cntg == 2) = iv(cntg == 2); % Set Isolated Values = Index
y_means = accumarray(x', y, [], @mean); % Accumulate ‘y’ By ‘x’
ys_means = [xu' y_means(xu)] % Isolate Non-Zeros
producing:
ys_means =
3 2.3333
4 6
5 5
10 10
11 1
The code is not as efficient as I would like it to be, but then this problem does not lend itself to concise solutions.
Thanks for your answer. Yes, this is not efficient as before. What I am expecting as the answer is following. In your latest answer also, 4th value of the 1st column should be 3. If I can get the following answer using accumarray and a few editions, I am okay with it. Now I understand the importance os accumarray command.
x_new y_new
3 2.33
4 6
5 5
3 10
11 1
Well, I provided another answer, in another of your posts. Have a look on that too.
I may not fully understand exactly what you want.
There is an Answer to your other, somewhat revised and updated version of this Question, that appears to be more efficient than my revised code, but it is not obvious to me how he would also supply the corresponding indices. Before delving into this myself, I’ll see what he comes up with.
Meanwhile, I will think about this and consider an entirely new effort and a new approach in light of your new requirements.
I do appreciate your already having Accepted my Answer.
Well, updated the answer, to include both start and end index of the block, as well as x-new.
As star strider mentioned, my answer on the other post is solving the same problem but using a different approach.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
产品
另请参阅
2015-4-25
2015-4-25
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
