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
답변 (1개)
James Tursa
2020년 11월 10일
편집: James Tursa
2020년 11월 10일
unique(your_variable)
or
unique(your_variable,'stable')
댓글 수: 9
James Tursa
2020년 11월 10일
편집: James Tursa
2020년 11월 10일
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
2020년 11월 10일
James Tursa
2020년 11월 10일
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
2020년 11월 11일
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
2020년 11월 11일
@Stephen: Good catch! I totally missed that.
Steven Lord
2020년 11월 11일
@Stephen: I missed that as well.
카테고리
도움말 센터 및 File Exchange에서 Language Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
