How to remove repeat entities in a matlab array

I have an array with 17 values within it, I have only one number than appears twice, i want the new identity to have 16 values in it, only including this perticular repeat value only once in the new identity

Respuestas (1)

James Tursa
James Tursa el 10 de Nov. de 2020
Editada: James Tursa el 10 de Nov. de 2020
unique(your_variable)
or
unique(your_variable,'stable')

9 comentarios

Et.B200
Et.B200 el 10 de Nov. de 2020
Editada: Et.B200 el 10 de Nov. de 2020
It still outputs as a 17 valued array :(
input: infected is the array being counted to display that there is 17 values, value outputs are set to an array called amount. Want infected to only include 16 values
amount = numel(infected);
unique(amount)
display(['The amount of people infected is: ',num2str(amount)]
output:
The amount of infected is: 17
James Tursa
James Tursa el 10 de Nov. de 2020
Editada: James Tursa el 10 de Nov. de 2020
Then you have 17 unique values. I would guess that the two values you think are the same are in fact slightly different. Subtract these two values and see what you get. If you want those two nearly equal values to be treated as the same then maybe you want the uniquetol( ) function:
Et.B200
Et.B200 el 10 de Nov. de 2020
Both repeat values are at a value of 53, whole numbers
James Tursa
James Tursa el 10 de Nov. de 2020
I rather doubt that unique( ) would miss that. What do you get when you directly subtract these two elements?
The values that are displayed may not be exactly the same as the values that are stored.
format short
x = [1, 1+1e-13]
x = 1×2
1.0000 1.0000
x(1) == x(2) % false
ans = logical
0
The two elements of x are not the same, but in the short display format they look the same. Therefore asking for the unique elements gives them both.
y = unique(x) % two elements
y = 1×2
1.0000 1.0000
If you want to treat them as "close enough" to one another, consider using uniquetol.
z = uniquetol(x)
z = 1
Et.B200
Et.B200 el 11 de Nov. de 2020
I have this when I equate values 9 and 12 together, which are the positions of my repeat value.
Stephen23
Stephen23 el 11 de Nov. de 2020
Editada: Stephen23 el 11 de Nov. de 2020
The code you show in your comment:
amount = numel(infected);
unique(amount)
does not call unique on the data array (as you should), instead you call unique on the output from numel. But numel returns a single scalar value, and calling unique on that single value is going to return exactly the same scalar value, so there is absolutely no point in doing that.
What you should be doing is calling unique on the array of values, and then count how many are returned:
amount = numel(unique(infected));
fprintf('The number of people infected: %d\n',amount)
James Tursa
James Tursa el 11 de Nov. de 2020
@Stephen: Good catch! I totally missed that.
Steven Lord
Steven Lord el 11 de Nov. de 2020
@Stephen: I missed that as well.

Iniciar sesión para comentar.

Categorías

Más información sobre Language Fundamentals en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Nov. de 2020

Comentada:

el 11 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by