How to remove repeat entities in a matlab array
Mostrar comentarios más antiguos
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
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
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
el 10 de Nov. de 2020
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) == x(2) % false
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
If you want to treat them as "close enough" to one another, consider using uniquetol.
z = uniquetol(x)
Et.B200
el 11 de Nov. de 2020
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
el 11 de Nov. de 2020
@Stephen: Good catch! I totally missed that.
Steven Lord
el 11 de Nov. de 2020
@Stephen: I missed that as well.
Categorías
Más información sobre Language Fundamentals en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
