How can I group data points together
古いコメントを表示
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 件のコメント
Image Analyst
2020 年 12 月 2 日
Why did you delete your question?

Rik
2020 年 12 月 2 日
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
2020 年 12 月 2 日
Rena Berman
2021 年 5 月 6 日
(Answers Dev) Restored edit
回答 (1 件)
35 件のコメント
Matthew Suddith
2020 年 11 月 30 日
Cris LaPierre
2020 年 11 月 30 日
編集済み: Cris LaPierre
2020 年 11 月 30 日
For those of us not familiar with your data, how would we know what values to use?
Matthew Suddith
2020 年 11 月 30 日
Cris LaPierre
2020 年 11 月 30 日
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
2020 年 11 月 30 日
Matthew Suddith
2020 年 11 月 30 日
Cris LaPierre
2020 年 11 月 30 日
編集済み: Cris LaPierre
2020 年 11 月 30 日
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
2020 年 11 月 30 日
Matthew Suddith
2020 年 11 月 30 日
Matthew Suddith
2020 年 11 月 30 日
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
2020 年 11 月 30 日
Matthew Suddith
2020 年 11 月 30 日
Matthew Suddith
2020 年 11 月 30 日
Cris LaPierre
2020 年 11 月 30 日
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
2020 年 11 月 30 日
Cris LaPierre
2020 年 12 月 1 日
Do any of those header lines contain variable names identifying what is in each column?
Matthew Suddith
2020 年 12 月 1 日
Cris LaPierre
2020 年 12 月 1 日
Which line has that info? Line 364?
Matthew Suddith
2020 年 12 月 1 日
Cris LaPierre
2020 年 12 月 1 日
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
2020 年 12 月 1 日
Cris LaPierre
2020 年 12 月 1 日
編集済み: Cris LaPierre
2020 年 12 月 1 日
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
2020 年 12 月 1 日
Cris LaPierre
2020 年 12 月 1 日
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
2020 年 12 月 1 日
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
2020 年 12 月 1 日
Cris LaPierre
2020 年 12 月 1 日
編集済み: Cris LaPierre
2020 年 12 月 1 日
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
2020 年 12 月 1 日
Matthew Suddith
2020 年 12 月 1 日
Cris LaPierre
2020 年 12 月 1 日
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
2020 年 12 月 1 日
Cris LaPierre
2020 年 12 月 1 日
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
2020 年 12 月 2 日
Cris LaPierre
2020 年 12 月 2 日
カテゴリ
ヘルプ センター および File Exchange で Standard File Formats についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!