function [T,tim,suc,Dn,Un,Sn,Vn] = initialnmfCA4(U, S, V, z, k, n, s, calcp)

T = [];
tim = 0;
suc = 0;
Dn = [];
Un = [];
Sn = [];
Vn = [];

if rank(S) < s
    fprintf(2,'No NMF computed: Rank deficient.\n');
    return;
end

actpwd = pwd;
cd(calcp);

dlmwrite('wrap/allg.txt',[k;n;s;z]);
dlmwrite('wrap/L.txt',U*S(:,1:z),'precision','%15.15e','delimiter',' ');
dlmwrite('wrap/R.txt',V(:,1:z)','precision','%15.15e','delimiter',' ');
dlmwrite('wrap/tim.txt',0,'precision','%d');

arch = computer('arch');

if (strcmp(arch,'maci64') == 1) || (strcmp(arch,'maca64') == 1)
    syc = './AFScomputationMAC64.exe 0';
elseif strcmp(arch,'win32') == 1
    syc = 'AFScomputationWINDOWS32.exe 0';
elseif strcmp(arch,'win64') == 1
    syc = 'AFScomputationWINDOWS64.exe 0';
elseif strcmp(arch,'glnx86') == 1
    syc = './AFScomputationUNIX32.exe 0';
elseif strcmp(arch,'glnxa64') == 1
    syc = './AFScomputationUNIX64.exe 0';
else
    fprintf(2,'No executable version for this type of system.\n');
    set(gcf,'Pointer','arrow');
    cd(actpwd);
    return;
end

fprintf(' C routine (initial NMF) in progress ... (press CTRL + C to cancel) ... ');

T = eye(s,z);

if s < z
    dlmwrite('wrap/T0.txt',T(:,2:z),'precision','%15.15e','delimiter',' ');
    system(syc);
    tim = load('wrap/tim.txt');
    if tim(1) == 1
        results = load('wrap/InitNMF.txt');
        T = [ones(s,1) reshape(results(1:s*(z-1)),s,z-1)];

        Ct = U*S(:,1:z)*pinv(T);
        At = T*V(:,1:z)';

        D = Ct*At;
        [U,S,V] = svds(D,s);
        for i=1:s
            if -min(V(:,i)) > max(V(:,i))
                U(:,i) = -U(:,i);
                V(:,i) = -V(:,i);
            end
        end
        L = U*S;
        R = V';

        T = At*V(:,1:s);
        for i=1:s
            T(i,:) = T(i,:)/T(i,1);
        end;

        dlmwrite('wrap/allg.txt',[k;n;s;s]);
        dlmwrite('wrap/L.txt',U*S(:,1:s),'precision','%15.15e','delimiter',' ');
        dlmwrite('wrap/R.txt',V(:,1:s)','precision','%15.15e','delimiter',' ');
        dlmwrite('wrap/T0.txt',T(:,2:s),'precision','%15.15e','delimiter',' ');
        dlmwrite('wrap/tim.txt',0,'precision','%d');
        
        bothtimes = tim(2);
    else
        fprintf('computation canceled or failed.\n');
        return;
    end
    Un = U(:,1:s);
    Sn = S(1:s,1:s);
    Vn = V(:,1:s);
else
    Un = U(:,1:s);
    Sn = S(1:s,1:s);
    Vn = V(:,1:s);
    
    dlmwrite('wrap/T0.txt',T(:,2:s),'precision','%15.15e','delimiter',' ');
    
    bothtimes = 0;
end

system(syc);

tim = load('wrap/tim.txt');
if tim(1) == 1
    results = load('wrap/InitNMF.txt');
    
    T = [ones(s,1) reshape(results(1:s*(s-1)),s,s-1)];
    suc = 1;
    tim = bothtimes + tim(2);
    
    fprintf('ready, time: %2.2f s.\n',tim);
else
    fprintf('computation canceled or failed.\n');
    set(gcf,'Pointer','arrow');
end

cd(actpwd);
