简体   繁体   中英

Matlab/Octave: Function with array and number as argument -> subscript error

As Octave does not seem to bring a Gauss function for 1D or 2D by default I tried to define those functions by myself.

I started of with the definition

function [result] = Gauss(x,[x0 sigma])
result = 1/(sqrt(2*pi*sigma^2))*exp(-1(x-x0)^2/2*sigma^2)
end

which resulted in a subscript error

error: subscript indices must be either positive integers or logicals

I Tried to modify the function definition as follows:

function [result] = Gauss(x,[x0 sigma])

as I found a function gaussmf from Matlab, where you can pass an array x and single numbers in x0 and sigma. Unfortunately I still get a subscription error with that.

So my question is: How can I pass an array and additional non-array arguments in this case? I thought this would be rather automatic, but does not seem to be... ;)

Thanks

I suspect the error comes from:

exp(-1(x-x0)^2/2*sigma^2)

and I suspect you want

exp(-(x-x0)^2/2*sigma^2)

instead. As you have written it you are trying to index 1 with x-x0 . In fact, if x is a vector, you probably want:

exp(-(x-x0).^2/2*sigma^2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM