Could someone please explain what is wrong in my function?
Mostrar comentarios más antiguos
I am attempting to create a fuction that takes in a matrix element and outputs with 1 0 or -1 depending on if th element is positive, zero or nagative respectively.
In the first line it underlines the first ( and , in the (A(r,c)) as well as both () around (r,c)
I am quite new so any help would be appreciated :)
function [Sign] = signum ( A(r,c) )
if A(r,c) == 0
Sign = 0;
elseif A(r,c) < 0
Sign = -1;
elseif A(r,c) > 0
Sign = 1;
end
disp(Sign);
end
Respuesta aceptada
Más respuestas (2)
David Hill
el 9 de Jun. de 2021
Why not just use built-in function sign()?
a=sign(A);
%then index into a for specific element
1 comentario
Sergey Popov
el 9 de Jun. de 2021
Jan
el 9 de Jun. de 2021
function [Sign] = signum(A)
The indices (r,c) belongs to the caller of the function, not inside the function. You call this function by:
signum(A(r,c))
Categorías
Más información sobre Database Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!