Trying to remove zeroes from dataset to determine a proper min, max, mean and median

2 Ansichten (letzte 30 Tage)
%Tried using this method but completely gets rid of columns with zeroes
load generated_data.mat
colsWithZeros = any(X1==0)
colsWithZeros = 1×180 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
X1_glc = X1(:, ~colsWithZeros)
X1_glc = 2289×1
91.7600 91.7600 91.7600 91.7600 91.7600 91.7600 91.7600 91.7600 91.7600 91.7600
mean = mean(X1)
mean = 1×180
62.0394 62.3873 68.4067 60.3122 64.0506 49.8952 65.1440 65.4754 62.5643 63.9094 78.4804 49.9754 91.9750 67.2370 93.4761 67.3756 64.4858 83.4535 80.4765 64.4197 94.5063 65.0364 96.3659 60.2519 97.4069 97.4069 88.8258 63.0172 99.2272 99.2272
median = median(X1)
median = 1×180
89.2451 89.1466 89.3330 88.4463 89.9378 89.0360 90.0888 90.2249 88.9785 89.9283 91.7261 89.0596 91.7261 88.4692 91.7262 90.8759 89.9933 91.7250 91.7136 90.0067 91.7262 88.7749 91.7268 89.0138 91.7263 91.7263 91.7262 89.7431 91.7306 91.7306
max = max(X1)
max = 1×180
157.8314 164.6824 181.0046 152.7691 171.4886 157.3913 179.2350 178.5693 162.1231 173.8150 248.3298 157.3913 305.5951 179.6339 294.4348 183.3821 180.2525 302.6373 270.3745 177.8871 305.4929 174.4470 355.9559 143.7747 341.6236 341.6236 288.5927 170.6274 355.0885 355.0885
min = min(X1)
min = 1×180
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 15 Dez. 2021
You could replace the zeros with NaN, then use the 'omitnan' option in the mean, median, max and min functions.
load generated_data.mat
xX1 = X1;
xX1(xX1==0)=missing;
mean(xX1,"omitnan")
ans = 1×180
111.1175 111.9158 116.1595 109.2205 112.3463 98.9689 114.1767 114.2327 111.7080 113.3142 128.4990 98.9565 142.8296 117.6648 143.4093 116.2192 113.9829 137.8247 134.2644 112.6484 146.7604 113.0359 149.6482 108.1699 139.1788 139.1788 136.4579 111.9057 138.1576 138.1576
median(xX1,"omitnan")
ans = 1×180
99.5444 99.6217 99.9971 98.5570 98.5408 91.7533 98.7447 99.0285 99.2992 99.4590 104.7525 91.7536 106.2120 99.1072 107.6894 101.3442 99.0944 106.5169 104.0568 98.6323 107.6290 98.0593 109.1235 98.2417 102.6912 102.6912 107.1210 98.9206 100.8909 100.8909
max(xX1,[],"omitnan")
ans = 1×180
157.8314 164.6824 181.0046 152.7691 171.4886 157.3913 179.2350 178.5693 162.1231 173.8150 248.3298 157.3913 305.5951 179.6339 294.4348 183.3821 180.2525 302.6373 270.3745 177.8871 305.4929 174.4470 355.9559 143.7747 341.6236 341.6236 288.5927 170.6274 355.0885 355.0885
min(xX1,[],"omitnan")
ans = 1×180
87.8088 87.7950 86.3213 87.6592 87.7570 88.9939 86.9030 86.8868 87.4553 87.6818 82.7320 88.9939 74.6615 86.3000 81.1566 87.8140 87.6421 77.0906 84.6946 86.8836 75.6674 86.5045 79.5652 88.0240 67.5941 67.5941 78.0374 87.9315 66.8053 66.8053
By the way, you want to avoid naming your variables the same as MATLAB functions. That replaces the function with the variable, which will prevent you from using the function.
mean = mean(xX1);
% example of what happens when a varible replaces a MATLAB Function
mean2 = mean(X2;)
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
  2 Kommentare
Image Analyst
Image Analyst am 15 Dez. 2021
Exactly what I was going to suggest. So Nathaniel, could you click the "Accept this answer" if you think it solved your question to award Chris reputation points?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by