Cell array gets updated with NaN values after the first run
4 views (last 30 days)
Show older comments
I have a dataset of dimension 32x32x1000 that I converted to a 1000x1 cell array with 32x32 matrices in each cell. I run a function to approximate two new cell arrays with the function run on each of the 1000 matrices in the array but after the first cell every celi gets updated with a 32x32 matrix with NaN values. Is there an error in my code which prevents the consecutive updates. Also I am a bit doubtful of my application of the nested loop. Given below is the line that runs the function:
[L25_1,S25_1,f25_1] = tnn_admm(H25_1,1e-5,1.5,100);
And the following code represents the function tnn_admm:(along with two additional functions required to run it)
function [L,S,f_time] = tnn_admm(X,eps,rho,max_iter)
L = cell(size(X));
S = cell(size(X));
Y = cell(size(X));
L{1} = zeros(size(X{1}));
S{1} = zeros(size(X{1}));
Y{1} = zeros(size(X{1}));
U = cell(size(X));
V = cell(size(X));
A = cell(size(X));
B = cell(size(X));
k = length(X);
mu_1 = 0.25/mean(X{1}(:));
mu_m = 10*mu_1;
tic
for i = 1:k
[m,n] = size(X{i});
lam = 1/sqrt(max(m,n));
[U{i},~,V{i}] = svd(X{i});
A{i} = U{i}.';
B{i} = V{i}.';
mu = zeros(k,1);
mu(1) = mu_1;
iter = 1;
while iter<max_iter
iter = iter+1;
% Calculate L
L{i+1} = svt((X{i} - S{i} + (1/mu(i))*(Y{i}-A{i}.'*B{i})),mu(i));
% Calculate S
S{i+1} = shrink(X{i} - L{i+1} + (Y{i}/mu(i)),lam/mu(i));
% Calculate Y
Y{i+1} = Y{i} + mu(i)*(X{i}-L{i+1}-S{i+1});
% Update mu
mu(i+1) = min(rho*mu(i),mu_m);
l_err = norm(L{i+1}-L{i},'fro');
s_err = norm(S{i+1}-S{i},'fro');
if (l_err<=eps) && (s_err<=eps)
break;
end
end
% [L,S] = admm(X{i},A_l,B_l,max_iter,eps,lam,mu_0,mu_m,rho);
% X{i+1} = L{i+1}+S{i+1};
% if (norm(X{i+1}-X{i},'fro')<=eps)
% break;
% end
end
f_time = toc;
end
function E = shrink(s,tau)
E = sign(s)*(max(0,abs(s)-tau)-max(0,-abs(s)-tau));
end
function W = svt(Q,mu)
[U_q,S_q,V_q] = svd(Q);
mu_s = mu*eye(size(S_q));
S_m = max(S_q-mu_s,0);
W = U_q*S_m*V_q.';
end
I am curious about why the loop updates the cells after (2,1) to NaN values. Any input about my implementation of the algorithm is also welcome.
Thanks
2 Comments
Dyuman Joshi
on 7 Apr 2023
Edited: Dyuman Joshi
on 7 Apr 2023
Is H25_1 supposed to be the 32x32x1000 numeric array? If yes, then X{k} is not a valid call for a numeric array. If it is not a numeric array, please attach the data for H25_1.
And if you have to assign the same output to multiple variables, deal() would be a good option to make things compact.
[L,S,Y,U,V,A,B] = cell(size(X));
Accepted Answer
More Answers (1)
Cris LaPierre
on 7 Apr 2023
Edited: Cris LaPierre
on 7 Apr 2023
I see slightly different behavior than what you describe. There are a couple issues, but the one causing the NaNs is your value of mu in your while loop. Except for the first case, it is 0, and 1/0 is inf.
You can inspect your code further using the debugging tools in MATLAB.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!