Why does my vector repeat numbers?
조회 수: 1 (최근 30일)
이전 댓글 표시
So I am trying to sort a random array of integers and with my code everything works except certain numbers will repeat multiple times. I'm not sure what I am doing that's making it repeat but I think it has to do with me possibly overwriting my variable x(k).
function y = sort(x)
for k = 1:length(x)-1
% Compare x(k) with values in original array x
for i = (k+1):length(x)
if x(k) < x(i)
temp = 0;
temp = x(i);
x(k) = temp;
end
end
y = x;
댓글 수: 4
Stephen23
2015년 4월 28일
Also note that you should avoid using i and j as variable names, as these are both names of the inbuilt imaginary unit.
Chad Greene
2015년 4월 28일
Also be careful using i and j as variables. They're both built in as the imaginary unit. Overwriting them is usually not a problem, but when it is a problem, it can be hard to track down.
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!