Home > Back-end >  Arrays have incompatible sizes for this operation
Arrays have incompatible sizes for this operation

Time:04-29

function r = fun(x, params, type)
    h = @(x) x(1)-x(8);
    x= sym('x',[1,8]);

    gamma = 16;

    if type == 'linear'
        r = gamma*h(x);
    elseif type == 'sin'
        r = gamma*sin(h(x));
    end
    
end

When I run this function for type 'sin' I always get this error

Arrays have incompatible sizes for this operation.

Error in fun (line 7)
    if type == 'linear'

How to fix this?

I just one to pass type and depending on that create my ouput, I though string could be okay, but it doesn't work.

I want to multiply my function handle with gamma in case of linear and multiply and take sin in case of sine.

CodePudding user response:

You may mean

if strcmp(type,'linear')

type is a char array, and matlab does not support that type of direct array to array comparison for text. It tries to compare each letter with each other, but if length(type) is not the same as length('linear') it errors.

  • Related