Why does times(A,B) gives me negative values when A and B don't have any ???

Hello, i'm working with this code, which is a non-negative factorization :
function [G] = NN_Update_G(G_0,S,H,V,nb_iter,beta)
mustBePositive(G_0);
G=G_0;
if beta<1
gamma_beta=1/(1-beta);
elseif 1<=beta && beta <=2
gamma_beta = 1;
else
gamma_beta = 1/(beta-1);
end
G_old=G;
for k=1:nb_iter
mustBePositive(G_old);
product = (((S'*(((S*G_old*H).^(beta-2)).*V)*H'))./(S'*((S*G_old*H).^(beta-1))*H')).^gamma_beta;
mustBePositive(product);mustBePositive(G_old);
G = times(G_old,product);
mustBePositive(G);
G_old = G;
end
end
I don't know how but only the last mustBepostive(G) is triggered. It's the only one, which would mean that both G and product are positive beforehand.
I really hope someone can make some sense out of this because it's really bothering me not understanding what's the problem...

7 commentaires

Torsten
Torsten le 19 Juin 2025
Modifié(e) : Torsten le 19 Juin 2025
We can't run your code because the inputs to the function "NN_Update_G" are missing.
I can't really put it here because it's inside a bigger function using lots of data /:
Two real, numeric and positive values multiplied usually give a real, numeric and positive value. So if you can't supply the inputs to the function, I suggest to inspect the values for "product", "G_old" and "G".
Yes that's what I thought, sorry I can't supply the inputs, gonna look into it and if I find something useful I'll post it here so that if anyone else got that it will help them .
Thanks for your help nonetheless
Matt J
Matt J le 19 Juin 2025
Modifié(e) : Matt J le 19 Juin 2025
I can't really put it here because it's inside a bigger function using lots of data /:
You can use a breakpoint to pause code execution where the malfunctioning code starts. You can then save all the necessary input variables to a .mat file and post it here.
Benjamin
Benjamin le 19 Juin 2025
Modifié(e) : Benjamin le 19 Juin 2025
Thanks for that, didn't know I could put parameters into a file like that (:
BUT, I tried but all the parameters (even alone) are too heavy to be included here /: (Apparently some files are attached so tell me if it works I guess. PS : it's just one matrice that's too heavy, so it's missing H. I'll try to find another way to send it)
for nb_iter I use 5 because it's in another loop but feel free to try anything
I use 0 for beta
If you apply
load ("G_zero.mat")
G_0(G_0<=0)
G_0(G_0<0)
you will see that your G_0 has 0 negative elements, but 148 zero elements.

Connectez-vous pour commenter.

 Réponse acceptée

Try using mustBeNonnegative instead of mustBePositive. This will detect if your calculation underflowed to 0.
x = realmin
x = 2.2251e-308
mustBePositive(x) % true so no error
y = x*x % underflow
y = 0
mustBeNonnegative(y) % true so no error
mustBePositive(y) % false
Value must be positive.

Plus de réponses (1)

dpb
dpb le 19 Juin 2025
Déplacé(e) : dpb le 19 Juin 2025
mustBePositive(0)
Value must be positive.
shows that zero is not considered positive by mustBePositive. You don't give any klews as to what the magnitudes of inputs are, but one must surmise that one or more of the elements must have underflowed owing to a large exponent in the product expression.

Catégories

Produits

Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by