'Arrays have incompatible sizes for this operation

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ander Biguri