Convert negative and out of defined range data to NaN

13 vues (au cours des 30 derniers jours)
Niklas Weichbold
Niklas Weichbold le 21 Nov 2021
Hello, my problem is that I have a matrix of 9 columns and want the quality of the data shown in a special range (as shown in "lo"=minimum size and "hi"=maximum size). If data is not in this range it should be shown as NaN.
My problem is that only data with the size 0(Zero) is shown as NaN, but not the ones outside my defined range (not even the negative data).
lo = [0.01 2.4 192 15 4 3 140 0 1];
hi = [1.4996 17.1 238 114 11 34 180 100 16];
for i=1:9
isnan(num(:,i)<lo(i));
isnan(num(:,i)>hi(i));
end
Thanks in advance!

Réponse acceptée

DGM
DGM le 21 Nov 2021
Modifié(e) : DGM le 21 Nov 2021
You should be able to do something like this:
lo = [0.01 2.4 192 15 4 3 140 0 1];
hi = [1.4996 17.1 238 114 11 34 180 100 16];
A = randi([0 200],5,9)
A = 5×9
61 193 30 100 126 162 15 153 159 49 32 200 88 159 49 151 146 48 88 28 197 89 122 40 114 113 177 56 58 49 50 179 83 136 125 145 160 148 3 190 118 200 65 15 185
A(A<lo | A>hi) = NaN
A = 5×9
NaN NaN NaN 100 NaN NaN NaN NaN NaN NaN NaN 200 88 NaN NaN 151 NaN NaN NaN NaN 197 89 NaN NaN NaN NaN NaN NaN NaN NaN 50 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 15 NaN
This sort of implicit array expansion should work in R2016b and newer versions.
That's a lot of NaNs, but bear in mind that the random matrix used in the example isn't exactly tailored to stay within bounds.
  4 commentaires
DGM
DGM le 21 Nov 2021
% test array
A = randi(9,10,10);
A(A<3) = NaN
A = 10×10
9 6 9 4 8 5 NaN 4 3 6 6 8 3 9 NaN 3 6 6 4 4 7 9 5 3 5 7 5 NaN 4 NaN NaN NaN 5 4 7 7 6 NaN 9 3 4 NaN NaN 8 NaN 8 6 3 6 8 3 3 9 9 NaN 7 4 6 5 7 7 NaN NaN 9 NaN 7 4 NaN 9 6 3 4 NaN 7 NaN 4 5 8 4 9 3 4 4 NaN 6 3 NaN 9 8 8 7 NaN 4 NaN 7 7 8 3 NaN 3
B = A(~isnan(A(:,1)),:) % remove rows
B = 9×10
9 6 9 4 8 5 NaN 4 3 6 6 8 3 9 NaN 3 6 6 4 4 7 9 5 3 5 7 5 NaN 4 NaN 4 NaN NaN 8 NaN 8 6 3 6 8 3 3 9 9 NaN 7 4 6 5 7 7 NaN NaN 9 NaN 7 4 NaN 9 6 3 4 NaN 7 NaN 4 5 8 4 9 3 4 4 NaN 6 3 NaN 9 8 8 7 NaN 4 NaN 7 7 8 3 NaN 3
nanspercol = sum(isnan(B),1);
B = B(:,nanspercol<=size(B,1)/4) % remove columns
B = 9×7
9 4 5 NaN 4 3 6 6 9 3 6 6 4 4 7 3 7 5 NaN 4 NaN 4 8 8 6 3 6 8 3 9 7 4 6 5 7 7 9 7 4 NaN 9 6 3 7 4 5 8 4 9 3 NaN 3 NaN 9 8 8 7 NaN 7 8 3 NaN 3
Niklas Weichbold
Niklas Weichbold le 21 Nov 2021
Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by