unique values inside a matrix

Hello,
Assuming i have a matrix like this m=
1 30
1 20
1 20
1 20
2 30
2 28
2 28
... ...
i want it to be like this:
1 30
1 20
2 30
2 28
PS: THE MATRIX IS HUGE CONSISTING OF 11000 ROWS AND 9 COLUMNS
unique is not working since it has it's taking a unique value of all, for example if i apply it for the second column it will take 28 for 1 and not for 2
thank you in advance

 Respuesta aceptada

Star Strider
Star Strider el 21 de Sept. de 2019
Editada: Star Strider el 21 de Sept. de 2019
Try this:
A = [1 30
1 20
1 20
1 20
2 30
2 28
2 28];
Out = unique(A, 'rows', 'stable')
producing:
Out =
1 30
1 20
2 30
2 28
Experiment to get different results.

9 comentarios

Michel tawil
Michel tawil el 21 de Sept. de 2019
"1 74
1 73
1 72
1 70
1 69
1 67
1 65
1 63
1 61
1 58
1 57
1 55
1 53
1 51
1 50
1 49
1 48
1 47
1 46
1 45
1 44
1 43
1 42
1 42
1 41
1 41
1 40
1 32
1 31
1 30
1 30
1 29
1 28
1 28
1 27
1 26
1 26
1 25
1 24
1 24
1 23
1 22
1 21
1 21
1 20
1 20
1 20
1 20
1 20"
after applying it, nothing changed
thanks
Star Strider
Star Strider el 21 de Sept. de 2019
It changes when I run it:
A = [1 74
1 73
1 72
1 70
1 69
1 67
1 65
1 63
1 61
1 58
1 57
1 55
1 53
1 51
1 50
1 49
1 48
1 47
1 46
1 45
1 44
1 43
1 42
1 42
1 41
1 41
1 40
1 32
1 31
1 30
1 30
1 29
1 28
1 28
1 27
1 26
1 26
1 25
1 24
1 24
1 23
1 22
1 21
1 21
1 20
1 20
1 20
1 20
1 20];
Out = unique(A, 'rows', 'stable')
produces:
Out =
1 74
1 73
1 72
1 70
1 69
1 67
1 65
1 63
1 61
1 58
1 57
1 55
1 53
1 51
1 50
1 49
1 48
1 47
1 46
1 45
1 44
1 43
1 42
1 41
1 40
1 32
1 31
1 30
1 29
1 28
1 27
1 26
1 25
1 24
1 23
1 22
1 21
1 20
With ‘A’ being (49x2) and ‘Out’ being (38x2).
If nothing truly changes, we may not be getting all the information we need to have.
Michel tawil
Michel tawil el 21 de Sept. de 2019
for example the last 20 values are rounded to 20, not exactly 20, could it be a reason?
Star Strider
Star Strider el 21 de Sept. de 2019
You may need to use uniquetol instead. See specifically the section on Perpare Vectors for Exact Comparison, since the 'stable' argument is not supported, and 'rows' becomes 'ByRows',1.
Star Strider
Star Strider el 21 de Sept. de 2019
Using uniquetol with your .mat file:
D = load('C.mat');
A = D.C(:,1:2)
[Out,ia,ic] = uniquetol(A, 0.1, 'ByRows',1)
OutStable = A(ia,:)
produces:
A =
1 74
1 73
1 72
1 70
1 69
1 67
1 65
1 63
1 61
1 58
1 57
1 55
1 53
1 51
1 50
1 49
1 48
1 47
1 46
1 45
1 44
1 43
1 42
1 42
1 41
1 41
1 40
1 40
1 32
1 31
1 30
1 30
1 29
1 28
1 28
1 27
1 26
1 26
1 25
1 24
1 24
1 23
1 22
1 21
1 21
1 20
1 20
1 20
1 20
1 20
2 85
2 84
2 82
2 80
2 78
2 77
2 75
2 73
2 72
2 71
2 69
2 68
2 67
2 66
2 65
2 63
2 62
2 61
2 60
2 58
2 57
2 56
2 54
2 53
2 52
2 51
2 50
2 49
2 48
2 47
2 46
2 45
2 44
2 44
2 43
2 42
2 42
2 41
2 33
2 33
2 33
2 32
3 91
3 89
3 87
3 85
3 83
3 80
3 78
3 75
OutStable =
1 20
1 30
1 40
1 50
1 61
1 72
2 32
2 42
2 52
2 62
2 72
2 82
3 75
3 85
Experiment to get the result you want.
Michel tawil
Michel tawil el 21 de Sept. de 2019
yes thanks but probably the unique/uniquetol is working on the other colums not only on the first two?
Star Strider
Star Strider el 22 de Sept. de 2019
Sure!
The code changes to:
D = load('C.mat');
C = D.C;
[Out,ia,ic] = uniquetol(C(:,[1 2]), 0.1, 'ByRows',1);
OutStable = C(ia,:)
producing:
OutStable =
1 20 568 28.4 1 27.719 63.087 27.719 63.087
1 30 755 37.75 1 27.719 63.087 27.719 63.087
1 40 810 40.5 1 27.719 63.087 27.719 63.087
1 50 890 44.5 1 27.719 63.087 27.719 63.087
1 61 1005 50.25 1 27.719 63.087 27.719 63.087
1 72 1108 55.4 1 27.719 63.087 27.719 63.087
2 32 784 39.2 1 27.719 63.087 27.719 63.087
2 42 831 41.55 1 27.719 63.087 27.719 63.087
2 52 904 45.2 1 27.719 63.087 27.719 63.087
2 62 983 49.15 1 27.719 63.087 27.719 63.087
2 72 1020 51 1 27.719 63.087 27.719 63.087
2 82 1068 53.4 1 27.719 63.087 27.719 63.087
3 75 1040 52 1 27.719 63.087 27.719 63.088
3 85 1106 55.3 1 27.719 63.087 27.719 63.088
This is the shortg format option, since it’s easier to read (and post). The full precision remains internally.
Michel tawil
Michel tawil el 22 de Sept. de 2019
Editada: Michel tawil el 22 de Sept. de 2019
Thank you!
Star Strider
Star Strider el 22 de Sept. de 2019
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 21 de Sept. de 2019
I'm not sure what you mean by unique is not working. You haven't explained the reasoning for going from your input to your output, but it does look like you want unique rows:
result = unique(m, 'rows')
With your demo data:
>> m = [
1 30
1 20
1 20
1 20
2 30
2 28
2 28];
>> unique(m, 'rows')
ans =
1 20
1 30
2 28
2 30
>> unique(m, 'rows', 'stable') %with stable to get the same order as your example
ans =
1 30
1 20
2 30
2 28

5 comentarios

Michel tawil
Michel tawil el 21 de Sept. de 2019
I applied it, it looks the same as mr.star's answer, for some reason it didn't work.
if u look in the end of the answer i gave to him, u can see that I have 5 of 20s but they weren't orignally 20s, I applied the command 'round' before ( round(19.8)->20), could it be a reason of not detecting it ?
Guillaume
Guillaume el 21 de Sept. de 2019
The simplest way for us to understand what you're talking about is to attach a mat file with your data to your question.
>> unique([20; round(19.8)])
ans =
20
As you can see it's not a problem unique returns just one value. Whatever your data is, it's not exactly how you describe it.
Possibly, it's something like:
>> v = [20; 20+1e-14]
v =
20
20
>> unique(v) %return 2 elements because the 2nd one is not exactly 20.
ans =
20
20
>> ans - 20
ans =
0
1.06581410364015e-14
Michel tawil
Michel tawil el 21 de Sept. de 2019
hello, please find it attached here, i noticed that other columns could have non-unique values but i want the unique function to be just applied for all the rows but for columns 1 and 2 ? idk if this is possible
Michel tawil
Michel tawil el 21 de Sept. de 2019
i can use this and it works well but i lose the cotents of the other colums
M=unique(C(:,1:2),'rows','stable');
Michel tawil
Michel tawil el 22 de Sept. de 2019
Thank you !

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 21 de Sept. de 2019

Comentada:

el 22 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by