I'm running median absolute deviation outlier analysis on a matrix B
(126 x 7). I thought my code below was working but I get an error about dimension mismatch. Can anyone help me? I'm still a beginner with MATLAB.
b = B; %identifying raw data for outlier analysis
k=size(B,2); %preallocating size of loop to run to be equivalent to number columns in B
for j= 1:k
a=b(:,j); %identify data used
fa = abs(a-median(a))./(mad(a,1)./.6745); %if x>3.5 then outlier
dec_mad=fa>2.24; %logical operator identifying outlier
nout(j)=sum(dec_mad); %sum of outliers
x = dec_mad ==0; % logical of data with outliers removed
b(:,j) = a(x); %data without outliers
end
This line is the issue:
b(:,j) = a(x);
If x
contains some zeros (i.e. outliers were found), the output of a(x)
is smaller than your original a
. Therefore you are trying to replace b(:,j)
- 126 values - with a(x)
- less than 126 values, hence dimension mismatch.
You could just replace any outliers with NaN
:
b(dec_mad,j)=NaN;
Or use a cell array which would allow your individual output columns to be different sizes.
out{j} = a(x);
Incidentally, it's best not to use j
for loops, it's a MATLAB built-in for complex numbers.