Why do I get this error "In an assignment A(:) = B, the number of elements in A and B must be the same"?
조회 수: 2 (최근 30일)
이전 댓글 표시
In this code I created a function to sort a vector from smallest to largest. It works when the vector has odd number of elements but not when it is even. Here's the code:
function[r] =vecsort(n)
r=zeros(size(n));
for i=1:n
[a,b]=min(n);
r(i)=a;
n=n([1:b-1 b+1:end]);
end
end
댓글 수: 2
채택된 답변
Jos (10584)
2016년 4월 21일
You seem to use the variable n in two ways:
- an unsorted variable (vector? or array?)
- the number of elements of something (for i=1:n)
I assume you want to change the second one.
Furthermore, min(A) will give a single value when A is a vector, but multiple values when A is a matrix ...
댓글 수: 1
James Tursa
2016년 4월 21일
Mohannad, for 2, you could change the for loop indexing to fix it for vector inputs. E.g.,
for i=1:numel(n)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!