How can I group data points together
Mostrar comentarios más antiguos
I need to "bin" data points together into one number. This file has 70,000 lines of data points, in unevenly spaced, often repeating increments. For example, I need to average all the different numbers (2.419, 2.417, 2.405, etc...) with decimals into 2.000.
4 comentarios
Image Analyst
el 2 de Dic. de 2020
Why did you delete your question?

Rik
el 2 de Dic. de 2020
I need to "bin" data points together into one number. This file has 70,000 lines of data points, in unevenly spaced, often repeating increments. For example, I need to average all the different numbers (2.419, 2.417, 2.405, etc...) with decimals into 2.000.
Matthew Suddith
el 2 de Dic. de 2020
Rena Berman
el 6 de Mayo de 2021
(Answers Dev) Restored edit
Respuestas (1)
35 comentarios
Matthew Suddith
el 30 de Nov. de 2020
Cris LaPierre
el 30 de Nov. de 2020
Editada: Cris LaPierre
el 30 de Nov. de 2020
For those of us not familiar with your data, how would we know what values to use?
Matthew Suddith
el 30 de Nov. de 2020
Cris LaPierre
el 30 de Nov. de 2020
I would think a better undertanding of what your data looks like would greatly faciliate proposing a solution. You can attach a sample of your data using the paperclip icon.
Absent that, I would encourage you to look at the documentation for groupsummary. You can average your data by groups you specify.
Matthew Suddith
el 30 de Nov. de 2020
Matthew Suddith
el 30 de Nov. de 2020
Cris LaPierre
el 30 de Nov. de 2020
Editada: Cris LaPierre
el 30 de Nov. de 2020
Ok, so for the solution I'm thinking about to work, i would need to create an "actual depth" column that would take your values and bin them to the corresponding actual value. Could you tell us what the actual depths should be? How much does the values in the file vary from the actual depths?
Any chance you can upload an actual text file of your data? I'm not feeling motivated enough to transcribe the values in the png. I believe the upload has to be less that 5MB, so you can delete some rows if necessary. It would be nice to see at least a couple of the different depths in the file.
Matthew Suddith
el 30 de Nov. de 2020
Matthew Suddith
el 30 de Nov. de 2020
Matthew Suddith
el 30 de Nov. de 2020
Ok, so now it's just about developing an algorithm for turning recorded depth into standard depths. Since no guidance has been given on how to do that, I defer to my original answer. Here's some sample code that uses the round function.
data = readtable("sample.txt");
data.Properties.VariableNames(1) = "Depth";
% Create groups by rounding the depths to integer values
data.grpDepth = round(data.Depth);
newData = groupsummary(data,"grpDepth","mean")
Matthew Suddith
el 30 de Nov. de 2020
Matthew Suddith
el 30 de Nov. de 2020
Matthew Suddith
el 30 de Nov. de 2020
Cris LaPierre
el 30 de Nov. de 2020
This likely means at least one of your depth values contains a value that cannot be rounded. Does one of the rows contain a non-numeric value? Does it work with a subset of the actual data?
Matthew Suddith
el 30 de Nov. de 2020
Cris LaPierre
el 1 de Dic. de 2020
Do any of those header lines contain variable names identifying what is in each column?
Matthew Suddith
el 1 de Dic. de 2020
Cris LaPierre
el 1 de Dic. de 2020
Which line has that info? Line 364?
Matthew Suddith
el 1 de Dic. de 2020
Cris LaPierre
el 1 de Dic. de 2020
There are 10 rows unaccounted for. This head has 354 rows of data, but you mentioned the numbers start at row 365. Are there blank rows between the header and the first row of numbers? Could you attach a subset of your data file containing the first 1000 rows including the header?
Matthew Suddith
el 1 de Dic. de 2020
Cris LaPierre
el 1 de Dic. de 2020
Editada: Cris LaPierre
el 1 de Dic. de 2020
There are numerous ways to do this, but probably the easiest to understand is this.
data = readtable("first1000.txt",'NumHeaderLines',354,"MultipleDelimsAsOne",true,"LeadingDelimitersRule","ignore");
data.Properties.VariableNames(1) = "Depth";
% Create groups by rounding the depths to integer values
data.grpDepth = round(data.Depth);
newData = groupsummary(data,"grpDepth","mean")
Matthew Suddith
el 1 de Dic. de 2020
Cris LaPierre
el 1 de Dic. de 2020
This has to be done manually. Using the names I see in the header, something like this at the end should do it.
newData.Properties.VariableNames(3:end) = ["depSM","sal00","t090C","CStarAt0",...
"flECO-AFL","sbeox0ML/L","sbox0Mm/Kg","sbeox0PS","oxsatML/L","oxsatMm/Kg",...
"par","turbWETbb0","prDM","sal11","t190C","T2-T190C","secS-priS","timeJ",...
"c0uS/cm","c1uS/cm","C2-C1uS/cm","density00","sigma-theta00","potemp090C",...
"v4","v3","v2","v5","flag"]
Cris LaPierre
el 1 de Dic. de 2020
WRT the questions asked here
I really don't have enough information to answer that question. What file are you comparing it to? How were the data grouped and averaged in that file?
You could try using a method other than round.
I'm not sure how using a for/while loop helps you here.
Matthew Suddith
el 1 de Dic. de 2020
Cris LaPierre
el 1 de Dic. de 2020
Editada: Cris LaPierre
el 1 de Dic. de 2020
Try using fix (assigns 2-2.9 a value of 2) or ceil (assigns 2.01-3 a vaue of 3) instead of round. It's a simple change to make to the code, and together form the 3 most likely methods used.
Matthew Suddith
el 1 de Dic. de 2020
Matthew Suddith
el 1 de Dic. de 2020
Cris LaPierre
el 1 de Dic. de 2020
Round, ceil, fix and floor all worked for me. I think you have a syntax error. I suggest reading the documentation I linked to previously to see how to use them. You should just have to replace "round" in the current code with "ceil", for example.
Yes, it is possible to use writetable to save a table to a text file. Again. read the documentation I linked to previously to see how to do it.
Matthew Suddith
el 1 de Dic. de 2020
Cris LaPierre
el 1 de Dic. de 2020
It looks like by default it writes a csv file. You could look at the name-value pairs for what options are available.
Matthew Suddith
el 2 de Dic. de 2020
Cris LaPierre
el 2 de Dic. de 2020
Categorías
Más información sobre Standard File Formats en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!