How do i find rows that contain duplicate values only?

3 次查看(过去 30 天)
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]

采纳的回答

OCDER
OCDER 2018-5-10
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 个评论
flemingtb
flemingtb 2018-5-10
This works but i'm struggling to understand why. Can you explain the code in line '3' and '4'?
OCDER
OCDER 2018-5-10
编辑:OCDER 2018-5-10
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Author Block Masks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by