MATLAB: How to select certain columns of a matrix only when the values in the 4th row are bigger than three values of the other four rows in that particular column

select certain columns of a matrix using if else condition

Hi everybody.
I have a Matrix with 5 rows and 27 columns M(5,27).
-465016.671990511 -7739.99191635794 -341535.491595863 -85371.1969301907
-5903.99881269675 -7859.03942441580 1327.06121931661 -2689.99261151688
-9775.84903413543 -24436.8700789172 10631.1822509721 -5457.55830920490
-115990.998278705 -14946.7388544833 -102.707785969593 -22516.0893903143
-49215.4580227008 -42337.1765610354 -22878.6399931591 -17776.5021930945
looks like above. I don't want the columns (using 0 index) where the absolute value in the fourth row is bigger than at least 3 absolute values of the other 4 values in the particular column (values in row 1:3 and 5). From the above shown columns it should give me a result like a=[0,1,1,0] because only in the first and fourth column the values in the 4th row are bigger than at least three other values in that particulare column ( first column: 4th value 115990 > 465016, 5903, 9775, 49215; fourth column: 4th value 22516> 2689, 5457, 17116)
What is the easiest way to write a code for this? Using the if else statement? Thanks already in advance!

Best Answer

For the example in your question, the following will work:
[~,c] = size(A);
a = ones(1,c);
for col = 1:c
if((sum(abs(A(4,col)) > abs(A([1:3,5],col)))) >=3)
a(col) = 0;
end
end
You don't need an else statement if you pre-allocate the a array as being all ones.