How to find mean value for the row with the same variable?

7 views (last 30 days)
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
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
Ilkin Abdullayev on 19 May 2020
You mean problem is the way that I import data? Maybe I confused you, I can import data the problem occur when I run the code for that data\
darova
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?

Sign in to comment.


Image Analyst
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
Ilkin Abdullayev
Ilkin Abdullayev on 20 May 2020
Edited: Steven Lord on 20 May 2020
Thanks a lot this also is helpfull. In my data one of the column is degree. For instance(3,1.5,2.5,6,......). I need a mean value after each 360 degree. Do you have any idea for this?
[SL: removed the underline formatting]
Image Analyst
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!