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.
2 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Image Analyst
on 7 Apr 2013
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 Comments
Image Analyst
on 7 Apr 2013
Edited: Image Analyst
on 7 Apr 2013
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
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!