You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: bbnnls.m
    path of file: ./repos/encode/external
    
    the code of the file until where you have to start completion: function out = bbnnls(M, b, x0, opt)
% function out = bbnnls_New(A, b, x0, opt)
% 
% This is a modified version of BBNNLS code originally written by Suvrit Sra, Dongmin Kim
% This version accept as parameter a factorization of matrix A which is a
% structure M containing:
%       1) The Dictionary M.DictSig;
%       2) A sparse 3D array M.Phi with size [nFibers,nAtoms,Nvoxels]

% BBNNLS   -- Solve NNLS problems via SBB
% 
% WARNING Use at own risk!
% NOTE --- guaranteed co
    
function and gradient --- requires good implementation of A*x
% and A'*y for appropriate x and y
function [f g] = funcGrad(A, b, x)    
    [nFibers] = size(A.Phi,3); %feGet(fe,'nfibers');
    [nTheta]  = size(A.DictSig,1);
    [nAtoms] = size(A.DictSig,2); %feGet(fe,'natoms');
    [nVoxels] = size(A.Phi,2); %feGet(fe,'nvoxels');    

    %Ax = M_times_w(A,x) - b;
    Ax = M_times_w(A.Phi.subs(:,1),A.Phi.subs(:,2),A.Phi.subs(:,3),A.Phi.vals,A.DictSig,x,nTheta,nVoxels)- b;
    
    f = 0.5*norm(Ax)^2;
    if (nargout > 1)
%     g = A'*Ax;
%     g =
     g = Mtransp_times_b(A.Phi.subs(:,1),A.Phi.subs(:,2),A.Phi.subs(:,3),A.Phi.vals,A.DictSig,reshape(Ax,[nTheta,nVoxels]),nFibers);% MEX Intel compiler version     
    end