Exponential approximation for vector input

4 次查看(过去 30 天)
I was double checking the behaviour of a sigmoid function used in my Simulink model and I noticed that I was getting incorrect approximations when I made the computation for a vector of values
vect = [-5.0000 -5.0000 -5.0000 1.0000 0.9000 0.8000 0.7000 -5.0000 -5.0000];
y_vect = 1/(1+exp(-2*(vect'-1)));
% Value calculated using the vector
y_vect(4)
ans = 0
% Value calculated alone
y_val = 1/(1+exp(-2*(vect(4)-1)))
y_val = 0.5000
This approximation in my case causes great confussion due to the magnitude of the quantity expected.
Is there any way to solve this?

采纳的回答

Sulaymon Eshkabilov
You have overlooked one dot. Here is the corrected commands:
vect = [-5.0000 -5.0000 -5.0000 1.0000 0.9000 0.8000 0.7000 -5.0000 -5.0000];
y_vect = 1./(1+exp(-2*(vect-1)));
% Value calculated using the vector
y_vect(4)
ans = 0.5000
% Value calculated alone
y_val = 1/(1+exp(-2*(vect(4)-1)))
y_val = 0.5000
  1 个评论
Eduardo
Eduardo 2023-2-1
Oh nice to know!
I wrongly thought the broadcasting would be done automatically since we just had a scalar in the numerator

请先登录,再进行评论。

更多回答(1 个)

Voss
Voss 2023-1-31
vect = [-5.0000 -5.0000 -5.0000 1.0000 0.9000 0.8000 0.7000 -5.0000 -5.0000];
Using / (matrix right division), as you have it now:
y_vect = 1/(1+exp(-2*(vect'-1)));
disp(y_vect)
1.0e-05 * 0.6144 0 0 0 0 0 0 0 0
Using ./ (element-wise right division):
y_vect = 1./(1+exp(-2*(vect'-1)));
disp(y_vect)
0.0000 0.0000 0.0000 0.5000 0.4502 0.4013 0.3543 0.0000 0.0000

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by