mean calculation by comparison
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
Thishan Dharshana Karandana Gamalathge
2015년 4월 25일
편집: Thishan Dharshana Karandana Gamalathge
2015년 4월 25일
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
2015년 4월 25일
편집: Star Strider
2015년 4월 25일
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
2015년 4월 25일
Well, I provided another answer, in another of your posts. Have a look on that too.
Star Strider
2015년 4월 25일
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
2015년 4월 25일
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)
