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

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)

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

Thank you for the code.
I run it and instead of A i put "newtable" which is the name of my data and left the rest same.
But it gave me this error.
What could be the reasons?
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in correleationfor10cm (line 20)
data = [data; nan nan];
I am leaving a part of my data. you may notice what is the wrong.
Try importdata
A = importdata('mathwork.csv',',');
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\
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.

This is exactly what the function grpstats() does, if you have the Statistics and Machine Learning Toolbox.

4 Comments

Hi Image Analyst, Yes I have but i am not close with grpstats(), could you please give me some example code for the data that I have upload.
Because as far as I understood, if I want to use this function I need to know for example I need to specify the rows that I want to have mean value. However in my data I have more 200000 rows and I do not know the for example what is the value between 120220 and 120230.
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.
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]
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.

Asked:

on 18 May 2020

Commented:

on 20 May 2020

Community Treasure Hunt

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

Start Hunting!