フィルターのクリア

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 件のコメント
jgg
jgg 2016 年 4 月 21 日
編集済み: jgg 2016 年 4 月 21 日
I do not get that error when I run your code, but it definitely does not sort a vector either. Have you posted the right code?
What was wrong with the sort function?
Mohannad Abboushi
Mohannad Abboushi 2016 年 4 月 21 日
I have to write a function without using the built in sort feature of matlab. Also that's what I get when I use it.

サインインしてコメントする。

採用された回答

Jos (10584)
Jos (10584) 2016 年 4 月 21 日
You seem to use the variable n in two ways:
  1. an unsorted variable (vector? or array?)
  2. 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
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 ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by