【问题标题】:Unexpected matlab operator? [duplicate]意外的matlab运算符? [复制]
【发布时间】:2015-11-30 23:36:07
【问题描述】:

我正在编写代码(使用 MatLab)来生成切比雪夫多项式。当 n 等于 0 和 1 时,此代码运行良好。但是对于 n=2 和更大,它给了我这个错误:

错误使用 * 内部矩阵维度必须一致。

myCheb 中的错误(第 13 行) T(1,x) = 2*x*myCheb(n-1,x)-myCheb(n-2,x);

感谢您帮助解决这个问题。

我的代码如下:

function [ T ] = myCheb( n,x )

out = zeros (1,size(x,2));

for i= 1:x
    if n==0
        T(1,x) = 1;
    elseif n==1
        T(1,x) = 2*x;
    else

        T(1,x) = 2*x*myCheb(n-1,x)-myCheb(n-2,x);
    end

end
out = T(1,x);
end 

【问题讨论】:

  • 参见:mtimestimes
  • 输入示例:[T] = myCheb(2, 1:3)

标签: matlab


【解决方案1】:

在 Matlab 中,* 运算符定义矩阵乘法。

尝试将第 13 行更改为

T(1,x) = 2.*x.*myCheb(n-1,x)-myCheb(n-2,x); (注意星号之前的句号) 这将逐个元素进行乘法

http://au.mathworks.com/help/matlab/ref/times.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2011-03-17
    • 2015-12-11
    相关资源
    最近更新 更多