How do I remove a set of repeated values in a x by 2 matrix?

For example,
Given the matrix:
matrix =
18 4
12 11
11 12
4 18
I want to get rid of the repeated values that happen after the first two rows to just get
matrix =
18 4
12 11

4 comentarios

MR
MR el 22 de Mzo. de 2020
Editada: MR el 22 de Mzo. de 2020
Additional info:
If I have the matrix
matrix =
8 2
7 3
5 5
3 7
2 8
I want to keep the 3rd row, but get rid of the last two rows. etc.
What if 1 number matches with other in a diffetent row?
MR
MR el 22 de Mzo. de 2020
if it matches with another in a different row then we are getting rid of it... the only time we wanna keep the number in the next column is if they're in the same row..
for example for this matrix
matrix =
23 2
22 4
21 6
19 8
14 14
8 19
6 21
4 22
2 23
I want to just reduce it down to
23 2
22 4
21 6
19 8
14 14
Hope that makes sense
Rik
Rik el 25 de Mzo. de 2020
In response to your flag ("I posted an unclear question and did not receive help"):
You did receive help. Why don't you try describing the question more clearly if you think that is the problem?

Respuestas (2)

Rik
Rik el 25 de Mzo. de 2020
I think the code below is what you need. There may be much more efficient methods, but this will do as long as this will not be the bottleneck in your workflow.
clc
v=[18 4
12 11
11 12
4 18];
remove_duplicate_rows(v)
v=[23 2
22 4
21 6
19 8
14 14
8 19
6 21
4 22
2 23];
remove_duplicate_rows(v)
function v=remove_duplicate_rows(v)
keeprows=false(size(v,1),1);
keeprows(1)=true;
for r=2:numel(keeprows)
%check if the values are already in the rows that we want to keep
%compare against numel(unique(v(r,:))) to catch repeated values
if numel(setdiff(v(r,:),v(keeprows,:)))==numel(unique(v(r,:)))
keeprows(r)=true;
end
end
v=v(keeprows,:);
end

La pregunta está cerrada.

Etiquetas

Preguntada:

MR
el 22 de Mzo. de 2020

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by