Delete outliers in each column of a matrix
이전 댓글 표시
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!
답변 (2개)
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)
z(r,:) = [] % delete rows which contain z>3 in any of the column
Image Analyst
2021년 7월 19일
0 개 추천
Why don't you just use the build-in isoutlier() function?
카테고리
도움말 센터 및 File Exchange에서 Hypothesis Tests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!