Azzera filtri
Azzera filtri

How do i find rows that contain duplicate values only?

3 visualizzazioni (ultimi 30 giorni)
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]

Risposta accettata

OCDER
OCDER il 10 Mag 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 Commenti
flemingtb
flemingtb il 10 Mag 2018
This works but i'm struggling to understand why. Can you explain the code in line '3' and '4'?
OCDER
OCDER il 10 Mag 2018
Modificato: OCDER il 10 Mag 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Author Block Masks in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by