필터 지우기
필터 지우기

Find the mean of values in a column when a condition is met

조회 수: 50 (최근 30일)
FortuitousMonkey
FortuitousMonkey 2018년 6월 13일
댓글: ssg 2021년 2월 8일
I am processing some data in matlab from excel.
The data is in matrix form (used read function).
I need to find the average of column c when column a value for the corresponding row is = to somevalue.
If I were to do this in excel I would use AVERAGEIF(A:A,=650,C:C) and it calculates the correct results. However I don't know how to replicate this statement in matlab.
Ideally the output would be in matrix form. Although I've attached a picture of excel data of a few words. The information I need to process is likely going to be 1000-3000 x 40 matrix.
Any thoughts?

채택된 답변

Star Strider
Star Strider 2018년 6월 13일
Try this:
M = randi(9, 10, 3) % Create Matrix
someValue = 3 % Define ‘somevalue’
C_mean = mean(M(M(:,1) == someValue, 3)) % Mean Of Column #3 For Column #1 = ‘somevalue’
  댓글 수: 4
Nev
Nev 2019년 5월 29일
편집: Nev 2019년 5월 29일
Hi, I have a similar problem but the "some value" needs to be a range so:
If column 1 is between value 1 and value 2 (e.g. 2.000 - 2.999) then average the values in column 2.
somevalue = 2.000:2.999; <-- this doesnt seem to work and I am not sure how to tell it to include all numbers inbetween.
Any help would be much appreciated!
ssg
ssg 2021년 2월 8일
I too have the same problem as mentioned by @Nev. Any suggestions about it ?

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

추가 답변 (1개)

sura Naji
sura Naji 2019년 10월 25일
l have amatrix contact from 1 colum and 1000 rows l want to find the mean and the standard deviation also l want to find the worst value of this because l use the particle swarm optimization

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by