How to find mean value for the row with the same variable?
Show older comments
Hello everybody,
I have data 1000x2. In my second column the variables repeated. For example, between 1-20 row the value of 2nd column is 15. Between 20-25 is 14. Between 25-36 is 19 and so on. Just for the information the values in my first row is change regularly.
So what i need to do, is to find the mean value of the rows which contain same variable. For example, if i do it manualy for the first 20 row i will get,
(45.6,15). I want to do this for the whole data.
However there are 1 problem.
For example the value of 2nd column between 1-20 row is 15 but the value agais is 15 for the 220-225 rows. So I need a code that I can use but will not make a problem from this point of view.
Thanks
Answers (2)
darova
on 18 May 2020
Try this
k = 0;
i1 = 1;
A = [A; nan nan];
for i = 1:size(A,1)
if A(i,2)~=A(i+1,2) % search for an end of section of second column
k = k + 1; % number of mean value
M(k) = mean(A(i1:i,1)); % calculate mean
i1 = i; % new start of section
end
end
5 Comments
Ilkin Abdullayev
on 18 May 2020
Ilkin Abdullayev
on 18 May 2020
darova
on 18 May 2020
Try importdata
A = importdata('mathwork.csv',',');
Ilkin Abdullayev
on 19 May 2020
darova
on 20 May 2020
I think the problem occurs because of the way you are importing the data. Because of the data size actually. The excel file consists of two columns (nx2). What is the size when you imported the data?
Image Analyst
on 18 May 2020
0 votes
This is exactly what the function grpstats() does, if you have the Statistics and Machine Learning Toolbox.
4 Comments
Ilkin Abdullayev
on 18 May 2020
Image Analyst
on 19 May 2020
I'm sure you've done it already by now, but this is how I did it. Try it this way:
myTable = readtable('mathwork.csv');
means = grpstats(myTable, 'Var2')
If you don't want the average for every value in the second column, but in ranges, then you need to call histogram() instead. Specify the edges for the histogram that are whatever ranges you want.
Ilkin Abdullayev
on 20 May 2020
Edited: Steven Lord
on 20 May 2020
Image Analyst
on 20 May 2020
You could do a for loop
theMeans = zeros(1, 360);
for angle = 0 : 359
indexes = angleColumn >= angle & angleColumn < angle + 1;
theMeans(angle + 1) = mean(angleColumn(indexes));
end
Categories
Find more on Univariate Discrete Distributions 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!