Creating variable averaged over two strata

1 回表示 (過去 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 Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by