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 件のコメント
Image Analyst
Image Analyst 2013 年 4 月 7 日
What does "oddm([7 4.5 nan])" mean?
Marco
Marco 2013 年 4 月 7 日
the [7 4.5 nan] part is the array I entered, the isoddm() around it is the function, it means that the array hasn't been evaluated

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

回答 (1 件)

Image Analyst
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 件のコメント
Marco
Marco 2013 年 4 月 7 日
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 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

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

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by