Calculate mean of numeric column vector based on repeated rows in a string array

2 Ansichten (letzte 30 Tage)
Hello, I have a string array (227293-by-3) consisting of weekly dates, latitude and longitude as in the following example:
"20101024 _ 20101031" "416" "826"
"20101024 _ 20101031" "416" "826"
"20101024 _ 20101031" "415" "826"
"20101024 _ 20101031" "415" "827"
"20101024 _ 20101031" "415" "827"
"20101024 _ 20101031" "415" "827"
"20101024 _ 20101031" "415" "827"
I need to calculate the mean of the values in a numerical column vector (227293-by-1) corresponding to the repeating identical rows in the previous string array.
The desired output as per the example would be a column vector:
[mean of rows 1 and 2
row 3
mean of rows 4 through 7...]
I could get the index of the beginning of unique rows in the string array but how could I use that or if there is another method.
Thanks for any assistance.

Akzeptierte Antwort

dpb
dpb am 13 Jul. 2022
Bearbeitet: dpb am 13 Jul. 2022
First, don't use a string array to hold the numeric data -- you don't show us how you got to the above, but if it's coming from a file, use readtable to import it instead of whatever you did...
Given that, then use
tD=array2table(data,"VariableNames",{'Date Range','Lat','Lon'});
tD.("Date Range")=categorical(tD.("Date Range"));
tD.Lat=str2double(tD.Lat);
tD.Lon=str2double(tD.Lon);
tD.SomeVariable=randi(100,size(tD.Lat))
tGrpMean=groupsummary(tD,{'Lat','Lon'},'mean','SomeVariable')
tD =
7×4 table
Date Range Lat Lon SomeVariable
___________________ ___ ___ ____________
20101024 _ 20101031 416 826 38
20101024 _ 20101031 416 826 60
20101024 _ 20101031 415 826 6
20101024 _ 20101031 415 827 44
20101024 _ 20101031 415 827 16
20101024 _ 20101031 415 827 84
20101024 _ 20101031 415 827 55
tGrpMean =
3×4 table
Lat Lon GroupCount mean_SomeVariable
___ ___ __________ _________________
415 826 1 6
415 827 4 49.75
416 826 2 49
>>
using the data above and working around the unfortunate choice of using string array...

Weitere Antworten (0)

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by