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.
Afficher commentaires plus anciens
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])
Réponses (1)
Image Analyst
le 7 Avr 2013
0 votes
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 commentaires
Marco
le 7 Avr 2013
Image Analyst
le 7 Avr 2013
Modifié(e) : Image Analyst
le 7 Avr 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
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!