索引必须是实数正整数或逻辑。 &安培;&安培;试图访问f(0,0); index必须是正整数或逻辑

时间:2014-05-18 11:26:02

标签: matlab

我是来自c ++的matlab新手,并尝试进行2d卷积。我做了干运行,但似乎没有出错但我不知道为什么会这样 我的简单逻辑,如果值小于等于零,则置零。我正在使用这个表达式来解决这个问题

enter image description here

我在

面临2个错误

1)在f(q,p)=零(q,p);

2)在output_args(x,y)= output_args(x,y)+(W(s,t)* f(q,p));

function output_args = convo(img,k )
 //skipped the portion of code---

z=s;i=t;
    for x=1:row
        for y=1:col
           for s=s:a+2
              % disp(s);
               for t= t:b+2
                   q=z-s;
                   p=i-t;
                   if q<=0
                       if p <=0
                           f(q,p)=0; %// trying to place zero at index so help needed which is 1st error i have said
                       end
                   end
           %        disp(f(q,p));
                   output_args(x,y)=output_args(x,y) + (W(s, t)* f(q,p));  %//2nd error is comin (as i have told you above.

               end
           end
     w=w+1;   
        end
        z=z+1;
    end

end

在控制台:(我申请时出错

1) Subscript indices must either be real positive integers or logicals at f(q,p)=0

2) Attempted to access f(0,0); index must be a positive integer or logical.  output_args(x,y)=output_args(x,y) + (W(s, t)* f(q,p));

所以任何想法?

1 个答案:

答案 0 :(得分:2)

if q<=0
    if p <=0
        f(q,p)=0; % p and q will always be invalid!
    end
end

嗯,那是在乞求那个错误。在MATLAB中,指数必须大于零。虽然您可以反转if条件以确保索引是正数,但这将改变代码的修改。

if q > 0
    if p > 0
        f(q,p)=0; % p and q will always be valid.
    end
end

如果您需要从-5到5进行索引(例如),则可以改为从1到11进行索引。请记住,每次显示值时都会减去6。或者,存储&#34;真实&#34;另一个向量中的索引。