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

Risposte (1)

James Tursa
James Tursa il 10 Nov 2020
Modificato: James Tursa il 10 Nov 2020
unique(your_variable)
or
unique(your_variable,'stable')

9 Commenti

Et.B200
Et.B200 il 10 Nov 2020
Modificato: Et.B200 il 10 Nov 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 il 10 Nov 2020
Modificato: James Tursa il 10 Nov 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:
Both repeat values are at a value of 53, whole numbers
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
I have this when I equate values 9 and 12 together, which are the positions of my repeat value.
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)
@Stephen: Good catch! I totally missed that.
@Stephen: I missed that as well.

Accedi per commentare.

Categorie

Scopri di più su Language Fundamentals in Centro assistenza e File Exchange

Richiesto:

il 10 Nov 2020

Commentato:

il 11 Nov 2020

Community Treasure Hunt

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

Start Hunting!

Translated by