Creating variable averaged over two strata

조회 수: 2 (최근 30일)
aaron Harvey
aaron Harvey 2016년 3월 1일
편집: Mohammad Abouali 2016년 3월 2일
Hi there,
I have a two long variables (+40,000 measures) of doubles x & y and i would like to calculate a third variable from them z. However X and Y are measured over time and at various depths in the ocean, not always at the same exact depths so in order to calculate my z variable i need to do some matlab wizardry?? (I have the date in every format under the sun and also separate variable for the year,month and day seperately)
I would like a code that calculates monthly averages at certain depths.
For example to go from a matrix of [date, x, y, depth] which contain various NaNs to one which has 1:20:4000 depth in the first column repeated for every month (total of 350 months in my data set) and then corresponding monthly average for x and y.
Thank you for any help with this in advance
  댓글 수: 1
John BG
John BG 2016년 3월 1일
would it be possible to read a sample of the data?

댓글을 달려면 로그인하십시오.

채택된 답변

Mohammad Abouali
Mohammad Abouali 2016년 3월 1일
편집: Mohammad Abouali 2016년 3월 2일
first convert your date so that you get year and month separately. If you have the date as a string you can use datevec() command as follows:
dateVector = datevec(dateStr);
year = dateVector(:,1);
month = dateVector(:,2);
then you can use grpstats(). I suggest to create a table first, but you can also use a regular array.
dataTBL = table();
dataTBL.Year= year;
dataTBL.month= month;
dataTBL.depth = depth;
dataTBL.x = x;
dataTBL.y = y;
monthlyMean = grpstats(dataTBL, {'year',month','depth'},{@(c) (nanmean(c(:)))})
If you upload a sample data we can be of more help.
  댓글 수: 2
aaron Harvey
aaron Harvey 2016년 3월 1일
Thank-you, a very quick reply and it has helped me sort this out. However i now have a the probablem of multiplying by NaN in my data. How can i multiply two vectors containin NaN's such that.
a=[1 2 3 NaN 2 3]'
b=[3 2 NaN 2 1 5]'
a*b=[3 4 NaN NaN 2 15]'
Mohammad Abouali
Mohammad Abouali 2016년 3월 2일
편집: Mohammad Abouali 2016년 3월 2일
I am not sure what you are asking. If you are trying to multiply two vectors element by element you need to use .* instead of * (pay attention to the dot before *).
So it would be something like this
a=[1 2 3 NaN 2 3]'
b=[3 2 NaN 2 1 5]'
a.*b
ans =
3
4
NaN
NaN
2
15
If one of the element is NaN, then the multiplication results would be NaN.
Since you mentinoed your data has some NaN, earlier I suggested using nanmean instead of mean.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by