MATLAB: How to compare elements with above and below element in a column

compare elementscompare elements in column

Hello
I have a matrix of 4 columns. I would like to compare each element in 4th column with previous and next element in the same column. If the difference is more than 50, the elemnt should turn to 0. (This is to eliminate unexpectedly high or low values inside 4th column. Other columns should not change at all)
My written code does not work, so I will be glad if some one can help in this. I expect the result look like this:
A = [15 2 7 36
20 3 14 41
25 4 21 0
30 5 28 22]
A = [15 2 7 36
20 3 14 41
25 4 21 567
30 5 28 22]
M = length (A(:,4)),
i = A(1,4) %first element of the column
k = A(M,4) %last element of the column
for T = 2:M-1
idx = A(T,4)
if idx> 'A(T+1,4)'+200
A(idx,4) = 0;
if idx < 'A(T-1,4)'-200
A(idx,4) = 0;
end
end
fname = sprintf('new.csv');
csvwrite (fname,A); % Write to CSV file
end

Best Answer

Is this what you want?
for T = 2:M
if( abs(A(T,4) - A(T-1,4)) > 50 )
A(T,4) = 0;
end
end