comparison of loop and vectorization in matlab

后端 未结 4 680
轮回少年
轮回少年 2021-01-24 04:46

let us consider following code for impulse function

function y=impulse_function(n);
y=0;
if n==0
    y=1;
end
end

this code

>         


        
4条回答
  •  野的像风
    2021-01-24 05:15

    Your function is not defined to handle vector input.

    Modify your impluse function as follows:

    function y=impulse_function(n)
        [a b]=size(n);
        y=zeros(a,b);
        y(n==0)=1;
    end
    

    In your definition of impulse_function, whole array is compared to zero and return value is only a single number instead of a vector.

提交回复
热议问题