Effacer les filtres
Effacer les filtres

How do i find rows that contain duplicate values only?

3 vues (au cours des 30 derniers jours)
flemingtb
flemingtb le 10 Mai 2018
Modifié(e) : OCDER le 10 Mai 2018
for example i want to identify the row and column that contain "3" only and delete them from the data
x = [1 3 5 6; 2 3 6 4; 9 3 9 6; 3 3 3 3]

Réponse acceptée

OCDER
OCDER le 10 Mai 2018
x = [1 3 5 6; 2 3 6 4; 9 3 9 6; 3 3 3 3; 1 3 3 4]
B = (x == 3);
DelCol = sum(B, 1) == size(B, 1);
DelRow = sum(B, 2) == size(B, 2);
x(:, DelCol) = [];
x(DelRow, :) = [];
  2 commentaires
flemingtb
flemingtb le 10 Mai 2018
This works but i'm struggling to understand why. Can you explain the code in line '3' and '4'?
OCDER
OCDER le 10 Mai 2018
Modifié(e) : OCDER le 10 Mai 2018
B = (x == 3) returns a matrix. 1 is where x == 3
0 1 0 0
0 1 0 0
0 1 0 0
1 1 1 1
0 1 1 0
DelCol = sum(B, 1) == size(B, 1); Takes the sum of B downward along the row, then finds where the sum == the number of rows.
EX: If there are 5 3's and 5 rows, then that column is all 3's.
0 1 0 0
DelRow = sum(B, 2) == size(B, 2); same as above, but this find which rows are all 3's.
0
0
0
1
0
x(:, DelCol) = []; %Delete all columns where DelCol is 1
x(DelRow, :) = []; %Delete all rows where DelRow is 1

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Author Block Masks 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