find the reptition in vector
이전 댓글 표시
HI,I have x=[2 4 4 1 1],I want if the number is not repeated like 2 to execute a condition.If repeated ,to execute another condition.
댓글 수: 1
Image Analyst
2014년 4월 6일
편집: Image Analyst
2014년 4월 6일
So, for this x, are you going to execute the operation 1 or 2, or not? You have both unique and repeated numbers in this array, so which "condition" or operation are you going to execute????
채택된 답변
추가 답변 (1개)
Image Analyst
2014년 4월 6일
Is this what you want:
x=[2 4 4 1 1]
ux = unique(x)
counts = histc(x, ux) % Histogram
for k = 1 : length(ux)
checkValue = ux(k);
if counts(k) == 1
fprintf('Execute operation 1 because %d is unique.\n', checkValue);
else
fprintf('Execute operation 2 because %d is repeated.\n', checkValue);
end
end
In the command window:
x =
2 4 4 1 1
ux =
1 2 4
counts =
2 1 2
Execute operation 2 because 1 is repeated.
Execute operation 1 because 2 is unique.
Execute operation 2 because 4 is repeated.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!