Calculate mean of numeric column vector based on repeated rows in a string array
조회 수: 1 (최근 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.
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!