Delete outliers in each column of a matrix

1 view (last 30 days)
Hi!
I have a matrix in which each line relates to a trial and each column is a specific moment I am analysing. I want to get the zscore for each column and, when it is > 3, I want to delete all the trial.
So, if I have a zscore > 3 in the position (120,3) of the matriz, I would delete all the 120th line ( all the values in (120,1); (120,2);(120,3) and (120,4)) .
I tried to use the following code but for some reason it is giving me the error: A null assignment can have only one non-colon index.
Error in NameOfTheScript > removeOutliers (line 72)
inicialVec(i,1) = [ ];
The code I'm trying to use is the following one (I'm using it in a function because I need to apply it a few times to different matrixes:
% %The inicial matrix I'm using its appended to this question
function finalMatrix = removeOutliers(inicialMatrix)
z = abs(zscore(inicialMatrix, 0, 1));
column1 = inicialMatrix(:,1);
column2 = inicialMatrix(:,2);
column3 = inicialMatrix(:,3);
column4 = inicialMatrix(:,4);
z1 = abs(zscore(column1, 0, 1));
z2 = abs(zscore(column2, 0, 1));
z3 = abs(zscore(column3, 0, 1));
z4 = abs(zscore(column4, 0, 1));
for i = 1:size(inicialMatrix,1)
if (z1(i) > 3 || z2(i) > 3 || z3(i) > 3 || z4(i) > 3)
inicialMatrix(i,1) = [];
inicialMatrix(i,2) = [];
inicialMatrix(i,3) = [];
inicialMatrix(i,4) = [];
end
end
finalMatrix = inicialMatrix;
end
Thank you in advance!

Answers (2)

Mohammad Sami
Mohammad Sami on 19 Jul 2021
You can simplify your code like this.
% generate test data
initmatrix = rand(100,100);
initmatrix(12,99) = 1000;
initmatrix(100,20) = 1000;
% actual code
z = abs(zscore(initmatrix,0,1));
r = any(z>3,2);
find(r)
ans = 2×1
12 100
z(r,:) = [] % delete rows which contain z>3 in any of the column
z = 98×100
0.4486 1.6351 0.8696 1.5466 1.2569 0.3750 0.6603 1.4480 0.5331 1.6135 0.4029 0.1502 1.3191 1.5485 1.3806 1.9509 0.7581 1.2962 0.2115 0.0948 1.1977 0.7170 0.7267 1.8464 1.4979 1.1862 0.0955 1.3534 0.7246 0.0227 1.3435 0.5270 0.7838 0.4420 0.8637 1.6983 0.7365 1.5835 1.7832 1.1995 0.8098 0.3783 0.6114 0.7289 1.3684 0.5199 0.1479 1.6011 1.1674 0.1008 1.6536 0.9117 0.2892 0.0628 0.8930 1.5835 1.6276 0.3462 1.2780 1.6239 1.3840 0.9861 0.6293 0.7125 1.1208 0.6858 1.0796 1.0425 0.3800 0.6624 0.4181 0.0222 1.6590 1.2461 1.1508 0.5898 0.3082 0.1390 0.0944 0.1030 0.2596 0.9520 1.4091 1.0283 0.3187 1.2428 0.7697 1.1105 0.5035 0.3883 0.9446 1.2221 0.5471 0.7634 0.1452 0.4226 0.8562 0.3720 1.6256 1.3693 1.1051 0.5899 1.6194 1.4136 0.8398 1.0892 1.6172 1.2361 1.5738 0.1038 0.8252 0.3551 0.0096 0.1220 1.2095 0.6092 1.1176 0.3401 1.5413 0.0468 0.7778 0.7851 1.0830 0.8929 0.8980 1.7098 0.5250 0.3496 0.6848 1.0156 0.3100 1.0994 0.5688 0.6123 1.0206 1.4735 1.1513 0.8435 1.4876 0.0977 0.3021 0.2839 1.1523 0.7655 1.6208 0.3270 1.0914 1.4994 0.0605 0.8266 0.5422 1.3897 1.1328 1.2168 0.6141 1.4902 0.3619 1.1045 0.1055 0.2017 1.1860 0.6501 0.6592 0.8164 1.7778 1.3261 0.4172 0.0283 1.2942 0.1023 0.5454 1.0562 1.1242 0.2077 1.1497 0.4685 0.3934 1.3274 1.7776 0.5990 1.3802 1.3604 0.9775 0.4382 0.9015 1.4396 1.3423 1.1144 1.0314 0.8868 1.6298 0.9499 1.0735 1.0541 1.1243 1.4728 0.3105 1.4822 0.7873 0.1030 1.8047 1.8457 1.2293 1.3497 1.0004 0.8491 1.3042 0.4379 0.1624 0.9576 1.1294 0.9827 0.5206 0.5442 0.9353 1.1303 0.2730 0.6589 1.8006 0.9030 0.8461 0.9939 1.3696 1.0230 0.6484 1.0853 0.3890 1.3706 1.5768 0.0992 0.0129 0.6445 0.9603 1.2165 1.6887 1.2893 1.9955 0.8172 0.2980 0.2460 0.4145 1.2895 0.1903 0.7916 0.2410 0.7043 1.6700 0.5196 1.3627 0.5251 0.8479 0.1703 0.0975 0.0517 1.1446 0.7058 1.0924 1.4912 0.2258 0.0979 1.6034 0.4135 1.3310 1.0421 0.4732 1.1292 1.1430 0.5628 0.3155 0.7439 1.5187 0.7845 0.5416 0.9036 1.0819 0.8406 0.7669 0.8888 0.3895 0.7547 1.3280 1.4624 0.0907 0.2329 1.3002 0.7398 1.5080 1.6224 0.4189 0.0986 1.8357 1.2939 0.4354 1.3778 1.3226 0.4811 0.3108 0.8923 1.1767 0.1574

Image Analyst
Image Analyst on 19 Jul 2021
Why don't you just use the build-in isoutlier() function?

Community Treasure Hunt

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

Start Hunting!