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?

3 vues (au cours des 30 derniers jours)
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 commentaires
Stephen23
Stephen23 le 18 Fév 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
Kim Arnold le 18 Fév 2020
Thank you very much for this short line of code, i thought there is no need for loops but as a beginner i really appreciate any help!

Connectez-vous pour commenter.

Réponse acceptée

ME
ME le 18 Fév 2020
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.
  4 commentaires

Connectez-vous pour commenter.

Plus de réponses (1)

Bhaskar R
Bhaskar R le 18 Fév 2020
Modifié(e) : Bhaskar R le 18 Fév 2020
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 commentaires
Bhaskar R
Bhaskar R le 18 Fév 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by