How to remove repeat entities in a matlab array
Mostra commenti meno recenti
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
il 10 Nov 2020
Modificato: James Tursa
il 10 Nov 2020
unique(your_variable)
or
unique(your_variable,'stable')
9 Commenti
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:
Et.B200
il 10 Nov 2020
James Tursa
il 10 Nov 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
il 11 Nov 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
il 11 Nov 2020
@Stephen: Good catch! I totally missed that.
Steven Lord
il 11 Nov 2020
@Stephen: I missed that as well.
Categorie
Scopri di più su Language Fundamentals in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
