finding the mean based on a specific value in other column
Show older comments
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

Accepted Answer
More Answers (2)
madhan ravi
on 9 Oct 2020
Edited: madhan ravi
on 9 Oct 2020
ix = (abs(column_1 - 527.1235) < 1e-4) &...
(abs(column_1 - 927.5) < 1e-1);
M = mean(column_4(ix))
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 Comments
Wow, just while I was posting this two other answers appeared. So you have some choices. If I understand your problem correctly though, then this should be a pretty clean way to do it. I guess if you are just matching on two columns then there are many ways to do it. I think this approach scales well though if at some point you have a similar problem but with multiple columns (or whole rows) you want to match on.
madhan ravi
on 9 Oct 2020
>> X
X =
527.1235 1.0000 927.5000
>> criteria
criteria =
527.1235 927.5000
>> idl = ismember(X(:,[1,3]),criteria,'rows')
idl =
logical
0
>>
Najam us Saqib Fraz
on 9 Oct 2020
Asad (Mehrzad) Khoddam
on 9 Oct 2020
When data is missing in the data file, it shows as NaN. You can remove the lines that are NaN
Jon
on 9 Oct 2020
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
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!