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

2 次查看(过去 30 天)
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.

采纳的回答

dpb
dpb 2022-7-13
编辑:dpb 2022-7-13
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...

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by