필터 지우기
필터 지우기

Perform this operation on a scalar/vector or matrix

조회 수: 6 (최근 30일)
atan
atan 2019년 3월 26일
댓글: atan 2019년 3월 27일
I have a code like this
The function takes z as argument. z can be a scalar or vector or matrix
IF it is a vector, I want the operation stated to be performed on every element of a vector, if its a matrix I want the operation to be performed on every element of a matrix and if its a scalar on the scalar
It is not working. See below
1) If I give z as scalar 0. I get 0.5 which is correct
2) If I give z as 0.5, I get 0.6225 which is correct
3) I give as z as [0;0;0.5], I expect the answer to be 0.5 0.5 0.6225 but it doesn't work
function g = sigmoid(z)
%SIGMOID Compute sigmoid function
% g = SIGMOID(z) computes the sigmoid of z.
% You need to return the following variables correctly
g = zeros(size(z));
g = (1/(1+exp(-z)));

채택된 답변

Stephan
Stephan 2019년 3월 26일
편집: Stephan 2019년 3월 26일
g = (1./(1+exp(-z)));
% ^
% |______ perform elementwise
  댓글 수: 2
madhan ravi
madhan ravi 2019년 3월 26일
Also
g = zeros(...) % line is unnecessary
atan
atan 2019년 3월 27일
Thanks !
This works:
g = (1./(1+exp(-z)));
I know that "." does the magic to perfoirm the element wise operation. I'm still unsure on where exactly one should insert that "."
Thanks !

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by