making an array to skip missing data for mean calculations

7 ビュー (過去 30 日間)
Brian Harr
Brian Harr 2019 年 4 月 8 日
回答済み: Akira Agata 2019 年 4 月 8 日
Hello, this is my first time using Matlab.
I have several arrays of monthly data. I have figured out how to calculate the mean for necessary columns, but there are some entries of missing data that were entered as -999. These outliers skew an accurate mean calculation. I want to create a new array for each month that excludes each -999 entry and counts the number of entries that are excluded.
Here is my code so far
//creates a new array from the January array 8th column//
Jan = [January(:,8)];
i am getting an error "undefined operator '==' for input arguments of type 'cell'."
if Jan(:,1) == -999

回答 (1 件)

Akira Agata
Akira Agata 2019 年 4 月 8 日
If you want to calculate mean value for each column with ignoring -999 value, how about the following solution?
% Assuming your data is 100-by-8 and contains 10 '-999's
yourData = rand(100,8);
yourData(randperm(numel(yourData),10)) = -999;
% Replace -999 with NaN
idx = yourData == -999;
yourData(idx) = NaN;
% Calculate mean value for each column with setting 'omitnan' option
avg = mean(yourData,'omitnan');

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by