How to find mean value for the row with the same variable?
7 views (last 30 days)
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
0 Comments
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
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
This is exactly what the function grpstats() does, if you have the Statistics and Machine Learning Toolbox.
4 Comments
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
See Also
Categories
Find more on Repeated Measures and MANOVA 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!