フィルターのクリア

Error : Output argument "y" (and maybe others) not assigned during call to "N".

4 ビュー (過去 30 日間)
Kiran Kumar Asokan
Kiran Kumar Asokan 2019 年 9 月 7 日
コメント済み: Walter Roberson 2019 年 9 月 8 日
I am currently working on building B spline curves basis function which is a recursive function. I am always getting this error Output argument "y" (and maybe others) not assigned during call to "N". Please let me know how to sort this.
function y = N(k,i,t,m)
%k is the order
%i is the pointer for the control point
%t (knot value) is the parameter used for defining a point on the curve
%m is the knot vector
% for the knot value t to be in between the ti and ti+k
if t < m(1,i+k+1)
if t > m(1,i+1)
if k == 1
y = 1;
end
for h = 2:1:k
y = ((((t - m(1,i+1))/ (m(1,i+h)- m(1,i+1)))* N(h-1,i,t,m)) + (((m(1,i+h+1) - t)/ (m(1,i+h+1)- m(1,i+2)))* N(h-1,i+1,t,m)));
end
else
y = 0;
end
end
end
  1 件のコメント
Stephen23
Stephen23 2019 年 9 月 8 日
"Please let me know how to sort this."
Use consistent code indentation.
Your code is very badly indented. Badly indented code is one way that beginners hide bugs in their code, by making the logic of the code hard to understand.
Make your code easier to understand by using consistent indexing (e.g. the MATLAB default indexing: select all code text, ctrl+i). Then the reason for your unassigned output is obvious.

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

回答 (2 件)

Star Strider
Star Strider 2019 年 9 月 7 日
The easiest way is to assign a value to ‘y’ at the outset. It will be re-assigned if the if blocks allow it.
Example —
function y = N(k,i,t,m)
y = NaN;
%k is the order
... rest of the function code ...
end
  1 件のコメント
Kiran Kumar Asokan
Kiran Kumar Asokan 2019 年 9 月 8 日
I am still getting random outputs. I have attached the required function below. Can anyone help me know whats wrong ?

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


Walter Roberson
Walter Roberson 2019 年 9 月 7 日
Reformat your code:
if t < m(1,i+k+1)
if t > m(1,i+1)
if k == 1
y = 1;
end
for h = 2:1:k
y = ((((t - m(1,i+1))/ (m(1,i+h)- m(1,i+1)))* N(h-1,i,t,m)) + (((m(1,i+h+1) - t)/ (m(1,i+h+1)- m(1,i+2)))* N(h-1,i+1,t,m)));
end
else
y = 0;
end
end
Notice that the else is associated with the inner if t > m(1,i+1) and so is not invoked in the case where t < m(1,i+k+1) is false. Therefore if t < m(1,i+k+1) is false, nothing is assigned to y.
  2 件のコメント
Kiran Kumar Asokan
Kiran Kumar Asokan 2019 年 9 月 8 日
I am still getting random outputs. I have attached the required function below. Can anyone help me know whats wrong ?
Walter Roberson
Walter Roberson 2019 年 9 月 8 日
Please explain further what you mean by "random outputs"

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

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by