finding the mean based on a specific value in other column

조회 수: 24 (최근 30일)
Najam us Saqib Fraz
Najam us Saqib Fraz 2020년 10월 9일
댓글: Jon 2020년 10월 9일
Guys I have following data as an example.The data contain 4 coloumns.want to average 4th coloumn when 1st couloumn is equal to 527.1235 and third coloumn is 927.5

채택된 답변

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 10월 9일
편집: Asad (Mehrzad) Khoddam 2020년 10월 9일
You can find the rows with the first condition and the other rows for the second condition. The intersection of the two rows, are the row numbers that satisfy both conditions:
rows = intersect(find(a(:,1)==527.1235), find(a(:,3)==927.5));
% average of the above rows
avg = mean(a(rows,4));
disp(avg)
or simpler :
rows = find(a(:,1)==527.1235 & a(:,3)==927.5);
% average of the above rows
avg = mean(a(rows,4));
disp(avg)
  댓글 수: 3
Najam us Saqib Fraz
Najam us Saqib Fraz 2020년 10월 9일
Thank You !! But I get NaN while use both the approaches !! The data I showed is a little part of my huge data...While importing it in Matlab and using your suggested solution it gives me NaN.....
Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 10월 9일
When data is missing in the data file, it shows as NaN. You can remove the lines that are NaN
use this option:
avg = mean(a(rows,4),'omitnan');

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

추가 답변 (2개)

madhan ravi
madhan ravi 2020년 10월 9일
편집: madhan ravi 2020년 10월 9일
ix = (abs(column_1 - 527.1235) < 1e-4) &...
(abs(column_1 - 927.5) < 1e-1);
M = mean(column_4(ix))

Jon
Jon 2020년 10월 9일
편집: Jon 2020년 10월 9일
Lets say you have put your data into an array X.
Find a logical index where the rows match your criteria using:
criteria = [527.1235 927.5]
idl = ismember(X(:,[1,3]),criteria,'rows')
then do the averaging on the 4th colulmn for the rows where the criteria matches
xMean = mean(X(idl,4))
  댓글 수: 5
Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 10월 9일
When data is missing in the data file, it shows as NaN. You can remove the lines that are NaN
Jon
Jon 2020년 10월 9일
I think you are trying to show that floating point comparisons could be a problem if they are not exact. I assumed they were exact, in any case given your example I get idl = 1 not 0
>> X = [527.1235 1.0000 927.5000],criteria =[527.1235 927.5000]
X =
527.1235 1.0000 927.5000
criteria =
527.1235 927.5000
>> idl = ismember(X(:,[1,3]),criteria,'rows')
idl =
logical
1

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by