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 (최근 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
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
Kim Arnold
Kim Arnold 2020년 2월 18일
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!

댓글을 달려면 로그인하십시오.

채택된 답변

ME
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.
  댓글 수: 4

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bhaskar R
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
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by