I have a function file that I worked out myself, all I need to do is apply it to an array of values but it's not working, I'd appreciate any help.
1 回表示 (過去 30 日間)
古いコメントを表示
Here is my function file so far
function x=isoddm(a)
if isnan(a)==1
x=-1;
elseif mod(a,2)==0.5
x=-1;
elseif mod(a,2)==1.5
x=-1;
elseif mod(a,2)==0
x=0;
elseif mod(a,2)==1
x=1;
end
All I need to do is apply it to an array. Here is an example array a=[7 4.5 nan] and its result isoddm([7 4.5 nan])
2 件のコメント
回答 (1 件)
Image Analyst
2013 年 4 月 7 日
You don't have a plain "else" condition and since none of the conditions that you do have are satisfied, none of the If/else blocks get entered, and so x never gets defined. Here, check out this helpful link: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
2 件のコメント
Image Analyst
2013 年 4 月 7 日
編集済み: Image Analyst
2013 年 4 月 7 日
Why not just put into a loop over k to calculate x(k) as a function of a(k):
for k = 1 : numel(a)
if isnan(a(k))==1
x(k)=-1;
elseif mod(a(k),2)==0.5
x(k)=-1;
elseif mod(a(k),2)==1.5
x(k)=-1;
elseif mod(a(k),2)==0
x(k)=0;
elseif mod(a(k),2)==1
x(k)=1;
else
x(k) = nan;
end
end
Regarding comparing to .5 and 1.5, see the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!