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?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2020-2-18
"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
采纳的回答
ME
2020-2-18
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.
更多回答(1 个)
Bhaskar R
2020-2-18
编辑:Bhaskar R
2020-2-18
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 个评论
Bhaskar R
2020-2-18
" 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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!