Check and eliminate rows based on conditions.

Hi. I have a matrix as below. I need help to code this problem.
A=
1 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 1
1 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
1. Therefore, I have to check every row in third column.
2. For every row in third column, I have to ensure that all previous rows have more value than current row value. If any previous row value in third column is less than current value, that previous row will be eliminated. Else, previous row will maintain.
3. For matrix A, as result, all rows except row 3, 10, 11, 12, and 13 will eliminate. End result after check and eliminate will produce matrix B as below.
B=
0 0 1 0 0 0 0
0 0 1 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 1 0
Thank you.

2 commentaires

Walter Roberson
Walter Roberson le 2 Août 2016
According to your rules, rows 11, 12, and 13 should be retained. The entry in the third column for those is 0, and no previous row has a value less than 0.
yue ishida
yue ishida le 2 Août 2016
Modifié(e) : yue ishida le 2 Août 2016
Thanks for your correction. You're right. I'm missing on row 11, 12, and 13 too. I already improved my question.

Connectez-vous pour commenter.

 Réponse acceptée

mask = A(:,3) == max(A(:,3));
A = A(mask,:);

5 commentaires

The above was a solution to the original example, but is not in itself a solution to the corrected example. For that,
mask = A(:,3) == max(A(:,3));
lastidx = find(mask, 1, 'last');
mask(lastidx+1:end) = 1;
A = A(mask,:);
yue ishida
yue ishida le 16 Août 2016
thank you for your contribution on correction
yue ishida
yue ishida le 4 Juil 2017
Modifié(e) : yue ishida le 4 Juil 2017
I want to ask, is similar code can apply to this matrix?
A =
[1.0000 -0.1367 0.0366 0.2431 0.1541 0.2263 0.3913 -0.1903
-0.1367 1.0000 -0.3278 -0.1718 -0.3803 -0.1378 -0.0839 0.2210
0.0366 -0.3278 1.0000 0.2087 0.4730 0.2077 0.0717 -0.1989
0.2431 -0.1718 0.2087 1.0000 0.2513 0.3382 0.5135 -0.3838
0.1541 -0.3803 0.4730 0.2513 1.0000 0.1547 0.0867 -0.1398
0.2263 -0.1378 0.2077 0.3382 0.1547 1.0000 0.5096 -0.4888]
1. Therefore, I have to check every row in third column.
2. For every row in third column, I have to ensure that all previous rows have more value than current row value. If any previous row value in third column is less than current value, that previous row will be eliminated. Else, previous row will maintain.
3. For matrix A, as result, all rows except row 3, 5, and 6 will eliminate. End result after check and eliminate will produce matrix B as below.
B =
0.0366 -0.3278 1.0000 0.2087 0.4730 0.2077 0.0717 -0.1989
0.1541 -0.3803 0.4730 0.2513 1.0000 0.1547 0.0867 -0.1398
0.2263 -0.1378 0.2077 0.3382 0.1547 1.0000 0.5096 -0.4888
Please explain your question. What are the expected results and what are the rules?
can we apply similar code to cell? the cell is like this. algorithm is same.
step2 =
'Q1' [1x117 char] [22] [0.8148]
'Q2' [1x86 char] [17] [0.6296]
'Q3' [1x57 char] [12] [0.4444]
'Q4' [1x86 char] [17] [0.6296]
'Q5' [1x67 char] [14] [0.5185]
'Q6' [1x113 char] [22] [0.8148]
This is algorithm needed.
1. Therefore, I have to check every row in third column.
2. For every row in third column, I have to ensure that all previous rows have more value than current row value. If any previous row value in third column is less than current value, that previous row will be eliminated. Else, previous row will maintain.
3. For cell step 2,, as result, all rows except row 1 (Q1) and last (Q6) will eliminate.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by