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?
Show older comments
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!
2 Comments
Stephen23
on 18 Feb 2020
"What is the easiest way to write a code for this?"
Not using loops.
>> idx = sum(abs(A(4,:))>abs(A([1:3,5],:)),1)<3
idx =
0 1 1 0
Kim Arnold
on 18 Feb 2020
Accepted Answer
More Answers (1)
mat = abs(your matrix say urmat);
ind = mat(4,:)>mat([1:3, 5], :);
ind_4 = ind(4,:);
desired_col = urmat(:, ind_4); % or mat(:, ind(4,:))
3 Comments
Kim Arnold
on 18 Feb 2020
Bhaskar R
on 18 Feb 2020
" it should give me a result like a=[0,1,1,0]" - As you mentioned thats why I had to perform < operation, now it is corrected.
Kim Arnold
on 18 Feb 2020
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!