Zscore a matrix with NaN
68 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Joelle Martin
am 19 Okt. 2015
Beantwortet: jkr
am 21 Jun. 2017
I have matrix A, and I need to find a zscore value (with the intention of running a PCA) of a matrix that has NaN values. I need to have matlab ignore the NaNs while solving for the zscore value. How can I do this? I tried Zscore = zscore(~isnan(A)) but it didn't work.
Thanks for your help!
0 Kommentare
Akzeptierte Antwort
Star Strider
am 19 Okt. 2015
Bearbeitet: Star Strider
am 19 Okt. 2015
You can create your own function to compute the Z-scores omitting NaN values (I believe the 'omitnan' option began in R2014b). The result vector retains the NaN values while it computes the Z-score without them:
zscor_xnan = @(x) bsxfun(@rdivide, bsxfun(@minus, x, mean(x,'omitnan')), std(x, 'omitnan'));
q = rand(10,2);
q([3 5 7],1) = NaN;
Zscore = zscor_xnan(q);
1 Kommentar
Patrick
am 22 Jul. 2016
Bearbeitet: Patrick
am 22 Jul. 2016
can you perhaps elaborate a little bit. I'm new but here's what I had for the function (using MatLab 2015)
function zcor_xnan = @(x) bsxfun(@rdivide, bsxfun(@minus, x, mean(x,'omitnan')), std(x, 'omitnan'));
end
Here is what I have in my script
if true
% code
q = rand(10,2);
q([3 5 7],1) = NaN;
Zscore = zscor_xnan(q);
end
Really sorry I know this is a very infantile question but I rarely use functions.
Weitere Antworten (1)
jkr
am 21 Jun. 2017
A simple approach for a vector 'x':
zscore = (x - nanmean(x))/nanstd(x);
0 Kommentare
Siehe auch
Kategorien
Mehr zu NaNs finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!