Clear Filters
Clear Filters

How do i find rows that contain duplicate values only?

3 views (last 30 days)
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]

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Author Block Masks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!