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.

7 views (last 30 days)
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])

Answers (1)

Image Analyst
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
Marco
Marco on 7 Apr 2013
Thank you for the tip, my problem is that I'm trying to use this function file on an array, how do I do that?
Image Analyst
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!