# How to evaluate mean for column

조회 수: 1 (최근 30일)
Karolina 2015년 5월 5일
댓글: Karolina 2015년 5월 6일
I have a data with 5 columns: X,Y,Z,A,B, the first three columns represent coordinates, the next ones some attributes of objects.
A column has values: 0 or 1, B columns has values: 1, 1, 1, 2, 2, 3, 3, ,3 ,3 , 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, ....
I would like to evaluate the mean value of A column for every individual value in column B. Eg. Evaluate mean value from column A but only for points which in B have value 1.
Does somebody know which function should I use to do this? Thank you in advance!

댓글을 달려면 로그인하십시오.

### 채택된 답변

You can use arrayfun:
dx=zeros(100,1);
dx(randi(100,10,1))=1;
B=cumsum(dx);
A=(1:100).^2;
meanvals=arrayfun(@(x) mean(A(B==x)),unique(B));
##### 댓글 수: 9이전 댓글 7개 표시이전 댓글 7개 숨기기
values=unique(B);
nums=histc(B,values);
small=nums>5 & out<1.3;
large=nums>5 & out>=1.3;
sl=small+2*large;
In sl you should have what you want. Plese see that I used "nums" as variable name as "Size" is pretty close to "size" and that's a Matlab function. Better don't name variables like this to prevent errors from the beginning.
Karolina 2015년 5월 6일
Super! Thank you very much! Have a nice day!

댓글을 달려면 로그인하십시오.

### 추가 답변 (2개)

Andrei Bobrov 2015년 5월 6일
[~,~,c] = unique(B);
out = accumarray(c(:),A(:),[],@mean);
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Ahmet Cecen 2015년 5월 5일
Below does the example you listed, change the value 1 to do the others.
mean(A(B==1))
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Karolina 2015년 5월 5일
Thank you for your answer. Do you know if there is some way to do this for all values in my B column automatically? In my B column I have values from "1" to "n" (I do not know the biggest value) and for every value I would like to have the mean from A column.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by