finding average of a percentile of rows in a matrix?

조회 수: 1 (최근 30일)
ARS
ARS 2012년 7월 26일
Hi All,
In the below given matrix, I wish to calculate the mean/average of first 20% rows and subtract it from the mean of the last 20% rows.
x =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
this is a sample matrix, so there can be 100 rows and 100 columns but I need to average the values in the first 20% and subtract it from the average of last 20%. No sorting is needed in my matrix. therefore, in the above matrix, I would need to average the first two rows and subtract the resultant from the average of the last two rows.
any help will be appreciated.
Regards,
AMD.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 7월 26일
편집: Azzi Abdelmalek 2012년 7월 26일
n=size(x,1);n1=round(n/5);row_mean=mean(x(n-n1+1:end,:))-mean(x(1:n1,:))
result_mean=mean(row_mean)
% you can use the result: row_mean wich is a line % or the result: result_mean which is the mean of the line row_mean
  댓글 수: 2
ARS
ARS 2012년 7월 30일
Hi Azzi,
Sorry for being late. Couldn't reply due to illness.
My data has NaNs and the answer results in a NaN. How can I avoid NaNs in this calculation.
Regards,
AMD.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 7월 30일
can we replace them with 0?

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

추가 답변 (2개)

Wayne King
Wayne King 2012년 7월 26일
편집: Wayne King 2012년 7월 26일
Assume A is your matrix.
numrows = round(0.2*size(A,1));
lastrowstart = size(A,1)-numrows+1;
lowermean = mean(A(1:numrows,:),2);
uppermean = mean(A(lastrowstart:end,:),2);
uppermean-lowermean
Or did you mean take the mean over all elements in the bottom 20% of rows and upper 20% of rows, so you end up with a single number?
If that is the case:
numrows = round(0.2*size(A,1));
lastrowstart = size(A,1)-numrows+1;
lowermean = mean(mean(A(1:numrows,:),2));
uppermean = mean(mean(A(lastrowstart:end,:),2));
uppermean-lowermean
  댓글 수: 5
Oleg Komarov
Oleg Komarov 2012년 7월 30일
Use nanmean instead of mean.
ARS
ARS 2012년 7월 31일
Thanks.

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


Azzi Abdelmalek
Azzi Abdelmalek 2012년 7월 30일
편집: Azzi Abdelmalek 2012년 7월 30일
%replacing nan by zero
x(find(isnan(x)))=0 % add this to replace nan by zero
% the previous code
n=size(x,1);n1=round(n/5);row_mean=mean(x(n-n1+1:end,:))-mean(x(1:n1,:))
result_mean=mean(row_mean)
%in case you want calculate the mean, ignoring nan , that means: if i have [1 3 nan 4]; the mean will not (1+3+0+4)/4, but (1+3+4)/3. in this case add this code
ind=arrayfun(@(y) ~isnan(y),x)
x(find(isnan(x)))=0;
n=size(x,1);n1=round(n/5);
row_mean1=sum(x(n-n1+1:end,:))./sum(ind(n-n1+1:end,:))-sum(x(1:n1,:))./sum(ind(1:n1,:))
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2012년 7월 30일
if you dn't want replace nan by 0, what do you want to do?

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by