基於sp++ matlab sign 符合函式實現
阿新 • • 發佈:2018-12-15
sign(整數)=1; sign(負數)=-1; sign(零)=0;
向量版
Vector<Type>sign(Vector<Type> a) { Vector<Type> p(a.size()); for (int i = 0; i < a.size(); i++) { if (a[i] > 0) p[i] = 1; else if (a[i] < 0) p[i] = -1; else p[i] = 0; } return p; }
矩陣版
Matrix<Type>sign(Matrix<Type> a) { Matrix<Type> p(a.dim(0), a.dim(1)); for (int i = 0; i < a.dim(0); i++) {
for (int j = 0; i < a.dim(1); j++)
{
if (a[i][j] > 0) p[i][j] = 0; if (a[i][j] < 0) p[i][j] = -1; else p[i][j] = 0; } } return p; }