MatLab:错误检查行向量中的整数

标签 matlab

我已经使用 MatLab 为霍纳算法编写了以下代码

function [answer ] = Simple( a,x )
%Simple takes two arguments that are in order and returns the value of the
%polynomial p(x). Simple is called by typing Simple(a,x)
% a is a row vector
%x is the associated scalar value
n=length(a);
result=a(n);
for j=n-1:-1:1 %for loop working backwards through the vector a  
   result=x*result+a(j);
end
answer=result;
end

我现在需要添加错误检查以确保调用者在行向量 a 中使用整数值。

对于我以前使用的整数检查

if(n~=floor(n))
    error(...

但这是针对单个值的,我不确定如何对 a 中的每个元素执行此检查。

最佳答案

你有(至少)两个选择。

1) 使用任意:

if (any(n ~= floor(n)))
  error('Bummer. At least one wasn''t an integer.')
end

或者更简洁...

assert(all(n == floor(n)), 'Bummer. At least one wasn''t an integer.')


2) 使用功能更强大的validateattributes:

validateattributes(n, {'double'}, {'integer'})

这个函数也可以检查十几个其他的东西。

关于MatLab:错误检查行向量中的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20335881/

相关文章:

regex - matlab regexprep 具有多个数字的多个字符串

Matlab矩阵乘法计算有效数字

matlab - 在某些用户事件上暂停 Matlab 脚本或函数

matlab - 霍夫变换后检测相交线

python - 在 H5PY 中打开文件时出错(未找到文件签名)

Python3,将MATLAB数组代码转换为Python版本?

matlab - 通过 ssh 在 Windows 上远程启动 matlab?不可能的?

java - 如何在 Java 中提取 MFCC 特性

matlab - 破译在 http ://stackoverflow. com/a/13135833/560821 中找到的函数句柄解决方案

matlab - %#ok<SAGROW> 注释在 MATLAB 中是什么意思?