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];

 Akzeptierte Antwort

Star Strider
Star Strider am 25 Apr. 2015
Bearbeitet: Star Strider am 25 Apr. 2015

0 Stimmen

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 Kommentare

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.
Thishan Dharshana Karandana Gamalathge
Bearbeitet: Star Strider am 25 Apr. 2015
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
Mohammad Abouali
Mohammad Abouali am 25 Apr. 2015
Well, I provided another answer, in another of your posts. Have a look on that too.
Star Strider
Star Strider am 25 Apr. 2015
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.
Mohammad Abouali
Mohammad Abouali am 25 Apr. 2015
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by