[
  {
    "path": "machine-learning-ex1/ex1/computeCost.m",
    "content": "function J = computeCost(X, y, theta)\n%COMPUTECOST Compute cost for linear regression\n%   J = COMPUTECOST(X, y, theta) computes the cost of using theta as the\n%   parameter for linear regression to fit the data points in X and y\n\n% Initialize some useful values\nm = length(y); % number of training examples\n\n% You need to return the following variables correctly \nJ = 0;\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the cost of a particular choice of theta\n%               You should set J to the cost.\n\ntemp1=X*theta-y;\ntemp2=sum(temp1.^2);\nJ=1/(2*m)*temp2;\n\n%sprintf('%f',J);\n\n% =========================================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/computeCostMulti.m",
    "content": "function J = computeCostMulti(X, y, theta)\n%COMPUTECOSTMULTI Compute cost for linear regression with multiple variables\n%   J = COMPUTECOSTMULTI(X, y, theta) computes the cost of using theta as the\n%   parameter for linear regression to fit the data points in X and y\n\n% Initialize some useful values\nm = length(y); % number of training examples\n\n% You need to return the following variables correctly \nJ = 0;\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the cost of a particular choice of theta\n%               You should set J to the cost.\ntemp=(X*theta-y)'*(X*theta-y);\nJ=1/(2*m)*temp;\n\n\n% =========================================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/ex1.m",
    "content": "%% Machine Learning Online Class - Exercise 1: Linear Regression\n\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the\n%  linear exercise. You will need to complete the following functions \n%  in this exericse:\n%\n%     warmUpExercise.m\n%     plotData.m\n%     gradientDescent.m\n%     computeCost.m\n%     gradientDescentMulti.m\n%     computeCostMulti.m\n%     featureNormalize.m\n%     normalEqn.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n% x refers to the population size in 10,000s\n% y refers to the profit in $10,000s\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% ==================== Part 1: Basic Function ====================\n% Complete warmUpExercise.m \nfprintf('Running warmUpExercise ... \\n');\nfprintf('5x5 Identity Matrix: \\n');\nwarmUpExercise()\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% ======================= Part 2: Plotting =======================\nfprintf('Plotting Data ...\\n')\ndata = load('ex1data1.txt');\nX = data(:, 1); y = data(:, 2);\nm = length(y); % number of training examples\n\n% Plot Data\n% Note: You have to complete the code in plotData.m\nplotData(X, y);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =================== Part 3: Gradient descent ===================\nfprintf('Running Gradient Descent ...\\n')\n\nX = [ones(m, 1), data(:,1)]; % Add a column of ones to x\ntheta = zeros(2, 1); % initialize fitting parameters\n\n% Some gradient descent settings\niterations = 1500;\nalpha = 0.01;\n\n% compute and display initial cost\ncomputeCost(X, y, theta)\n\n% run gradient descent\ntheta = gradientDescent(X, y, theta, alpha, iterations);\n\n% print theta to screen\nfprintf('Theta found by gradient descent: ');\nfprintf('%f %f \\n', theta(1), theta(2));\n\n% Plot the linear fit\nhold on; % keep previous plot visible\nplot(X(:,2), X*theta, '-')\nlegend('Training data', 'Linear regression')\nhold off % don't overlay any more plots on this figure\n\n% Predict values for population sizes of 35,000 and 70,000\npredict1 = [1, 3.5] *theta;\nfprintf('For population = 35,000, we predict a profit of %f\\n',...\n    predict1*10000);\npredict2 = [1, 7] * theta;\nfprintf('For population = 70,000, we predict a profit of %f\\n',...\n    predict2*10000);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ============= Part 4: Visualizing J(theta_0, theta_1) =============\nfprintf('Visualizing J(theta_0, theta_1) ...\\n')\n\n% Grid over which we will calculate J\ntheta0_vals = linspace(-10, 10, 100);\ntheta1_vals = linspace(-1, 4, 100);\n\n% initialize J_vals to a matrix of 0's\nJ_vals = zeros(length(theta0_vals), length(theta1_vals));\n\n% Fill out J_vals\nfor i = 1:length(theta0_vals)\n    for j = 1:length(theta1_vals)\n\t  t = [theta0_vals(i); theta1_vals(j)];    \n\t  J_vals(i,j) = computeCost(X, y, t);\n    end\nend\n\n\n% Because of the way meshgrids work in the surf command, we need to \n% transpose J_vals before calling surf, or else the axes will be flipped\nJ_vals = J_vals';\n% Surface plot\nfigure;\nsurf(theta0_vals, theta1_vals, J_vals)\nxlabel('\\theta_0'); ylabel('\\theta_1');\n\n% Contour plot\nfigure;\n% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100\ncontour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))\nxlabel('\\theta_0'); ylabel('\\theta_1');\nhold on;\nplot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);\n"
  },
  {
    "path": "machine-learning-ex1/ex1/ex1_multi.m",
    "content": "%% Machine Learning Online Class\n%  Exercise 1: Linear regression with multiple variables\n%\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the\n%  linear regression exercise. \n%\n%  You will need to complete the following functions in this \n%  exericse:\n%\n%     warmUpExercise.m\n%     plotData.m\n%     gradientDescent.m\n%     computeCost.m\n%     gradientDescentMulti.m\n%     computeCostMulti.m\n%     featureNormalize.m\n%     normalEqn.m\n%\n%  For this part of the exercise, you will need to change some\n%  parts of the code below for various experiments (e.g., changing\n%  learning rates).\n%\n\n%% Initialization\n\n%% ================ Part 1: Feature Normalization ================\n\n%% Clear and Close Figures\nclear ; close all; clc\n\nfprintf('Loading data ...\\n');\n\n%% Load Data\ndata = load('ex1data2.txt');\nX = data(:, 1:2);\ny = data(:, 3);\nm = length(y);\n\n% Print out some data points\nfprintf('First 10 examples from the dataset: \\n');\nfprintf(' x = [%.0f %.0f], y = %.0f \\n', [X(1:10,:) y(1:10,:)]');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n% Scale features and set them to zero mean\nfprintf('Normalizing Features ...\\n');\n\n[X mu sigma] = featureNormalize(X);%ע⣬XӦX_norm\n\n% Add intercept term to X\nX = [ones(m, 1) X];\n\n\n%% ================ Part 2: Gradient Descent ================\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: We have provided you with the following starter\n%               code that runs gradient descent with a particular\n%               learning rate (alpha). \n%\n%               Your task is to first make sure that your functions - \n%               computeCost and gradientDescent already work with \n%               this starter code and support multiple variables.\n%\n%               After that, try running gradient descent with \n%               different values of alpha and see which one gives\n%               you the best result.\n%\n%               Finally, you should complete the code at the end\n%               to predict the price of a 1650 sq-ft, 3 br house.\n%\n% Hint: By using the 'hold on' command, you can plot multiple\n%       graphs on the same figure.\n%\n% Hint: At prediction, make sure you do the same feature normalization.\n%\n\nfprintf('Running gradient descent ...\\n');\n\n% Choose some alpha value\nalpha = 0.01;\nnum_iters = 400;\n\n% Init Theta and Run Gradient Descent \ntheta = zeros(3, 1);\n[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);\n\n% Plot the convergence graph\nfigure;\nplot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);\nxlabel('Number of iterations');\nylabel('Cost J');\n\n% Display gradient descent's result\nfprintf('Theta computed from gradient descent: \\n');\nfprintf(' %f \\n', theta);\nfprintf('\\n');\n\n% Estimate the price of a 1650 sq-ft, 3 br house\n% ====================== YOUR CODE HERE ======================\n% Recall that the first column of X is all-ones. Thus, it does\n% not need to be normalized.\nprice = 0; % You should change this\n\npredict1=[1650 3];\nnorm_predict=(predict1-mu)./sigma;\nfinal_predict=[1 norm_predict];\n\nprice=final_predict*theta;\n% ============================================================\n\nfprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...\n         '(using gradient descent):\\n $%f\\n'], price);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ================ Part 3: Normal Equations ================\n\nfprintf('Solving with normal equations...\\n');\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: The following code computes the closed form \n%               solution for linear regression using the normal\n%               equations. You should complete the code in \n%               normalEqn.m\n%\n%               After doing so, you should complete this code \n%               to predict the price of a 1650 sq-ft, 3 br house.\n%\n\n%% Load Data\ndata = csvread('ex1data2.txt');\nX = data(:, 1:2);\ny = data(:, 3);\nm = length(y);\n\n% Add intercept term to X\nX = [ones(m, 1) X];\n\n% Calculate the parameters from the normal equation\ntheta = normalEqn(X, y);\n\n% Display normal equation's result\nfprintf('Theta computed from the normal equations: \\n');\nfprintf(' %f \\n', theta);\nfprintf('\\n');\n\n\n% Estimate the price of a 1650 sq-ft, 3 br house\n% ====================== YOUR CODE HERE ======================\nprice = 0; % You should change this\n\npredict2=[1 1650 3];\nprice=predict2*theta;\n\n% ============================================================\n\nfprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...\n         '(using normal equations):\\n $%f\\n'], price);\n\n"
  },
  {
    "path": "machine-learning-ex1/ex1/ex1data1.txt",
    "content": "6.1101,17.592\n5.5277,9.1302\n8.5186,13.662\n7.0032,11.854\n5.8598,6.8233\n8.3829,11.886\n7.4764,4.3483\n8.5781,12\n6.4862,6.5987\n5.0546,3.8166\n5.7107,3.2522\n14.164,15.505\n5.734,3.1551\n8.4084,7.2258\n5.6407,0.71618\n5.3794,3.5129\n6.3654,5.3048\n5.1301,0.56077\n6.4296,3.6518\n7.0708,5.3893\n6.1891,3.1386\n20.27,21.767\n5.4901,4.263\n6.3261,5.1875\n5.5649,3.0825\n18.945,22.638\n12.828,13.501\n10.957,7.0467\n13.176,14.692\n22.203,24.147\n5.2524,-1.22\n6.5894,5.9966\n9.2482,12.134\n5.8918,1.8495\n8.2111,6.5426\n7.9334,4.5623\n8.0959,4.1164\n5.6063,3.3928\n12.836,10.117\n6.3534,5.4974\n5.4069,0.55657\n6.8825,3.9115\n11.708,5.3854\n5.7737,2.4406\n7.8247,6.7318\n7.0931,1.0463\n5.0702,5.1337\n5.8014,1.844\n11.7,8.0043\n5.5416,1.0179\n7.5402,6.7504\n5.3077,1.8396\n7.4239,4.2885\n7.6031,4.9981\n6.3328,1.4233\n6.3589,-1.4211\n6.2742,2.4756\n5.6397,4.6042\n9.3102,3.9624\n9.4536,5.4141\n8.8254,5.1694\n5.1793,-0.74279\n21.279,17.929\n14.908,12.054\n18.959,17.054\n7.2182,4.8852\n8.2951,5.7442\n10.236,7.7754\n5.4994,1.0173\n20.341,20.992\n10.136,6.6799\n7.3345,4.0259\n6.0062,1.2784\n7.2259,3.3411\n5.0269,-2.6807\n6.5479,0.29678\n7.5386,3.8845\n5.0365,5.7014\n10.274,6.7526\n5.1077,2.0576\n5.7292,0.47953\n5.1884,0.20421\n6.3557,0.67861\n9.7687,7.5435\n6.5159,5.3436\n8.5172,4.2415\n9.1802,6.7981\n6.002,0.92695\n5.5204,0.152\n5.0594,2.8214\n5.7077,1.8451\n7.6366,4.2959\n5.8707,7.2029\n5.3054,1.9869\n8.2934,0.14454\n13.394,9.0551\n5.4369,0.61705\n"
  },
  {
    "path": "machine-learning-ex1/ex1/ex1data2.txt",
    "content": "2104,3,399900\n1600,3,329900\n2400,3,369000\n1416,2,232000\n3000,4,539900\n1985,4,299900\n1534,3,314900\n1427,3,198999\n1380,3,212000\n1494,3,242500\n1940,4,239999\n2000,3,347000\n1890,3,329999\n4478,5,699900\n1268,3,259900\n2300,4,449900\n1320,2,299900\n1236,3,199900\n2609,4,499998\n3031,4,599000\n1767,3,252900\n1888,2,255000\n1604,3,242900\n1962,4,259900\n3890,3,573900\n1100,3,249900\n1458,3,464500\n2526,3,469000\n2200,3,475000\n2637,3,299900\n1839,2,349900\n1000,1,169900\n2040,4,314900\n3137,3,579900\n1811,4,285900\n1437,3,249900\n1239,3,229900\n2132,4,345000\n4215,4,549000\n2162,4,287000\n1664,2,368500\n2238,3,329900\n2567,4,314000\n1200,3,299000\n852,2,179900\n1852,4,299900\n1203,3,239500\n"
  },
  {
    "path": "machine-learning-ex1/ex1/featureNormalize.m",
    "content": "function [X_norm, mu, sigma] = featureNormalize(X)\n%FEATURENORMALIZE Normalizes the features in X \n%   FEATURENORMALIZE(X) returns a normalized version of X where\n%   the mean value of each feature is 0 and the standard deviation\n%   is 1. This is often a good preprocessing step to do when\n%   working with learning algorithms.\n\n% You need to set these values correctly\nX_norm = X;\nmu = zeros(1, size(X, 2));\nsigma = zeros(1, size(X, 2));\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: First, for each feature dimension, compute the mean\n%               of the feature and subtract it from the dataset,\n%               storing the mean value in mu. Next, compute the \n%               standard deviation of each feature and divide\n%               each feature by it's standard deviation, storing\n%               the standard deviation in sigma. \n%\n%               Note that X is a matrix where each column is a \n%               feature and each row is an example. You need \n%               to perform the normalization separately for \n%               each feature. \n%\n% Hint: You might find the 'mean' and 'std' functions useful.\n%       \nmu=mean(X_norm);\nsigma=std(X_norm);\nfor i=1:size(X,2)\n    X_norm(:,i)=X_norm(:,i)-mu(i);\n    X_norm(:,i)=X_norm(:,i)./sigma(i);\nend\n\n\n\n% ============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/gradientDescent.m",
    "content": "function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)\n%GRADIENTDESCENT Performs gradient descent to learn theta\n%   theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by \n%   taking num_iters gradient steps with learning rate alpha\n\n% Initialize some useful values\nm = length(y); % number of training examples\nJ_history = zeros(num_iters, 1);\n\nfor iter = 1:num_iters\n\n    % ====================== YOUR CODE HERE ======================\n    % Instructions: Perform a single gradient step on the parameter vector\n    %               theta. \n    %\n    % Hint: While debugging, it can be useful to print out the values\n    %       of the cost function (computeCost) and gradient here.\n    %\n    \n    temp1=1/m*alpha*sum(X*theta-y);\n    temp2=1/m*alpha*sum((X*theta-y).*X(:,2));\n    theta(1)=theta(1)-temp1;\n    theta(2)=theta(2)-temp2;\n\n\n    % ============================================================\n\n    % Save the cost J in every iteration    \n    J_history(iter) = computeCost(X, y, theta);\n\nend\n\nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/gradientDescentMulti.m",
    "content": "function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)\n%GRADIENTDESCENTMULTI Performs gradient descent to learn theta\n%   theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by\n%   taking num_iters gradient steps with learning rate alpha\n\n% Initialize some useful values\nm = length(y); % number of training examples\nn = length(theta);\ntemp = zeros(n, num_iters);\nJ_history = zeros(num_iters, 1);\n\nfor iter = 1:num_iters\n\n    % ====================== YOUR CODE HERE ======================\n    % Instructions: Perform a single gradient step on the parameter vector\n    %               theta. \n    %\n    % Hint: While debugging, it can be useful to print out the values\n    %       of the cost function (computeCostMulti) and gradient here.\n    %\n    h = X*theta;\n    temp(:,iter) = theta - ((alpha/m)*(X'*(h-y)));\n    theta = temp(:,iter);\n    \n    %temp1=1/m*alpha*sum(X*theta-y);\n    %temp2=1/m*alpha*sum((X*theta-y).*X(:,2));\n    %temp3=1/m*alpha*sum((X*theta-y).*X(:,3));\n    %theta(1)=theta(1)-temp1;\n    %theta(2)=theta(2)-temp2;\n    %theta(3)=theta(3)-temp3;\n\n    % ============================================================\n\n    % Save the cost J in every iteration    \n    J_history(iter) = computeCostMulti(X, y, theta);\n\nend\n\nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/AUTHORS.txt",
    "content": "The author of \"jsonlab\" toolbox is Qianqian Fang. Qianqian\nis currently an Assistant Professor at Massachusetts General Hospital, \nHarvard Medical School.\n\nAddress: Martinos Center for Biomedical Imaging, \n         Massachusetts General Hospital, \n         Harvard Medical School\n         Bldg 149, 13th St, Charlestown, MA 02129, USA\nURL: http://nmr.mgh.harvard.edu/~fangq/\nEmail: <fangq at nmr.mgh.harvard.edu> or <fangqq at gmail.com>\n\n\nThe script loadjson.m was built upon previous works by\n\n- Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n       date: 2009/11/02\n- François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n       date: 2009/03/22\n- Joel Feenstra: http://www.mathworks.com/matlabcentral/fileexchange/20565\n       date: 2008/07/03\n\n\nThis toolbox contains patches submitted by the following contributors:\n\n- Blake Johnson <bjohnso at bbn.com>\n  part of revision 341\n\n- Niclas Borlin <Niclas.Borlin at cs.umu.se>\n  various fixes in revision 394, including\n  - loadjson crashes for all-zero sparse matrix.\n  - loadjson crashes for empty sparse matrix.\n  - Non-zero size of 0-by-N and N-by-0 empty matrices is lost after savejson/loadjson.\n  - loadjson crashes for sparse real column vector.\n  - loadjson crashes for sparse complex column vector.\n  - Data is corrupted by savejson for sparse real row vector.\n  - savejson crashes for sparse complex row vector. \n\n- Yul Kang <yul.kang.on at gmail.com>\n  patches for svn revision 415.\n  - savejson saves an empty cell array as [] instead of null\n  - loadjson differentiates an empty struct from an empty array\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/ChangeLog.txt",
    "content": "============================================================================\n\n   JSONlab - a toolbox to encode/decode JSON/UBJSON files in MATLAB/Octave\n\n----------------------------------------------------------------------------\n\nJSONlab ChangeLog (key features marked by *):\n\n== JSONlab 1.0 (codename: Optimus - Final), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2015/01/02 polish help info for all major functions, update examples, finalize 1.0\n 2014/12/19 fix a bug to strictly respect NoRowBracket in savejson\n\n== JSONlab 1.0.0-RC2 (codename: Optimus - RC2), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/11/22 show progress bar in loadjson ('ShowProgress') \n 2014/11/17 add Compact option in savejson to output compact JSON format ('Compact')\n 2014/11/17 add FastArrayParser in loadjson to specify fast parser applicable levels\n 2014/09/18 start official github mirror: https://github.com/fangq/jsonlab\n\n== JSONlab 1.0.0-RC1 (codename: Optimus - RC1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/09/17  fix several compatibility issues when running on octave versions 3.2-3.8\n 2014/09/17  support 2D cell and struct arrays in both savejson and saveubjson\n 2014/08/04  escape special characters in a JSON string\n 2014/02/16  fix a bug when saving ubjson files\n\n== JSONlab 0.9.9 (codename: Optimus - beta), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/01/22  use binary read and write in saveubjson and loadubjson\n\n== JSONlab 0.9.8-1 (codename: Optimus - alpha update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2013/10/07 better round-trip conservation for empty arrays and structs (patch submitted by Yul Kang)\n\n== JSONlab 0.9.8 (codename: Optimus - alpha), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2013/08/23 *universal Binary JSON (UBJSON) support, including both saveubjson and loadubjson\n\n== JSONlab 0.9.1 (codename: Rodimus, update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2012/12/18 *handling of various empty and sparse matrices (fixes submitted by Niclas Borlin)\n\n== JSONlab 0.9.0 (codename: Rodimus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/06/17 *new format for an invalid leading char, unpacking hex code in savejson\n 2012/06/01  support JSONP in savejson\n 2012/05/25  fix the empty cell bug (reported by Cyril Davin)\n 2012/04/05  savejson can save to a file (suggested by Patrick Rapin)\n\n== JSONlab 0.8.1 (codename: Sentiel, Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/02/28  loadjson quotation mark escape bug, see http://bit.ly/yyk1nS\n 2012/01/25  patch to handle root-less objects, contributed by Blake Johnson\n\n== JSONlab 0.8.0 (codename: Sentiel), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/01/13 *speed up loadjson by 20 fold when parsing large data arrays in matlab\n 2012/01/11  remove row bracket if an array has 1 element, suggested by Mykel Kochenderfer\n 2011/12/22 *accept sequence of 'param',value input in savejson and loadjson\n 2011/11/18  fix struct array bug reported by Mykel Kochenderfer\n\n== JSONlab 0.5.1 (codename: Nexus Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/21  fix a bug in loadjson, previous code does not use any of the acceleration\n 2011/10/20  loadjson supports JSON collections - concatenated JSON objects\n\n== JSONlab 0.5.0 (codename: Nexus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/16  package and release jsonlab 0.5.0\n 2011/10/15 *add json demo and regression test, support cpx numbers, fix double quote bug\n 2011/10/11 *speed up readjson dramatically, interpret _Array* tags, show data in root level\n 2011/10/10  create jsonlab project, start jsonlab website, add online documentation\n 2011/10/07 *speed up savejson by 25x using sprintf instead of mat2str, add options support\n 2011/10/06 *savejson works for structs, cells and arrays\n 2011/09/09  derive loadjson from JSON parser from MATLAB Central, draft savejson.m\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/LICENSE_BSD.txt",
    "content": "Copyright 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are\npermitted provided that the following conditions are met:\n\n   1. Redistributions of source code must retain the above copyright notice, this list of\n      conditions and the following disclaimer.\n\n   2. Redistributions in binary form must reproduce the above copyright notice, this list\n      of conditions and the following disclaimer in the documentation and/or other materials\n      provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS \nOR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nThe views and conclusions contained in the software and documentation are those of the\nauthors and should not be interpreted as representing official policies, either expressed\nor implied, of the copyright holders.\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/README.txt",
    "content": "===============================================================================\n=                                 JSONLab                                     =\n=           An open-source MATLAB/Octave JSON encoder and decoder             =\n===============================================================================\n\n*Copyright (C) 2011-2015  Qianqian Fang <fangq at nmr.mgh.harvard.edu>\n*License: BSD License, see License_BSD.txt for details\n*Version: 1.0 (Optimus - Final)\n\n-------------------------------------------------------------------------------\n\nTable of Content:\n\nI.  Introduction\nII. Installation\nIII.Using JSONLab\nIV. Known Issues and TODOs\nV.  Contribution and feedback\n\n-------------------------------------------------------------------------------\n\nI.  Introduction\n\nJSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable, \nhuman-readable and \"[http://en.wikipedia.org/wiki/JSON fat-free]\" text format \nto represent complex and hierarchical data. It is as powerful as \n[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely \nused for data-exchange in applications, and is essential for the wild success \nof [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and \n[http://en.wikipedia.org/wiki/Web_2.0 Web2.0]. \n\nUBJSON (Universal Binary JSON) is a binary JSON format, specifically \noptimized for compact file size and better performance while keeping\nthe semantics as simple as the text-based JSON format. Using the UBJSON\nformat allows to wrap complex binary data in a flexible and extensible\nstructure, making it possible to process complex and large dataset \nwithout accuracy loss due to text conversions.\n\nWe envision that both JSON and its binary version will serve as part of \nthe mainstream data-exchange formats for scientific research in the future. \nIt will provide the flexibility and generality achieved by other popular \ngeneral-purpose file specifications, such as\n[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly \nreduced complexity and enhanced performance.\n\nJSONLab is a free and open-source implementation of a JSON/UBJSON encoder \nand a decoder in the native MATLAB language. It can be used to convert a MATLAB \ndata structure (array, struct, cell, struct array and cell array) into \nJSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB \ndata structure. JSONLab supports both MATLAB and  \n[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).\n\n-------------------------------------------------------------------------------\n\nII. Installation\n\nThe installation of JSONLab is no different than any other simple\nMATLAB toolbox. You only need to download/unzip the JSONLab package\nto a folder, and add the folder's path to MATLAB/Octave's path list\nby using the following command:\n\n    addpath('/path/to/jsonlab');\n\nIf you want to add this path permanently, you need to type \"pathtool\", \nbrowse to the jsonlab root folder and add to the list, then click \"Save\".\nThen, run \"rehash\" in MATLAB, and type \"which loadjson\", if you see an \noutput, that means JSONLab is installed for MATLAB/Octave.\n\n-------------------------------------------------------------------------------\n\nIII.Using JSONLab\n\nJSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder, \nand savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and \ntwo equivallent functions -- loadubjson and saveubjson for the binary \nJSON. The detailed help info for the four functions can be found below:\n\n=== loadjson.m ===\n<pre>\n  data=loadjson(fname,opt)\n     or\n  data=loadjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09, including previous works from \n \n          Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n             created on 2009/11/02\n          Franois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n             created on  2009/03/22\n          Joel Feenstra:\n          http://www.mathworks.com/matlabcentral/fileexchange/20565\n             created on 2008/07/03\n \n  $Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a JSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n                          speed-optimized array parser when loading an \n                          array object. The fast array parser may \n                          collapse block arrays into a single large\n                          array similar to rules defined in cell2mat; 0 to \n                          use a legacy parser; if set to a larger-than-1\n                          value, this option will specify the minimum\n                          dimension to enable the fast array parser. For\n                          example, if the input is a 3D array, setting\n                          FastArrayParser to 1 will return a 3D array;\n                          setting to 2 will return a cell array of 2D\n                          arrays; setting to 3 will return to a 2D cell\n                          array of 1D vectors; setting to 4 will return a\n                          3D cell array.\n            opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n       dat=loadjson(['examples' filesep 'example1.json'])\n       dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n</pre>\n\n=== savejson.m ===\n\n<pre>\n  json=savejson(rootname,obj,filename)\n     or\n  json=savejson(rootname,obj,opt)\n  json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n  Object Notation) string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09\n \n  $Id: savejson.m 458 2014-12-19 22:17:17Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array).\n       filename: a string for the file name to save the output JSON data.\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n                          of a 1D/2D array;\n         opt.ArrayIndent [1|0]: if 1, output explicit data array with\n                          precedent indentation; if 0, no indentation\n         opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n                          to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n                          and $1 represents the sign. For those who want to use\n                          1e999 to represent Inf, they can set opt.Inf to '$11e999'\n         opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n                          to represent NaN\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSONP='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n         opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n         opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a string in the JSON format (see http://json.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       savejson('jmesh',jsonmesh)\n       savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n </pre>\n\n=== loadubjson.m ===\n\n<pre>\n  data=loadubjson(fname,opt)\n     or\n  data=loadubjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/01\n \n  $Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a UBJSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.IntEndian [B|L]: specify the endianness of the integer fields\n                          in the UBJSON input data. B - Big-Endian format for \n                          integers (as required in the UBJSON specification); \n                          L - input integer fields are in Little-Endian order.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       obj=struct('string','value','array',[1 2 3]);\n       ubjdata=saveubjson('obj',obj);\n       dat=loadubjson(ubjdata)\n       dat=loadubjson(['examples' filesep 'example1.ubj'])\n       dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n</pre>\n\n=== saveubjson.m ===\n\n<pre>\n  json=saveubjson(rootname,obj,filename)\n     or\n  json=saveubjson(rootname,obj,opt)\n  json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a Universal \n  Binary JSON (UBJSON) binary string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/17\n \n  $Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array)\n       filename: a string for the file name to save the output UBJSON data\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSON='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a binary string in the UBJSON format (see http://ubjson.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       saveubjson('jsonmesh',jsonmesh)\n       saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n</pre>\n\n\n=== examples ===\n\nUnder the \"examples\" folder, you can find several scripts to demonstrate the\nbasic utilities of JSONLab. Running the \"demo_jsonlab_basic.m\" script, you \nwill see the conversions from MATLAB data structure to JSON text and backward.\nIn \"jsonlab_selftest.m\", we load complex JSON files downloaded from the Internet\nand validate the loadjson/savejson functions for regression testing purposes.\nSimilarly, a \"demo_ubjson_basic.m\" script is provided to test the saveubjson\nand loadubjson pairs for various matlab data structures.\n\nPlease run these examples and understand how JSONLab works before you use\nit to process your data.\n\n-------------------------------------------------------------------------------\n\nIV. Known Issues and TODOs\n\nJSONLab has several known limitations. We are striving to make it more general\nand robust. Hopefully in a few future releases, the limitations become less.\n\nHere are the known issues:\n\n# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;\n# When processing names containing multi-byte characters, Octave and MATLAB \\\ncan give different field-names; you can use feature('DefaultCharacterSet','latin1') \\\nin MATLAB to get consistant results\n# savejson can not handle class and dataset.\n# saveubjson converts a logical array into a uint8 ([U]) array\n# an unofficial N-D array count syntax is implemented in saveubjson. We are \\\nactively communicating with the UBJSON spec maintainer to investigate the \\\npossibility of making it upstream\n# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \\\nfiles, however, it can parse all UBJSON files produced by saveubjson.\n\n-------------------------------------------------------------------------------\n\nV. Contribution and feedback\n\nJSONLab is an open-source project. This means you can not only use it and modify\nit as you wish, but also you can contribute your changes back to JSONLab so\nthat everyone else can enjoy the improvement. For anyone who want to contribute,\nplease download JSONLab source code from it's subversion repository by using the\nfollowing command:\n\n svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab\n\nYou can make changes to the files as needed. Once you are satisfied with your\nchanges, and ready to share it with others, please cd the root directory of \nJSONLab, and type\n\n svn diff > yourname_featurename.patch\n\nYou then email the .patch file to JSONLab's maintainer, Qianqian Fang, at\nthe email address shown in the beginning of this file. Qianqian will review \nthe changes and commit it to the subversion if they are satisfactory.\n\nWe appreciate any suggestions and feedbacks from you. Please use iso2mesh's\nmailing list to report any questions you may have with JSONLab:\n\nhttp://groups.google.com/group/iso2mesh-users?hl=en&pli=1\n\n(Subscription to the mailing list is needed in order to post messages).\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/jsonopt.m",
    "content": "function val=jsonopt(key,default,varargin)\n%\n% val=jsonopt(key,default,optstruct)\n%\n% setting options based on a struct. The struct can be produced\n% by varargin2struct from a list of 'param','value' pairs\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n%\n% $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $\n%\n% input:\n%      key: a string with which one look up a value from a struct\n%      default: if the key does not exist, return default\n%      optstruct: a struct where each sub-field is a key \n%\n% output:\n%      val: if key exists, val=optstruct.key; otherwise val=default\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n% \n\nval=default;\nif(nargin<=2) return; end\nopt=varargin{1};\nif(isstruct(opt) && isfield(opt,key))\n    val=getfield(opt,key);\nend\n\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/loadjson.m",
    "content": "function data = loadjson(fname,varargin)\n%\n% data=loadjson(fname,opt)\n%    or\n% data=loadjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09, including previous works from \n%\n%         Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n%            created on 2009/11/02\n%         François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n%            created on  2009/03/22\n%         Joel Feenstra:\n%         http://www.mathworks.com/matlabcentral/fileexchange/20565\n%            created on 2008/07/03\n%\n% $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a JSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n%                         speed-optimized array parser when loading an \n%                         array object. The fast array parser may \n%                         collapse block arrays into a single large\n%                         array similar to rules defined in cell2mat; 0 to \n%                         use a legacy parser; if set to a larger-than-1\n%                         value, this option will specify the minimum\n%                         dimension to enable the fast array parser. For\n%                         example, if the input is a 3D array, setting\n%                         FastArrayParser to 1 will return a 3D array;\n%                         setting to 2 will return a cell array of 2D\n%                         arrays; setting to 3 will return to a 2D cell\n%                         array of 1D vectors; setting to 4 will return a\n%                         3D cell array.\n%           opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n%      dat=loadjson(['examples' filesep 'example1.json'])\n%      dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\n\nif(jsonopt('ShowProgress',0,opt)==1)\n    opt.progressbar_=waitbar(0,'loading ...');\nend\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\nif(isfield(opt,'progressbar_'))\n    close(opt.progressbar_);\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=data(j).x0x5F_ArraySize_;\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    if next_char ~= '}'\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            parse_char(':');\n            val = parse_value(varargin{:});\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}'\n                break;\n            end\n            parse_char(',');\n        end\n    end\n    parse_char('}');\n\n%%-------------------------------------------------------------------------\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim2=[];\n    arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:});\n    pbar=jsonopt('progressbar_',-1,varargin{:});\n\n    if next_char ~= ']'\n\tif(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))\n            [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos);\n            arraystr=['[' inStr(pos:endpos)];\n            arraystr=regexprep(arraystr,'\"_NaN_\"','NaN');\n            arraystr=regexprep(arraystr,'\"([-+]*)_Inf_\"','$1Inf');\n            arraystr(arraystr==sprintf('\\n'))=[];\n            arraystr(arraystr==sprintf('\\r'))=[];\n            %arraystr=regexprep(arraystr,'\\s*,',','); % this is slow,sometimes needed\n            if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D\n        \tastr=inStr((e1l+1):(e1r-1));\n        \tastr=regexprep(astr,'\"_NaN_\"','NaN');\n        \tastr=regexprep(astr,'\"([-+]*)_Inf_\"','$1Inf');\n        \tastr(astr==sprintf('\\n'))=[];\n        \tastr(astr==sprintf('\\r'))=[];\n        \tastr(astr==' ')='';\n        \tif(isempty(find(astr=='[', 1))) % array is 2D\n                    dim2=length(sscanf(astr,'%f,',[1 inf]));\n        \tend\n            else % array is 1D\n        \tastr=arraystr(2:end-1);\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]);\n        \tif(nextidx>=length(astr)-1)\n                    object=obj;\n                    pos=endpos;\n                    parse_char(']');\n                    return;\n        \tend\n            end\n            if(~isempty(dim2))\n        \tastr=arraystr;\n        \tastr(astr=='[')='';\n        \tastr(astr==']')='';\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf);\n        \tif(nextidx>=length(astr)-1)\n                    object=reshape(obj,dim2,numel(obj)/dim2)';\n                    pos=endpos;\n                    parse_char(']');\n                    if(pbar>0)\n                        waitbar(pos/length(inStr),pbar,'loading ...');\n                    end\n                    return;\n        \tend\n            end\n            arraystr=regexprep(arraystr,'\\]\\s*,','];');\n\telse\n            arraystr='[';\n\tend\n        try\n           if(isoct && regexp(arraystr,'\"','once'))\n                error('Octave eval can produce empty cells for JSON-like input');\n           end\n           object=eval(arraystr);\n           pos=endpos;\n        catch\n         while 1\n            newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1);\n            val = parse_value(newopt);\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            parse_char(',');\n         end\n        end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    parse_char(']');\n    \n    if(pbar>0)\n        waitbar(pos/length(inStr),pbar,'loading ...');\n    end\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr len  esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    if inStr(pos) ~= '\"'\n        error_pos('String starting with \" expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    str = '';\n    while pos <= len\n        while index_esc <= len_esc && esc(index_esc) < pos\n            index_esc = index_esc + 1;\n        end\n        if index_esc > len_esc\n            str = [str inStr(pos:len)];\n            pos = len + 1;\n            break;\n        else\n            str = [str inStr(pos:esc(index_esc)-1)];\n            pos = esc(index_esc);\n        end\n        nstr = length(str); switch inStr(pos)\n            case '\"'\n                pos = pos + 1;\n                if(~isempty(str))\n                    if(strcmp(str,'_Inf_'))\n                        str=Inf;\n                    elseif(strcmp(str,'-_Inf_'))\n                        str=-Inf;\n                    elseif(strcmp(str,'_NaN_'))\n                        str=NaN;\n                    end\n                end\n                return;\n            case '\\'\n                if pos+1 > len\n                    error_pos('End of file reached right after escape character');\n                end\n                pos = pos + 1;\n                switch inStr(pos)\n                    case {'\"' '\\' '/'}\n                        str(nstr+1) = inStr(pos);\n                        pos = pos + 1;\n                    case {'b' 'f' 'n' 'r' 't'}\n                        str(nstr+1) = sprintf(['\\' inStr(pos)]);\n                        pos = pos + 1;\n                    case 'u'\n                        if pos+4 > len\n                            error_pos('End of file reached in escaped unicode character');\n                        end\n                        str(nstr+(1:6)) = inStr(pos-1:pos+4);\n                        pos = pos + 5;\n                end\n            otherwise % should never happen\n                str(nstr+1) = inStr(pos), keyboard\n                pos = pos + 1;\n        end\n    end\n    error_pos('End of file while expecting end of inStr');\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct\n    currstr=inStr(pos:end);\n    numstr=0;\n    if(isoct~=0)\n        numstr=regexp(currstr,'^\\s*-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+\\-]?\\d+)?','end');\n        [num, one] = sscanf(currstr, '%f', 1);\n        delta=numstr+1;\n    else\n        [num, one, err, delta] = sscanf(currstr, '%f', 1);\n        if ~isempty(err)\n            error_pos('Error reading number at position %d');\n        end\n    end\n    pos = pos + delta-1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n    \n    pbar=jsonopt('progressbar_',-1,varargin{:});\n    if(pbar>0)\n        waitbar(pos/len,pbar,'loading ...');\n    end\n    \n    switch(inStr(pos))\n        case '\"'\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'-','0','1','2','3','4','5','6','7','8','9'}\n            val = parse_number(varargin{:});\n            return;\n        case 't'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true')\n                val = true;\n                pos = pos + 4;\n                return;\n            end\n        case 'f'\n            if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false')\n                val = false;\n                pos = pos + 5;\n                return;\n            end\n        case 'n'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null')\n                val = [];\n                pos = pos + 4;\n                return;\n            end\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/loadubjson.m",
    "content": "function data = loadubjson(fname,varargin)\n%\n% data=loadubjson(fname,opt)\n%    or\n% data=loadubjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/01\n%\n% $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a UBJSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.IntEndian [B|L]: specify the endianness of the integer fields\n%                         in the UBJSON input data. B - Big-Endian format for \n%                         integers (as required in the UBJSON specification); \n%                         L - input integer fields are in Little-Endian order.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      obj=struct('string','value','array',[1 2 3]);\n%      ubjdata=saveubjson('obj',obj);\n%      dat=loadubjson(ubjdata)\n%      dat=loadubjson(['examples' filesep 'example1.ubj'])\n%      dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken fileendian systemendian\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\nfileendian=upper(jsonopt('IntEndian','B',opt));\n[os,maxelem,systemendian]=computer;\n\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\n\n\n%%\nfunction newdata=parse_collection(id,data,obj)\n\nif(jsoncount>0 && exist('data','var')) \n    if(~iscell(data))\n       newdata=cell(1);\n       newdata{1}=data;\n       data=newdata;\n    end\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=double(data(j).x0x5F_ArraySize_);\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1); % TODO\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        count=double(parse_number());\n    end\n    if next_char ~= '}'\n        num=0;\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            %parse_char(':');\n            val = parse_value(varargin{:});\n            num=num+1;\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}' || (count>=0 && num>=count)\n                break;\n            end\n            %parse_char(',');\n        end\n    end\n    if(count==-1)\n        parse_char('}');\n    end\n\n%%-------------------------------------------------------------------------\nfunction [cid,len]=elem_info(type)\nid=strfind('iUIlLdD',type);\ndataclass={'int8','uint8','int16','int32','int64','single','double'};\nbytelen=[1,1,2,4,8,4,8];\nif(id>0)\n    cid=dataclass{id};\n    len=bytelen(id);\nelse\n    error_pos('unsupported type at position %d');\nend\n%%-------------------------------------------------------------------------\n\n\nfunction [data adv]=parse_block(type,count,varargin)\nglobal pos inStr isoct fileendian systemendian\n[cid,len]=elem_info(type);\ndatastr=inStr(pos:pos+len*count-1);\nif(isoct)\n    newdata=int8(datastr);\nelse\n    newdata=uint8(datastr);\nend\nid=strfind('iUIlLdD',type);\nif(id<=5 && fileendian~=systemendian)\n    newdata=swapbytes(typecast(newdata,cid));\nend\ndata=typecast(newdata,cid);\nadv=double(len*count);\n\n%%-------------------------------------------------------------------------\n\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim=[];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1);\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        if(next_char=='[')\n            dim=parse_array(varargin{:});\n            count=prod(double(dim));\n        else\n            count=double(parse_number());\n        end\n    end\n    if(~isempty(type))\n        if(count>=0)\n            [object adv]=parse_block(type,count,varargin{:});\n            if(~isempty(dim))\n                object=reshape(object,dim);\n            end\n            pos=pos+adv;\n            return;\n        else\n            endpos=matching_bracket(inStr,pos);\n            [cid,len]=elem_info(type);\n            count=(endpos-pos)/len;\n            [object adv]=parse_block(type,count,varargin{:});\n            pos=pos+adv;\n            parse_char(']');\n            return;\n        end\n    end\n    if next_char ~= ']'\n         while 1\n            val = parse_value(varargin{:});\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            %parse_char(',');\n         end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    if(count==-1)\n        parse_char(']');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    type=inStr(pos);\n    if type ~= 'S' && type ~= 'C' && type ~= 'H'\n        error_pos('String starting with S expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    if(type == 'C')\n        str=inStr(pos);\n        pos=pos+1;\n        return;\n    end\n    bytelen=double(parse_number());\n    if(length(inStr)>=pos+bytelen-1)\n        str=inStr(pos:pos+bytelen-1);\n        pos=pos+bytelen;\n    else\n        error_pos('End of file while expecting end of inStr');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct fileendian systemendian\n    id=strfind('iUIlLdD',inStr(pos));\n    if(isempty(id))\n        error_pos('expecting a number at position %d');\n    end\n    type={'int8','uint8','int16','int32','int64','single','double'};\n    bytelen=[1,1,2,4,8,4,8];\n    datastr=inStr(pos+1:pos+bytelen(id));\n    if(isoct)\n        newdata=int8(datastr);\n    else\n        newdata=uint8(datastr);\n    end\n    if(id<=5 && fileendian~=systemendian)\n        newdata=swapbytes(typecast(newdata,type{id}));\n    end\n    num=typecast(newdata,type{id});\n    pos = pos + bytelen(id)+1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n\n    switch(inStr(pos))\n        case {'S','C','H'}\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'i','U','I','l','L','d','D'}\n            val = parse_number(varargin{:});\n            return;\n        case 'T'\n            val = true;\n            pos = pos + 1;\n            return;\n        case 'F'\n            val = false;\n            pos = pos + 1;\n            return;\n        case {'Z','N'}\n            val = [];\n            pos = pos + 1;\n            return;\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos e1l e1r maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/mergestruct.m",
    "content": "function s=mergestruct(s1,s2)\n%\n% s=mergestruct(s1,s2)\n%\n% merge two struct objects into one\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      s1,s2: a struct object, s1 and s2 can not be arrays\n%\n% output:\n%      s: the merged struct object. fields in s1 and s2 will be combined in s.\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(~isstruct(s1) || ~isstruct(s2))\n    error('input parameters contain non-struct');\nend\nif(length(s1)>1 || length(s2)>1)\n    error('can not merge struct arrays');\nend\nfn=fieldnames(s2);\ns=s1;\nfor i=1:length(fn)              \n    s=setfield(s,fn{i},getfield(s2,fn{i}));\nend\n\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/savejson.m",
    "content": "function json=savejson(rootname,obj,varargin)\n%\n% json=savejson(rootname,obj,filename)\n%    or\n% json=savejson(rootname,obj,opt)\n% json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n% Object Notation) string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09\n%\n% $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array).\n%      filename: a string for the file name to save the output JSON data.\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n%                         of a 1D/2D array;\n%        opt.ArrayIndent [1|0]: if 1, output explicit data array with\n%                         precedent indentation; if 0, no indentation\n%        opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n%                         to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n%                         and $1 represents the sign. For those who want to use\n%                         1e999 to represent Inf, they can set opt.Inf to '$11e999'\n%        opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n%                         to represent NaN\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSONP='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%        opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n%        opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a string in the JSON format (see http://json.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      savejson('jmesh',jsonmesh)\n%      savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\n\nwhitespaces=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nif(jsonopt('Compact',0,opt)==1)\n    whitespaces=struct('tab','','newline','','sep',',');\nend\nif(~isfield(opt,'whitespaces_'))\n    opt.whitespaces_=whitespaces;\nend\n\nnl=whitespaces.newline;\n\njson=obj2json(rootname,obj,rootlevel,opt);\nif(rootisarray)\n    json=sprintf('%s%s',json,nl);\nelse\n    json=sprintf('{%s%s%s}\\n',nl,json,nl);\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=sprintf('%s(%s);%s',jsonp,json,nl);\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    if(jsonopt('SaveBinary',0,opt)==1)\n\t    fid = fopen(opt.FileName, 'wb');\n\t    fwrite(fid,json);\n    else\n\t    fid = fopen(opt.FileName, 'wt');\n\t    fwrite(fid,json,'char');\n    end\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2json(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2json(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2json(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2json(name,item,level,varargin{:});\nelse\n    txt=mat2json(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2json(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=jsonopt('whitespaces_',struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n')),varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nif(len>1)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": [%s',padding0, checkname(name,varargin{:}),nl); name=''; \n    else\n        txt=sprintf('%s[%s',padding0,nl); \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": []',padding0, checkname(name,varargin{:})); name=''; \n    else\n        txt=sprintf('%s[]',padding0); \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n    for i=1:dim(1)\n       txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:}));\n       if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    end\n    if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n    if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2json(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\npadding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1));\nnl=ws.newline;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding0,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding0,nl); end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n  for i=1:dim(1)\n    names = fieldnames(item(i,j));\n    if(~isempty(name) && len==1)\n        txt=sprintf('%s%s\"%s\": {%s',txt,padding1, checkname(name,varargin{:}),nl); \n    else\n        txt=sprintf('%s%s{%s',txt,padding1,nl); \n    end\n    if(~isempty(names))\n      for e=1:length(names)\n\t    txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:}));\n        if(e<length(names)) txt=sprintf('%s%s',txt,','); end\n        txt=sprintf('%s%s',txt,nl);\n      end\n    end\n    txt=sprintf('%s%s}',txt,padding1);\n    if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n  end\n  if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n  if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2json(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding1,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding1,nl); end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    if(isoct)\n        val=regexprep(item(e,:),'\\\\','\\\\');\n        val=regexprep(val,'\"','\\\"');\n        val=regexprep(val,'^\"','\\\"');\n    else\n        val=regexprep(item(e,:),'\\\\','\\\\\\\\');\n        val=regexprep(val,'\"','\\\\\"');\n        val=regexprep(val,'^\"','\\\\\"');\n    end\n    val=escapejsonstring(val);\n    if(len==1)\n        obj=['\"' checkname(name,varargin{:}) '\": ' '\"',val,'\"'];\n\tif(isempty(name)) obj=['\"',val,'\"']; end\n        txt=sprintf('%s%s%s%s',txt,padding1,obj);\n    else\n        txt=sprintf('%s%s%s%s',txt,padding0,['\"',val,'\"']);\n    end\n    if(e==len) sep=''; end\n    txt=sprintf('%s%s',txt,sep);\nend\nif(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2json(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:}))\n    if(isempty(name))\n    \ttxt=sprintf('%s{%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    else\n    \ttxt=sprintf('%s\"%s\": {%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    end\nelse\n    if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0)\n        numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\\[',''),']','');\n    else\n        numtxt=matdata2json(item,level+1,varargin{:});\n    end\n    if(isempty(name))\n    \ttxt=sprintf('%s%s',padding1,numtxt);\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n           \ttxt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        else\n    \t    txt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        end\n    end\n    return;\nend\ndataformat='%s%s%s%s%s';\n\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n    end\n    txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsSparse_\": ','1', sep);\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([iy(:),data'],level+2,varargin{:}), nl);\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,data],level+2,varargin{:}), nl);\n    else\n        % General case, store row and column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,iy,data],level+2,varargin{:}), nl);\n    end\nelse\n    if(isreal(item))\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json(item(:)',level+2,varargin{:}), nl);\n    else\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl);\n    end\nend\ntxt=sprintf('%s%s%s',txt,padding1,'}');\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2json(mat,level,varargin)\n\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\ntab=ws.tab;\nnl=ws.newline;\n\nif(size(mat,1)==1)\n    pre='';\n    post='';\n    level=level-1;\nelse\n    pre=sprintf('[%s',nl);\n    post=sprintf('%s%s]',nl,repmat(tab,1,level-1));\nend\n\nif(isempty(mat))\n    txt='null';\n    return;\nend\nfloatformat=jsonopt('FloatFormat','%.10g',varargin{:});\n%if(numel(mat)>1)\n    formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]];\n%else\n%    formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\\n')]];\n%end\n\nif(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1)\n    formatstr=[repmat(tab,1,level) formatstr];\nend\n\ntxt=sprintf(formatstr,mat');\ntxt(end-length(nl):end)=[];\nif(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1)\n   txt=regexprep(txt,'1','true');\n   txt=regexprep(txt,'0','false');\nend\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],\\n['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\ntxt=[pre txt post];\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n\n%%-------------------------------------------------------------------------\nfunction newstr=escapejsonstring(str)\nnewstr=str;\nisoct=exist('OCTAVE_VERSION','builtin');\nif(isoct)\n   vv=sscanf(OCTAVE_VERSION,'%f');\n   if(vv(1)>=3.8) isoct=0; end\nend\nif(isoct)\n  escapechars={'\\a','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},escapechars{i});\n  end\nelse\n  escapechars={'\\a','\\b','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\\\','\\\\\\\\'));\n  end\nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/saveubjson.m",
    "content": "function json=saveubjson(rootname,obj,varargin)\n%\n% json=saveubjson(rootname,obj,filename)\n%    or\n% json=saveubjson(rootname,obj,opt)\n% json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a Universal \n% Binary JSON (UBJSON) binary string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/17\n%\n% $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array)\n%      filename: a string for the file name to save the output UBJSON data\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSON='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a binary string in the UBJSON format (see http://ubjson.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      saveubjson('jsonmesh',jsonmesh)\n%      saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\njson=obj2ubjson(rootname,obj,rootlevel,opt);\nif(~rootisarray)\n    json=['{' json '}'];\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=[jsonp '(' json ')'];\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    fid = fopen(opt.FileName, 'wb');\n    fwrite(fid,json);\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2ubjson(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2ubjson(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2ubjson(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2ubjson(name,item,level,varargin{:});\nelse\n    txt=mat2ubjson(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2ubjson(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item); % let's handle 1D cell first\nif(len>1) \n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) '[']; name=''; \n    else\n        txt='['; \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; \n    else\n        txt='Z'; \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=[txt '[']; end\n    for i=1:dim(1)\n       txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})];\n    end\n    if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2ubjson(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=[txt '[']; end\n  for i=1:dim(1)\n     names = fieldnames(item(i,j));\n     if(~isempty(name) && len==1)\n        txt=[txt S_(checkname(name,varargin{:})) '{']; \n     else\n        txt=[txt '{']; \n     end\n     if(~isempty(names))\n       for e=1:length(names)\n\t     txt=[txt obj2ubjson(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})];\n       end\n     end\n     txt=[txt '}'];\n  end\n  if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2ubjson(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    val=item(e,:);\n    if(len==1)\n        obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),''];\n\tif(isempty(name)) obj=['',S_(val),'']; end\n        txt=[txt,'',obj];\n    else\n        txt=[txt,'',['',S_(val),'']];\n    end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2ubjson(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) || jsonopt('ArrayToStruct',0,varargin{:}))\n      cid=I_(uint32(max(size(item))));\n      if(isempty(name))\n    \ttxt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ];\n      else\n          if(isempty(item))\n              txt=[S_(checkname(name,varargin{:})),'Z'];\n              return;\n          else\n    \t      txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))];\n          end\n      end\nelse\n    if(isempty(name))\n    \ttxt=matdata2ubjson(item,level+1,varargin{:});\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n            numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\\[',''),']','');\n           \ttxt=[S_(checkname(name,varargin{:})) numtxt];\n        else\n    \t    txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})];\n        end\n    end\n    return;\nend\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=[txt,S_('_ArrayIsComplex_'),'T'];\n    end\n    txt=[txt,S_('_ArrayIsSparse_'),'T'];\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([iy(:),data'],level+2,varargin{:})];\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,data],level+2,varargin{:})];\n    else\n        % General case, store row and column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,iy,data],level+2,varargin{:})];\n    end\nelse\n    if(isreal(item))\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson(item(:)',level+2,varargin{:})];\n    else\n        txt=[txt,S_('_ArrayIsComplex_'),'T'];\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})];\n    end\nend\ntxt=[txt,'}'];\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2ubjson(mat,level,varargin)\nif(isempty(mat))\n    txt='Z';\n    return;\nend\nif(size(mat,1)==1)\n    level=level-1;\nend\ntype='';\nhasnegtive=(mat<0);\nif(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0)))\n    if(isempty(hasnegtive))\n       if(max(mat(:))<=2^8)\n           type='U';\n       end\n    end\n    if(isempty(type))\n        % todo - need to consider negative ones separately\n        id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]);\n        if(isempty(find(id)))\n            error('high-precision data is not yet supported');\n        end\n        key='iIlL';\n\ttype=key(find(id));\n    end\n    txt=[I_a(mat(:),type,size(mat))];\nelseif(islogical(mat))\n    logicalval='FT';\n    if(numel(mat)==1)\n        txt=logicalval(mat+1);\n    else\n        txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')];\n    end\nelse\n    if(numel(mat)==1)\n        txt=['[' D_(mat) ']'];\n    else\n        txt=D_a(mat(:),'D',size(mat));\n    end\nend\n\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n%%-------------------------------------------------------------------------\nfunction val=S_(str)\nif(length(str)==1)\n  val=['C' str];\nelse\n  val=['S' I_(int32(length(str))) str];\nend\n%%-------------------------------------------------------------------------\nfunction val=I_(num)\nif(~isinteger(num))\n    error('input is not an integer');\nend\nif(num>=0 && num<255)\n   val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')];\n   return;\nend\nkey='iIlL';\ncid={'int8','int16','int32','int64'};\nfor i=1:4\n  if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1)))\n    val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')];\n    return;\n  end\nend\nerror('unsupported integer');\n\n%%-------------------------------------------------------------------------\nfunction val=D_(num)\nif(~isfloat(num))\n    error('input is not a float');\nend\n\nif(isa(num,'single'))\n  val=['d' data2byte(num,'uint8')];\nelse\n  val=['D' data2byte(num,'uint8')];\nend\n%%-------------------------------------------------------------------------\nfunction data=I_a(num,type,dim,format)\nid=find(ismember('iUIlL',type));\n\nif(id==0)\n  error('unsupported integer array');\nend\n\n% based on UBJSON specs, all integer types are stored in big endian format\n\nif(id==1)\n  data=data2byte(swapbytes(int8(num)),'uint8');\n  blen=1;\nelseif(id==2)\n  data=data2byte(swapbytes(uint8(num)),'uint8');\n  blen=1;\nelseif(id==3)\n  data=data2byte(swapbytes(int16(num)),'uint8');\n  blen=2;\nelseif(id==4)\n  data=data2byte(swapbytes(int32(num)),'uint8');\n  blen=4;\nelseif(id==5)\n  data=data2byte(swapbytes(int64(num)),'uint8');\n  blen=8;\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/blen)) data(:)'];\n  end\n  data=['[' data(:)'];\nelse\n  data=reshape(data,blen,numel(data)/blen);\n  data(2:blen+1,:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction data=D_a(num,type,dim,format)\nid=find(ismember('dD',type));\n\nif(id==0)\n  error('unsupported float array');\nend\n\nif(id==1)\n  data=data2byte(single(num),'uint8');\nelseif(id==2)\n  data=data2byte(double(num),'uint8');\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)'];\n  end\n  data=['[' data];\nelse\n  data=reshape(data,(id*4),length(data)/(id*4));\n  data(2:(id*4+1),:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction bytes=data2byte(varargin)\nbytes=typecast(varargin{:});\nbytes=bytes(:)';\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/jsonlab/varargin2struct.m",
    "content": "function opt=varargin2struct(varargin)\n%\n% opt=varargin2struct('param1',value1,'param2',value2,...)\n%   or\n% opt=varargin2struct(...,optstruct,...)\n%\n% convert a series of input parameters into a structure\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      'param', value: the input parameters should be pairs of a string and a value\n%       optstruct: if a parameter is a struct, the fields will be merged to the output struct\n%\n% output:\n%      opt: a struct where opt.param1=value1, opt.param2=value2 ...\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nlen=length(varargin);\nopt=struct;\nif(len==0) return; end\ni=1;\nwhile(i<=len)\n    if(isstruct(varargin{i}))\n        opt=mergestruct(opt,varargin{i});\n    elseif(ischar(varargin{i}) && i<len)\n        opt=setfield(opt,varargin{i},varargin{i+1});\n        i=i+1;\n    else\n        error('input must be in the form of ...,''name'',value,... pairs or structs');\n    end\n    i=i+1;\nend\n\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/makeValidFieldName.m",
    "content": "function str = makeValidFieldName(str)\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    isoct=exist('OCTAVE_VERSION','builtin');\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n"
  },
  {
    "path": "machine-learning-ex1/ex1/lib/submitWithConfiguration.m",
    "content": "function submitWithConfiguration(conf)\n  addpath('./lib/jsonlab');\n\n  parts = parts(conf);\n\n  fprintf('== Submitting solutions | %s...\\n', conf.itemName);\n\n  tokenFile = 'token.mat';\n  if exist(tokenFile, 'file')\n    load(tokenFile);\n    [email token] = promptToken(email, token, tokenFile);\n  else\n    [email token] = promptToken('', '', tokenFile);\n  end\n\n  if isempty(token)\n    fprintf('!! Submission Cancelled\\n');\n    return\n  end\n\n  try\n    response = submitParts(conf, email, token, parts);\n  catch\n    e = lasterror();\n    fprintf( ...\n      '!! Submission failed: unexpected error: %s\\n', ...\n      e.message);\n    fprintf('!! Please try again later.\\n');\n    return\n  end\n\n  if isfield(response, 'errorMessage')\n    fprintf('!! Submission failed: %s\\n', response.errorMessage);\n  else\n    showFeedback(parts, response);\n    save(tokenFile, 'email', 'token');\n  end\nend\n\nfunction [email token] = promptToken(email, existingToken, tokenFile)\n  if (~isempty(email) && ~isempty(existingToken))\n    prompt = sprintf( ...\n      'Use token from last successful submission (%s)? (Y/n): ', ...\n      email);\n    reenter = input(prompt, 's');\n\n    if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y')\n      token = existingToken;\n      return;\n    else\n      delete(tokenFile);\n    end\n  end\n  email = input('Login (email address): ', 's');\n  token = input('Token: ', 's');\nend\n\nfunction isValid = isValidPartOptionIndex(partOptions, i)\n  isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions));\nend\n\nfunction response = submitParts(conf, email, token, parts)\n  body = makePostBody(conf, email, token, parts);\n  submissionUrl = submissionUrl();\n  params = {'jsonBody', body};\n  responseBody = urlread(submissionUrl, 'post', params);\n  response = loadjson(responseBody);\nend\n\nfunction body = makePostBody(conf, email, token, parts)\n  bodyStruct.assignmentSlug = conf.assignmentSlug;\n  bodyStruct.submitterEmail = email;\n  bodyStruct.secret = token;\n  bodyStruct.parts = makePartsStruct(conf, parts);\n\n  opt.Compact = 1;\n  body = savejson('', bodyStruct, opt);\nend\n\nfunction partsStruct = makePartsStruct(conf, parts)\n  for part = parts\n    partId = part{:}.id;\n    fieldName = makeValidFieldName(partId);\n    outputStruct.output = conf.output(partId);\n    partsStruct.(fieldName) = outputStruct;\n  end\nend\n\nfunction [parts] = parts(conf)\n  parts = {};\n  for partArray = conf.partArrays\n    part.id = partArray{:}{1};\n    part.sourceFiles = partArray{:}{2};\n    part.name = partArray{:}{3};\n    parts{end + 1} = part;\n  end\nend\n\nfunction showFeedback(parts, response)\n  fprintf('== \\n');\n  fprintf('== %43s | %9s | %-s\\n', 'Part Name', 'Score', 'Feedback');\n  fprintf('== %43s | %9s | %-s\\n', '---------', '-----', '--------');\n  for part = parts\n    score = '';\n    partFeedback = '';\n    partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id));\n    partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id));\n    score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore);\n    fprintf('== %43s | %9s | %-s\\n', part{:}.name, score, partFeedback);\n  end\n  evaluation = response.evaluation;\n  totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore);\n  fprintf('==                                   --------------------------------\\n');\n  fprintf('== %43s | %9s | %-s\\n', '', totalScore, '');\n  fprintf('== \\n');\nend\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%\n% Service configuration\n%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\nfunction submissionUrl = submissionUrl()\n  submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1';\nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/normalEqn.m",
    "content": "function [theta] = normalEqn(X, y)\n%NORMALEQN Computes the closed-form solution to linear regression \n%   NORMALEQN(X,y) computes the closed-form solution to linear \n%   regression using the normal equations.\n\ntheta = zeros(size(X, 2), 1);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Complete the code to compute the closed form solution\n%               to linear regression and put the result in theta.\n%\n\n% ---------------------- Sample Solution ----------------------\n\ntheta=pinv(X'*X)*X'*y;\n\n\n% -------------------------------------------------------------\n\n\n% ============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/plotData.m",
    "content": "function plotData(x, y)\n%PLOTDATA Plots the data points x and y into a new figure \n%   PLOTDATA(x,y) plots the data points and gives the figure axes labels of\n%   population and profit.\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Plot the training data into a figure using the \n%               \"figure\" and \"plot\" commands. Set the axes labels using\n%               the \"xlabel\" and \"ylabel\" commands. Assume the \n%               population and revenue data have been passed in\n%               as the x and y arguments of this function.\n%\n% Hint: You can use the 'rx' option with plot to have the markers\n%       appear as red crosses. Furthermore, you can make the\n%       markers larger by using plot(..., 'rx', 'MarkerSize', 10);\n\nfigure; % open a new figure window\n\n\nplot(x,y,'rx','MarkerSize',10);%rx means color is red and show symbol is x,and x's size is 10\nylabel=('Profit in $10,000s');\nxlabel=('Population of City in 10,000s');\n\n\n\n% ============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/submit.m",
    "content": "function submit()\n  addpath('./lib');\n\n  conf.assignmentSlug = 'linear-regression';\n  conf.itemName = 'Linear Regression with Multiple Variables';\n  conf.partArrays = { ...\n    { ...\n      '1', ...\n      { 'warmUpExercise.m' }, ...\n      'Warm-up Exercise', ...\n    }, ...\n    { ...\n      '2', ...\n      { 'computeCost.m' }, ...\n      'Computing Cost (for One Variable)', ...\n    }, ...\n    { ...\n      '3', ...\n      { 'gradientDescent.m' }, ...\n      'Gradient Descent (for One Variable)', ...\n    }, ...\n    { ...\n      '4', ...\n      { 'featureNormalize.m' }, ...\n      'Feature Normalization', ...\n    }, ...\n    { ...\n      '5', ...\n      { 'computeCostMulti.m' }, ...\n      'Computing Cost (for Multiple Variables)', ...\n    }, ...\n    { ...\n      '6', ...\n      { 'gradientDescentMulti.m' }, ...\n      'Gradient Descent (for Multiple Variables)', ...\n    }, ...\n    { ...\n      '7', ...\n      { 'normalEqn.m' }, ...\n      'Normal Equations', ...\n    }, ...\n  };\n  conf.output = @output;\n\n  submitWithConfiguration(conf);\nend\n\nfunction out = output(partId)\n  % Random Test Cases\n  X1 = [ones(20,1) (exp(1) + exp(2) * (0.1:0.1:2))'];\n  Y1 = X1(:,2) + sin(X1(:,1)) + cos(X1(:,2));\n  X2 = [X1 X1(:,2).^0.5 X1(:,2).^0.25];\n  Y2 = Y1.^0.5 + Y1;\n  if partId == '1'\n    out = sprintf('%0.5f ', warmUpExercise());\n  elseif partId == '2'\n    out = sprintf('%0.5f ', computeCost(X1, Y1, [0.5 -0.5]'));\n  elseif partId == '3'\n    out = sprintf('%0.5f ', gradientDescent(X1, Y1, [0.5 -0.5]', 0.01, 10));\n  elseif partId == '4'\n    out = sprintf('%0.5f ', featureNormalize(X2(:,2:4)));\n  elseif partId == '5'\n    out = sprintf('%0.5f ', computeCostMulti(X2, Y2, [0.1 0.2 0.3 0.4]'));\n  elseif partId == '6'\n    out = sprintf('%0.5f ', gradientDescentMulti(X2, Y2, [-0.1 -0.2 -0.3 -0.4]', 0.01, 10));\n  elseif partId == '7'\n    out = sprintf('%0.5f ', normalEqn(X2, Y2));\n  end \nend\n"
  },
  {
    "path": "machine-learning-ex1/ex1/warmUpExercise.m",
    "content": "function A = warmUpExercise()\n%WARMUPEXERCISE Example function in octave\n%   A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix\n\nA = [];\n% ============= YOUR CODE HERE ==============\n% Instructions: Return the 5x5 identity matrix \n%               In octave, we return values by defining which variables\n%               represent the return values (at the top of the file)\n%               and then set them accordingly. \n\n\n\nA=eye(5);\n\n\n\n% ===========================================\n\n\nend\n"
  },
  {
    "path": "machine-learning-ex2/ex2/costFunction.m",
    "content": "function [J, grad] = costFunction(theta, X, y)\n%COSTFUNCTION Compute cost and gradient for logistic regression\n%   J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the\n%   parameter for logistic regression and the gradient of the cost\n%   w.r.t. to the parameters.\n\n% Initialize some useful values\nm = length(y); % number of training examples\n\n% You need to return the following variables correctly \nJ = 0;\ngrad = zeros(size(theta));\n\nh = sigmoid(X*theta);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the cost of a particular choice of theta.\n%               You should set J to the cost.\n%               Compute the partial derivatives and set grad to the partial\n%               derivatives of the cost w.r.t. each parameter in theta\n%\n% Note: grad should have the same dimensions as theta\n%\n\nJ = (-y'*log(h)-(1-y)'*log(1-h))/m;\n\ngrad = X'*(h-y)/m;\n\n\n\n\n\n\n% =============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex2/ex2/costFunctionReg.m",
    "content": "function [J, grad] = costFunctionReg(theta, X, y, lambda)\n%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization\n%   J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using\n%   theta as the parameter for regularized logistic regression and the\n%   gradient of the cost w.r.t. to the parameters. \n\n% Initialize some useful values\nm = length(y); % number of training examples\n\n% You need to return the following variables correctly \nJ = 0;\ngrad = zeros(size(theta));\n\nh = sigmoid(X*theta);\ntheta1 = theta;\ntheta1(1) = 0;\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the cost of a particular choice of theta.\n%               You should set J to the cost.\n%               Compute the partial derivatives and set grad to the partial\n%               derivatives of the cost w.r.t. each parameter in theta\n\n\n\nJ = (-y'*log(h)-(1-y)'*log(1-h)+theta1'*theta1*lambda/2)/m;\n\ngrad = X'*(h-y)/m+lambda/m*theta1;\n\n\n\n% =============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex2/ex2/ex2.m",
    "content": "%% Machine Learning Online Class - Exercise 2: Logistic Regression\n%\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the logistic\n%  regression exercise. You will need to complete the following functions \n%  in this exericse:\n%\n%     sigmoid.m\n%     costFunction.m\n%     predict.m\n%     costFunctionReg.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% Load Data\n%  The first two columns contains the exam scores and the third column\n%  contains the label.\n\ndata = load('ex2data1.txt');\nX = data(:, [1, 2]); y = data(:, 3);\n\n%% ==================== Part 1: Plotting ====================\n%  We start the exercise by first plotting the data to understand the \n%  the problem we are working with.\n\nfprintf(['Plotting data with + indicating (y = 1) examples and o ' ...\n         'indicating (y = 0) examples.\\n']);\n\nplotData(X, y);\n\n% Put some labels \nhold on;\n% Labels and Legend\nxlabel('Exam 1 score')\nylabel('Exam 2 score')\n\n% Specified in plot order\nlegend('Admitted', 'Not admitted')\nhold off;\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n\n%% ============ Part 2: Compute Cost and Gradient ============\n%  In this part of the exercise, you will implement the cost and gradient\n%  for logistic regression. You neeed to complete the code in \n%  costFunction.m\n\n%  Setup the data matrix appropriately, and add ones for the intercept term\n[m, n] = size(X);\n\n% Add intercept term to x and X_test\nX = [ones(m, 1) X];\n\n% Initialize fitting parameters\ninitial_theta = zeros(n + 1, 1);\n\n% Compute and display initial cost and gradient\n[cost, grad] = costFunction(initial_theta, X, y);\n\nfprintf('Cost at initial theta (zeros): %f\\n', cost);\nfprintf('Gradient at initial theta (zeros): \\n');\nfprintf(' %f \\n', grad);\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n\n%% ============= Part 3: Optimizing using fminunc  =============\n%  In this exercise, you will use a built-in function (fminunc) to find the\n%  optimal parameters theta.\n\n%  Set options for fminunc\noptions = optimset('GradObj', 'on', 'MaxIter', 400);\n\n%  Run fminunc to obtain the optimal theta\n%  This function will return theta and the cost \n[theta, cost] = ...\n\tfminunc(@(t)(costFunction(t, X, y)), initial_theta, options);\n\n% Print theta to screen\nfprintf('Cost at theta found by fminunc: %f\\n', cost);\nfprintf('theta: \\n');\nfprintf(' %f \\n', theta);\n\n% Plot Boundary\nplotDecisionBoundary(theta, X, y);\n\n% Put some labels \nhold on;\n% Labels and Legend\nxlabel('Exam 1 score')\nylabel('Exam 2 score')\n\n% Specified in plot order\nlegend('Admitted', 'Not admitted')\nhold off;\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n%% ============== Part 4: Predict and Accuracies ==============\n%  After learning the parameters, you'll like to use it to predict the outcomes\n%  on unseen data. In this part, you will use the logistic regression model\n%  to predict the probability that a student with score 45 on exam 1 and \n%  score 85 on exam 2 will be admitted.\n%\n%  Furthermore, you will compute the training and test set accuracies of \n%  our model.\n%\n%  Your task is to complete the code in predict.m\n\n%  Predict probability for a student with score 45 on exam 1 \n%  and score 85 on exam 2 \n\nprob = sigmoid([1 45 85] * theta);\nfprintf(['For a student with scores 45 and 85, we predict an admission ' ...\n         'probability of %f\\n\\n'], prob);\n\n% Compute accuracy on our training set\np = predict(theta, X);\n\nfprintf('Train Accuracy: %f\\n', mean(double(p == y)) * 100);\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n"
  },
  {
    "path": "machine-learning-ex2/ex2/ex2_reg.m",
    "content": "%% Machine Learning Online Class - Exercise 2: Logistic Regression\n%\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the second part\n%  of the exercise which covers regularization with logistic regression.\n%\n%  You will need to complete the following functions in this exericse:\n%\n%     sigmoid.m\n%     costFunction.m\n%     predict.m\n%     costFunctionReg.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% Load Data\n%  The first two columns contains the X values and the third column\n%  contains the label (y).\n\ndata = load('ex2data2.txt');\nX = data(:, [1, 2]); y = data(:, 3);\n\nplotData(X, y);\n\n% Put some labels \nhold on;\n\n% Labels and Legend\nxlabel('Microchip Test 1')\nylabel('Microchip Test 2')\n\n% Specified in plot order\nlegend('y = 1', 'y = 0')\nhold off;\n\n\n%% =========== Part 1: Regularized Logistic Regression ============\n%  In this part, you are given a dataset with data points that are not\n%  linearly separable. However, you would still like to use logistic \n%  regression to classify the data points. \n%\n%  To do so, you introduce more features to use -- in particular, you add\n%  polynomial features to our data matrix (similar to polynomial\n%  regression).\n%\n\n% Add Polynomial Features\n\n% Note that mapFeature also adds a column of ones for us, so the intercept\n% term is handled\nX = mapFeature(X(:,1), X(:,2));\n%[m, n] = size(X);\n%X = [ones(m, 1) X];\n\n% Initialize fitting parameters\ninitial_theta = zeros(size(X, 2), 1);\n\n% Set regularization parameter lambda to 1\nlambda = 1;\n\n% Compute and display initial cost and gradient for regularized logistic\n% regression\n[cost, grad] = costFunctionReg(initial_theta, X, y, lambda);\n\nfprintf('Cost at initial theta (zeros): %f\\n', cost);\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n%% ============= Part 2: Regularization and Accuracies =============\n%  Optional Exercise:\n%  In this part, you will get to try different values of lambda and \n%  see how regularization affects the decision coundart\n%\n%  Try the following values of lambda (0, 1, 10, 100).\n%\n%  How does the decision boundary change when you vary lambda? How does\n%  the training set accuracy vary?\n%\n\n% Initialize fitting parameters\ninitial_theta = zeros(size(X, 2), 1);\n\n% Set regularization parameter lambda to 1 (you should vary this)\nlambda = 1;\n\n% Set Options\noptions = optimset('GradObj', 'on', 'MaxIter', 400);\n\n% Optimize\n[theta, J, exit_flag] = ...\n\tfminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);\n\n% Plot Boundary\nplotDecisionBoundary(theta, X, y);\nhold on;\ntitle(sprintf('lambda = %g', lambda))\n\n% Labels and Legend\nxlabel('Microchip Test 1')\nylabel('Microchip Test 2')\n\nlegend('y = 1', 'y = 0', 'Decision boundary')\nhold off;\n\n% Compute accuracy on our training set\np = predict(theta, X);\n\nfprintf('Train Accuracy: %f\\n', mean(double(p == y)) * 100);\n\n\n"
  },
  {
    "path": "machine-learning-ex2/ex2/ex2data1.txt",
    "content": "34.62365962451697,78.0246928153624,0\n30.28671076822607,43.89499752400101,0\n35.84740876993872,72.90219802708364,0\n60.18259938620976,86.30855209546826,1\n79.0327360507101,75.3443764369103,1\n45.08327747668339,56.3163717815305,0\n61.10666453684766,96.51142588489624,1\n75.02474556738889,46.55401354116538,1\n76.09878670226257,87.42056971926803,1\n84.43281996120035,43.53339331072109,1\n95.86155507093572,38.22527805795094,0\n75.01365838958247,30.60326323428011,0\n82.30705337399482,76.48196330235604,1\n69.36458875970939,97.71869196188608,1\n39.53833914367223,76.03681085115882,0\n53.9710521485623,89.20735013750205,1\n69.07014406283025,52.74046973016765,1\n67.94685547711617,46.67857410673128,0\n70.66150955499435,92.92713789364831,1\n76.97878372747498,47.57596364975532,1\n67.37202754570876,42.83843832029179,0\n89.67677575072079,65.79936592745237,1\n50.534788289883,48.85581152764205,0\n34.21206097786789,44.20952859866288,0\n77.9240914545704,68.9723599933059,1\n62.27101367004632,69.95445795447587,1\n80.1901807509566,44.82162893218353,1\n93.114388797442,38.80067033713209,0\n61.83020602312595,50.25610789244621,0\n38.78580379679423,64.99568095539578,0\n61.379289447425,72.80788731317097,1\n85.40451939411645,57.05198397627122,1\n52.10797973193984,63.12762376881715,0\n52.04540476831827,69.43286012045222,1\n40.23689373545111,71.16774802184875,0\n54.63510555424817,52.21388588061123,0\n33.91550010906887,98.86943574220611,0\n64.17698887494485,80.90806058670817,1\n74.78925295941542,41.57341522824434,0\n34.1836400264419,75.2377203360134,0\n83.90239366249155,56.30804621605327,1\n51.54772026906181,46.85629026349976,0\n94.44336776917852,65.56892160559052,1\n82.36875375713919,40.61825515970618,0\n51.04775177128865,45.82270145776001,0\n62.22267576120188,52.06099194836679,0\n77.19303492601364,70.45820000180959,1\n97.77159928000232,86.7278223300282,1\n62.07306379667647,96.76882412413983,1\n91.56497449807442,88.69629254546599,1\n79.94481794066932,74.16311935043758,1\n99.2725269292572,60.99903099844988,1\n90.54671411399852,43.39060180650027,1\n34.52451385320009,60.39634245837173,0\n50.2864961189907,49.80453881323059,0\n49.58667721632031,59.80895099453265,0\n97.64563396007767,68.86157272420604,1\n32.57720016809309,95.59854761387875,0\n74.24869136721598,69.82457122657193,1\n71.79646205863379,78.45356224515052,1\n75.3956114656803,85.75993667331619,1\n35.28611281526193,47.02051394723416,0\n56.25381749711624,39.26147251058019,0\n30.05882244669796,49.59297386723685,0\n44.66826172480893,66.45008614558913,0\n66.56089447242954,41.09209807936973,0\n40.45755098375164,97.53518548909936,1\n49.07256321908844,51.88321182073966,0\n80.27957401466998,92.11606081344084,1\n66.74671856944039,60.99139402740988,1\n32.72283304060323,43.30717306430063,0\n64.0393204150601,78.03168802018232,1\n72.34649422579923,96.22759296761404,1\n60.45788573918959,73.09499809758037,1\n58.84095621726802,75.85844831279042,1\n99.82785779692128,72.36925193383885,1\n47.26426910848174,88.47586499559782,1\n50.45815980285988,75.80985952982456,1\n60.45555629271532,42.50840943572217,0\n82.22666157785568,42.71987853716458,0\n88.9138964166533,69.80378889835472,1\n94.83450672430196,45.69430680250754,1\n67.31925746917527,66.58935317747915,1\n57.23870631569862,59.51428198012956,1\n80.36675600171273,90.96014789746954,1\n68.46852178591112,85.59430710452014,1\n42.0754545384731,78.84478600148043,0\n75.47770200533905,90.42453899753964,1\n78.63542434898018,96.64742716885644,1\n52.34800398794107,60.76950525602592,0\n94.09433112516793,77.15910509073893,1\n90.44855097096364,87.50879176484702,1\n55.48216114069585,35.57070347228866,0\n74.49269241843041,84.84513684930135,1\n89.84580670720979,45.35828361091658,1\n83.48916274498238,48.38028579728175,1\n42.2617008099817,87.10385094025457,1\n99.31500880510394,68.77540947206617,1\n55.34001756003703,64.9319380069486,1\n74.77589300092767,89.52981289513276,1\n"
  },
  {
    "path": "machine-learning-ex2/ex2/ex2data2.txt",
    "content": "0.051267,0.69956,1\n-0.092742,0.68494,1\n-0.21371,0.69225,1\n-0.375,0.50219,1\n-0.51325,0.46564,1\n-0.52477,0.2098,1\n-0.39804,0.034357,1\n-0.30588,-0.19225,1\n0.016705,-0.40424,1\n0.13191,-0.51389,1\n0.38537,-0.56506,1\n0.52938,-0.5212,1\n0.63882,-0.24342,1\n0.73675,-0.18494,1\n0.54666,0.48757,1\n0.322,0.5826,1\n0.16647,0.53874,1\n-0.046659,0.81652,1\n-0.17339,0.69956,1\n-0.47869,0.63377,1\n-0.60541,0.59722,1\n-0.62846,0.33406,1\n-0.59389,0.005117,1\n-0.42108,-0.27266,1\n-0.11578,-0.39693,1\n0.20104,-0.60161,1\n0.46601,-0.53582,1\n0.67339,-0.53582,1\n-0.13882,0.54605,1\n-0.29435,0.77997,1\n-0.26555,0.96272,1\n-0.16187,0.8019,1\n-0.17339,0.64839,1\n-0.28283,0.47295,1\n-0.36348,0.31213,1\n-0.30012,0.027047,1\n-0.23675,-0.21418,1\n-0.06394,-0.18494,1\n0.062788,-0.16301,1\n0.22984,-0.41155,1\n0.2932,-0.2288,1\n0.48329,-0.18494,1\n0.64459,-0.14108,1\n0.46025,0.012427,1\n0.6273,0.15863,1\n0.57546,0.26827,1\n0.72523,0.44371,1\n0.22408,0.52412,1\n0.44297,0.67032,1\n0.322,0.69225,1\n0.13767,0.57529,1\n-0.0063364,0.39985,1\n-0.092742,0.55336,1\n-0.20795,0.35599,1\n-0.20795,0.17325,1\n-0.43836,0.21711,1\n-0.21947,-0.016813,1\n-0.13882,-0.27266,1\n0.18376,0.93348,0\n0.22408,0.77997,0\n0.29896,0.61915,0\n0.50634,0.75804,0\n0.61578,0.7288,0\n0.60426,0.59722,0\n0.76555,0.50219,0\n0.92684,0.3633,0\n0.82316,0.27558,0\n0.96141,0.085526,0\n0.93836,0.012427,0\n0.86348,-0.082602,0\n0.89804,-0.20687,0\n0.85196,-0.36769,0\n0.82892,-0.5212,0\n0.79435,-0.55775,0\n0.59274,-0.7405,0\n0.51786,-0.5943,0\n0.46601,-0.41886,0\n0.35081,-0.57968,0\n0.28744,-0.76974,0\n0.085829,-0.75512,0\n0.14919,-0.57968,0\n-0.13306,-0.4481,0\n-0.40956,-0.41155,0\n-0.39228,-0.25804,0\n-0.74366,-0.25804,0\n-0.69758,0.041667,0\n-0.75518,0.2902,0\n-0.69758,0.68494,0\n-0.4038,0.70687,0\n-0.38076,0.91886,0\n-0.50749,0.90424,0\n-0.54781,0.70687,0\n0.10311,0.77997,0\n0.057028,0.91886,0\n-0.10426,0.99196,0\n-0.081221,1.1089,0\n0.28744,1.087,0\n0.39689,0.82383,0\n0.63882,0.88962,0\n0.82316,0.66301,0\n0.67339,0.64108,0\n1.0709,0.10015,0\n-0.046659,-0.57968,0\n-0.23675,-0.63816,0\n-0.15035,-0.36769,0\n-0.49021,-0.3019,0\n-0.46717,-0.13377,0\n-0.28859,-0.060673,0\n-0.61118,-0.067982,0\n-0.66302,-0.21418,0\n-0.59965,-0.41886,0\n-0.72638,-0.082602,0\n-0.83007,0.31213,0\n-0.72062,0.53874,0\n-0.59389,0.49488,0\n-0.48445,0.99927,0\n-0.0063364,0.99927,0\n0.63265,-0.030612,0\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/AUTHORS.txt",
    "content": "The author of \"jsonlab\" toolbox is Qianqian Fang. Qianqian\nis currently an Assistant Professor at Massachusetts General Hospital, \nHarvard Medical School.\n\nAddress: Martinos Center for Biomedical Imaging, \n         Massachusetts General Hospital, \n         Harvard Medical School\n         Bldg 149, 13th St, Charlestown, MA 02129, USA\nURL: http://nmr.mgh.harvard.edu/~fangq/\nEmail: <fangq at nmr.mgh.harvard.edu> or <fangqq at gmail.com>\n\n\nThe script loadjson.m was built upon previous works by\n\n- Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n       date: 2009/11/02\n- François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n       date: 2009/03/22\n- Joel Feenstra: http://www.mathworks.com/matlabcentral/fileexchange/20565\n       date: 2008/07/03\n\n\nThis toolbox contains patches submitted by the following contributors:\n\n- Blake Johnson <bjohnso at bbn.com>\n  part of revision 341\n\n- Niclas Borlin <Niclas.Borlin at cs.umu.se>\n  various fixes in revision 394, including\n  - loadjson crashes for all-zero sparse matrix.\n  - loadjson crashes for empty sparse matrix.\n  - Non-zero size of 0-by-N and N-by-0 empty matrices is lost after savejson/loadjson.\n  - loadjson crashes for sparse real column vector.\n  - loadjson crashes for sparse complex column vector.\n  - Data is corrupted by savejson for sparse real row vector.\n  - savejson crashes for sparse complex row vector. \n\n- Yul Kang <yul.kang.on at gmail.com>\n  patches for svn revision 415.\n  - savejson saves an empty cell array as [] instead of null\n  - loadjson differentiates an empty struct from an empty array\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/ChangeLog.txt",
    "content": "============================================================================\n\n   JSONlab - a toolbox to encode/decode JSON/UBJSON files in MATLAB/Octave\n\n----------------------------------------------------------------------------\n\nJSONlab ChangeLog (key features marked by *):\n\n== JSONlab 1.0 (codename: Optimus - Final), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2015/01/02 polish help info for all major functions, update examples, finalize 1.0\n 2014/12/19 fix a bug to strictly respect NoRowBracket in savejson\n\n== JSONlab 1.0.0-RC2 (codename: Optimus - RC2), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/11/22 show progress bar in loadjson ('ShowProgress') \n 2014/11/17 add Compact option in savejson to output compact JSON format ('Compact')\n 2014/11/17 add FastArrayParser in loadjson to specify fast parser applicable levels\n 2014/09/18 start official github mirror: https://github.com/fangq/jsonlab\n\n== JSONlab 1.0.0-RC1 (codename: Optimus - RC1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/09/17  fix several compatibility issues when running on octave versions 3.2-3.8\n 2014/09/17  support 2D cell and struct arrays in both savejson and saveubjson\n 2014/08/04  escape special characters in a JSON string\n 2014/02/16  fix a bug when saving ubjson files\n\n== JSONlab 0.9.9 (codename: Optimus - beta), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/01/22  use binary read and write in saveubjson and loadubjson\n\n== JSONlab 0.9.8-1 (codename: Optimus - alpha update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2013/10/07 better round-trip conservation for empty arrays and structs (patch submitted by Yul Kang)\n\n== JSONlab 0.9.8 (codename: Optimus - alpha), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2013/08/23 *universal Binary JSON (UBJSON) support, including both saveubjson and loadubjson\n\n== JSONlab 0.9.1 (codename: Rodimus, update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2012/12/18 *handling of various empty and sparse matrices (fixes submitted by Niclas Borlin)\n\n== JSONlab 0.9.0 (codename: Rodimus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/06/17 *new format for an invalid leading char, unpacking hex code in savejson\n 2012/06/01  support JSONP in savejson\n 2012/05/25  fix the empty cell bug (reported by Cyril Davin)\n 2012/04/05  savejson can save to a file (suggested by Patrick Rapin)\n\n== JSONlab 0.8.1 (codename: Sentiel, Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/02/28  loadjson quotation mark escape bug, see http://bit.ly/yyk1nS\n 2012/01/25  patch to handle root-less objects, contributed by Blake Johnson\n\n== JSONlab 0.8.0 (codename: Sentiel), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/01/13 *speed up loadjson by 20 fold when parsing large data arrays in matlab\n 2012/01/11  remove row bracket if an array has 1 element, suggested by Mykel Kochenderfer\n 2011/12/22 *accept sequence of 'param',value input in savejson and loadjson\n 2011/11/18  fix struct array bug reported by Mykel Kochenderfer\n\n== JSONlab 0.5.1 (codename: Nexus Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/21  fix a bug in loadjson, previous code does not use any of the acceleration\n 2011/10/20  loadjson supports JSON collections - concatenated JSON objects\n\n== JSONlab 0.5.0 (codename: Nexus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/16  package and release jsonlab 0.5.0\n 2011/10/15 *add json demo and regression test, support cpx numbers, fix double quote bug\n 2011/10/11 *speed up readjson dramatically, interpret _Array* tags, show data in root level\n 2011/10/10  create jsonlab project, start jsonlab website, add online documentation\n 2011/10/07 *speed up savejson by 25x using sprintf instead of mat2str, add options support\n 2011/10/06 *savejson works for structs, cells and arrays\n 2011/09/09  derive loadjson from JSON parser from MATLAB Central, draft savejson.m\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/LICENSE_BSD.txt",
    "content": "Copyright 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are\npermitted provided that the following conditions are met:\n\n   1. Redistributions of source code must retain the above copyright notice, this list of\n      conditions and the following disclaimer.\n\n   2. Redistributions in binary form must reproduce the above copyright notice, this list\n      of conditions and the following disclaimer in the documentation and/or other materials\n      provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS \nOR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nThe views and conclusions contained in the software and documentation are those of the\nauthors and should not be interpreted as representing official policies, either expressed\nor implied, of the copyright holders.\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/README.txt",
    "content": "===============================================================================\n=                                 JSONLab                                     =\n=           An open-source MATLAB/Octave JSON encoder and decoder             =\n===============================================================================\n\n*Copyright (C) 2011-2015  Qianqian Fang <fangq at nmr.mgh.harvard.edu>\n*License: BSD License, see License_BSD.txt for details\n*Version: 1.0 (Optimus - Final)\n\n-------------------------------------------------------------------------------\n\nTable of Content:\n\nI.  Introduction\nII. Installation\nIII.Using JSONLab\nIV. Known Issues and TODOs\nV.  Contribution and feedback\n\n-------------------------------------------------------------------------------\n\nI.  Introduction\n\nJSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable, \nhuman-readable and \"[http://en.wikipedia.org/wiki/JSON fat-free]\" text format \nto represent complex and hierarchical data. It is as powerful as \n[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely \nused for data-exchange in applications, and is essential for the wild success \nof [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and \n[http://en.wikipedia.org/wiki/Web_2.0 Web2.0]. \n\nUBJSON (Universal Binary JSON) is a binary JSON format, specifically \noptimized for compact file size and better performance while keeping\nthe semantics as simple as the text-based JSON format. Using the UBJSON\nformat allows to wrap complex binary data in a flexible and extensible\nstructure, making it possible to process complex and large dataset \nwithout accuracy loss due to text conversions.\n\nWe envision that both JSON and its binary version will serve as part of \nthe mainstream data-exchange formats for scientific research in the future. \nIt will provide the flexibility and generality achieved by other popular \ngeneral-purpose file specifications, such as\n[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly \nreduced complexity and enhanced performance.\n\nJSONLab is a free and open-source implementation of a JSON/UBJSON encoder \nand a decoder in the native MATLAB language. It can be used to convert a MATLAB \ndata structure (array, struct, cell, struct array and cell array) into \nJSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB \ndata structure. JSONLab supports both MATLAB and  \n[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).\n\n-------------------------------------------------------------------------------\n\nII. Installation\n\nThe installation of JSONLab is no different than any other simple\nMATLAB toolbox. You only need to download/unzip the JSONLab package\nto a folder, and add the folder's path to MATLAB/Octave's path list\nby using the following command:\n\n    addpath('/path/to/jsonlab');\n\nIf you want to add this path permanently, you need to type \"pathtool\", \nbrowse to the jsonlab root folder and add to the list, then click \"Save\".\nThen, run \"rehash\" in MATLAB, and type \"which loadjson\", if you see an \noutput, that means JSONLab is installed for MATLAB/Octave.\n\n-------------------------------------------------------------------------------\n\nIII.Using JSONLab\n\nJSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder, \nand savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and \ntwo equivallent functions -- loadubjson and saveubjson for the binary \nJSON. The detailed help info for the four functions can be found below:\n\n=== loadjson.m ===\n<pre>\n  data=loadjson(fname,opt)\n     or\n  data=loadjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09, including previous works from \n \n          Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n             created on 2009/11/02\n          Franois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n             created on  2009/03/22\n          Joel Feenstra:\n          http://www.mathworks.com/matlabcentral/fileexchange/20565\n             created on 2008/07/03\n \n  $Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a JSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n                          speed-optimized array parser when loading an \n                          array object. The fast array parser may \n                          collapse block arrays into a single large\n                          array similar to rules defined in cell2mat; 0 to \n                          use a legacy parser; if set to a larger-than-1\n                          value, this option will specify the minimum\n                          dimension to enable the fast array parser. For\n                          example, if the input is a 3D array, setting\n                          FastArrayParser to 1 will return a 3D array;\n                          setting to 2 will return a cell array of 2D\n                          arrays; setting to 3 will return to a 2D cell\n                          array of 1D vectors; setting to 4 will return a\n                          3D cell array.\n            opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n       dat=loadjson(['examples' filesep 'example1.json'])\n       dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n</pre>\n\n=== savejson.m ===\n\n<pre>\n  json=savejson(rootname,obj,filename)\n     or\n  json=savejson(rootname,obj,opt)\n  json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n  Object Notation) string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09\n \n  $Id: savejson.m 458 2014-12-19 22:17:17Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array).\n       filename: a string for the file name to save the output JSON data.\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n                          of a 1D/2D array;\n         opt.ArrayIndent [1|0]: if 1, output explicit data array with\n                          precedent indentation; if 0, no indentation\n         opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n                          to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n                          and $1 represents the sign. For those who want to use\n                          1e999 to represent Inf, they can set opt.Inf to '$11e999'\n         opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n                          to represent NaN\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSONP='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n         opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n         opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a string in the JSON format (see http://json.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       savejson('jmesh',jsonmesh)\n       savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n </pre>\n\n=== loadubjson.m ===\n\n<pre>\n  data=loadubjson(fname,opt)\n     or\n  data=loadubjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/01\n \n  $Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a UBJSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.IntEndian [B|L]: specify the endianness of the integer fields\n                          in the UBJSON input data. B - Big-Endian format for \n                          integers (as required in the UBJSON specification); \n                          L - input integer fields are in Little-Endian order.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       obj=struct('string','value','array',[1 2 3]);\n       ubjdata=saveubjson('obj',obj);\n       dat=loadubjson(ubjdata)\n       dat=loadubjson(['examples' filesep 'example1.ubj'])\n       dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n</pre>\n\n=== saveubjson.m ===\n\n<pre>\n  json=saveubjson(rootname,obj,filename)\n     or\n  json=saveubjson(rootname,obj,opt)\n  json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a Universal \n  Binary JSON (UBJSON) binary string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/17\n \n  $Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array)\n       filename: a string for the file name to save the output UBJSON data\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSON='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a binary string in the UBJSON format (see http://ubjson.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       saveubjson('jsonmesh',jsonmesh)\n       saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n</pre>\n\n\n=== examples ===\n\nUnder the \"examples\" folder, you can find several scripts to demonstrate the\nbasic utilities of JSONLab. Running the \"demo_jsonlab_basic.m\" script, you \nwill see the conversions from MATLAB data structure to JSON text and backward.\nIn \"jsonlab_selftest.m\", we load complex JSON files downloaded from the Internet\nand validate the loadjson/savejson functions for regression testing purposes.\nSimilarly, a \"demo_ubjson_basic.m\" script is provided to test the saveubjson\nand loadubjson pairs for various matlab data structures.\n\nPlease run these examples and understand how JSONLab works before you use\nit to process your data.\n\n-------------------------------------------------------------------------------\n\nIV. Known Issues and TODOs\n\nJSONLab has several known limitations. We are striving to make it more general\nand robust. Hopefully in a few future releases, the limitations become less.\n\nHere are the known issues:\n\n# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;\n# When processing names containing multi-byte characters, Octave and MATLAB \\\ncan give different field-names; you can use feature('DefaultCharacterSet','latin1') \\\nin MATLAB to get consistant results\n# savejson can not handle class and dataset.\n# saveubjson converts a logical array into a uint8 ([U]) array\n# an unofficial N-D array count syntax is implemented in saveubjson. We are \\\nactively communicating with the UBJSON spec maintainer to investigate the \\\npossibility of making it upstream\n# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \\\nfiles, however, it can parse all UBJSON files produced by saveubjson.\n\n-------------------------------------------------------------------------------\n\nV. Contribution and feedback\n\nJSONLab is an open-source project. This means you can not only use it and modify\nit as you wish, but also you can contribute your changes back to JSONLab so\nthat everyone else can enjoy the improvement. For anyone who want to contribute,\nplease download JSONLab source code from it's subversion repository by using the\nfollowing command:\n\n svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab\n\nYou can make changes to the files as needed. Once you are satisfied with your\nchanges, and ready to share it with others, please cd the root directory of \nJSONLab, and type\n\n svn diff > yourname_featurename.patch\n\nYou then email the .patch file to JSONLab's maintainer, Qianqian Fang, at\nthe email address shown in the beginning of this file. Qianqian will review \nthe changes and commit it to the subversion if they are satisfactory.\n\nWe appreciate any suggestions and feedbacks from you. Please use iso2mesh's\nmailing list to report any questions you may have with JSONLab:\n\nhttp://groups.google.com/group/iso2mesh-users?hl=en&pli=1\n\n(Subscription to the mailing list is needed in order to post messages).\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/jsonopt.m",
    "content": "function val=jsonopt(key,default,varargin)\n%\n% val=jsonopt(key,default,optstruct)\n%\n% setting options based on a struct. The struct can be produced\n% by varargin2struct from a list of 'param','value' pairs\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n%\n% $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $\n%\n% input:\n%      key: a string with which one look up a value from a struct\n%      default: if the key does not exist, return default\n%      optstruct: a struct where each sub-field is a key \n%\n% output:\n%      val: if key exists, val=optstruct.key; otherwise val=default\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n% \n\nval=default;\nif(nargin<=2) return; end\nopt=varargin{1};\nif(isstruct(opt) && isfield(opt,key))\n    val=getfield(opt,key);\nend\n\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/loadjson.m",
    "content": "function data = loadjson(fname,varargin)\n%\n% data=loadjson(fname,opt)\n%    or\n% data=loadjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09, including previous works from \n%\n%         Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n%            created on 2009/11/02\n%         François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n%            created on  2009/03/22\n%         Joel Feenstra:\n%         http://www.mathworks.com/matlabcentral/fileexchange/20565\n%            created on 2008/07/03\n%\n% $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a JSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n%                         speed-optimized array parser when loading an \n%                         array object. The fast array parser may \n%                         collapse block arrays into a single large\n%                         array similar to rules defined in cell2mat; 0 to \n%                         use a legacy parser; if set to a larger-than-1\n%                         value, this option will specify the minimum\n%                         dimension to enable the fast array parser. For\n%                         example, if the input is a 3D array, setting\n%                         FastArrayParser to 1 will return a 3D array;\n%                         setting to 2 will return a cell array of 2D\n%                         arrays; setting to 3 will return to a 2D cell\n%                         array of 1D vectors; setting to 4 will return a\n%                         3D cell array.\n%           opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n%      dat=loadjson(['examples' filesep 'example1.json'])\n%      dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\n\nif(jsonopt('ShowProgress',0,opt)==1)\n    opt.progressbar_=waitbar(0,'loading ...');\nend\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\nif(isfield(opt,'progressbar_'))\n    close(opt.progressbar_);\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=data(j).x0x5F_ArraySize_;\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    if next_char ~= '}'\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            parse_char(':');\n            val = parse_value(varargin{:});\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}'\n                break;\n            end\n            parse_char(',');\n        end\n    end\n    parse_char('}');\n\n%%-------------------------------------------------------------------------\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim2=[];\n    arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:});\n    pbar=jsonopt('progressbar_',-1,varargin{:});\n\n    if next_char ~= ']'\n\tif(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))\n            [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos);\n            arraystr=['[' inStr(pos:endpos)];\n            arraystr=regexprep(arraystr,'\"_NaN_\"','NaN');\n            arraystr=regexprep(arraystr,'\"([-+]*)_Inf_\"','$1Inf');\n            arraystr(arraystr==sprintf('\\n'))=[];\n            arraystr(arraystr==sprintf('\\r'))=[];\n            %arraystr=regexprep(arraystr,'\\s*,',','); % this is slow,sometimes needed\n            if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D\n        \tastr=inStr((e1l+1):(e1r-1));\n        \tastr=regexprep(astr,'\"_NaN_\"','NaN');\n        \tastr=regexprep(astr,'\"([-+]*)_Inf_\"','$1Inf');\n        \tastr(astr==sprintf('\\n'))=[];\n        \tastr(astr==sprintf('\\r'))=[];\n        \tastr(astr==' ')='';\n        \tif(isempty(find(astr=='[', 1))) % array is 2D\n                    dim2=length(sscanf(astr,'%f,',[1 inf]));\n        \tend\n            else % array is 1D\n        \tastr=arraystr(2:end-1);\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]);\n        \tif(nextidx>=length(astr)-1)\n                    object=obj;\n                    pos=endpos;\n                    parse_char(']');\n                    return;\n        \tend\n            end\n            if(~isempty(dim2))\n        \tastr=arraystr;\n        \tastr(astr=='[')='';\n        \tastr(astr==']')='';\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf);\n        \tif(nextidx>=length(astr)-1)\n                    object=reshape(obj,dim2,numel(obj)/dim2)';\n                    pos=endpos;\n                    parse_char(']');\n                    if(pbar>0)\n                        waitbar(pos/length(inStr),pbar,'loading ...');\n                    end\n                    return;\n        \tend\n            end\n            arraystr=regexprep(arraystr,'\\]\\s*,','];');\n\telse\n            arraystr='[';\n\tend\n        try\n           if(isoct && regexp(arraystr,'\"','once'))\n                error('Octave eval can produce empty cells for JSON-like input');\n           end\n           object=eval(arraystr);\n           pos=endpos;\n        catch\n         while 1\n            newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1);\n            val = parse_value(newopt);\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            parse_char(',');\n         end\n        end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    parse_char(']');\n    \n    if(pbar>0)\n        waitbar(pos/length(inStr),pbar,'loading ...');\n    end\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr len  esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    if inStr(pos) ~= '\"'\n        error_pos('String starting with \" expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    str = '';\n    while pos <= len\n        while index_esc <= len_esc && esc(index_esc) < pos\n            index_esc = index_esc + 1;\n        end\n        if index_esc > len_esc\n            str = [str inStr(pos:len)];\n            pos = len + 1;\n            break;\n        else\n            str = [str inStr(pos:esc(index_esc)-1)];\n            pos = esc(index_esc);\n        end\n        nstr = length(str); switch inStr(pos)\n            case '\"'\n                pos = pos + 1;\n                if(~isempty(str))\n                    if(strcmp(str,'_Inf_'))\n                        str=Inf;\n                    elseif(strcmp(str,'-_Inf_'))\n                        str=-Inf;\n                    elseif(strcmp(str,'_NaN_'))\n                        str=NaN;\n                    end\n                end\n                return;\n            case '\\'\n                if pos+1 > len\n                    error_pos('End of file reached right after escape character');\n                end\n                pos = pos + 1;\n                switch inStr(pos)\n                    case {'\"' '\\' '/'}\n                        str(nstr+1) = inStr(pos);\n                        pos = pos + 1;\n                    case {'b' 'f' 'n' 'r' 't'}\n                        str(nstr+1) = sprintf(['\\' inStr(pos)]);\n                        pos = pos + 1;\n                    case 'u'\n                        if pos+4 > len\n                            error_pos('End of file reached in escaped unicode character');\n                        end\n                        str(nstr+(1:6)) = inStr(pos-1:pos+4);\n                        pos = pos + 5;\n                end\n            otherwise % should never happen\n                str(nstr+1) = inStr(pos), keyboard\n                pos = pos + 1;\n        end\n    end\n    error_pos('End of file while expecting end of inStr');\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct\n    currstr=inStr(pos:end);\n    numstr=0;\n    if(isoct~=0)\n        numstr=regexp(currstr,'^\\s*-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+\\-]?\\d+)?','end');\n        [num, one] = sscanf(currstr, '%f', 1);\n        delta=numstr+1;\n    else\n        [num, one, err, delta] = sscanf(currstr, '%f', 1);\n        if ~isempty(err)\n            error_pos('Error reading number at position %d');\n        end\n    end\n    pos = pos + delta-1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n    \n    pbar=jsonopt('progressbar_',-1,varargin{:});\n    if(pbar>0)\n        waitbar(pos/len,pbar,'loading ...');\n    end\n    \n    switch(inStr(pos))\n        case '\"'\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'-','0','1','2','3','4','5','6','7','8','9'}\n            val = parse_number(varargin{:});\n            return;\n        case 't'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true')\n                val = true;\n                pos = pos + 4;\n                return;\n            end\n        case 'f'\n            if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false')\n                val = false;\n                pos = pos + 5;\n                return;\n            end\n        case 'n'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null')\n                val = [];\n                pos = pos + 4;\n                return;\n            end\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/loadubjson.m",
    "content": "function data = loadubjson(fname,varargin)\n%\n% data=loadubjson(fname,opt)\n%    or\n% data=loadubjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/01\n%\n% $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a UBJSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.IntEndian [B|L]: specify the endianness of the integer fields\n%                         in the UBJSON input data. B - Big-Endian format for \n%                         integers (as required in the UBJSON specification); \n%                         L - input integer fields are in Little-Endian order.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      obj=struct('string','value','array',[1 2 3]);\n%      ubjdata=saveubjson('obj',obj);\n%      dat=loadubjson(ubjdata)\n%      dat=loadubjson(['examples' filesep 'example1.ubj'])\n%      dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken fileendian systemendian\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\nfileendian=upper(jsonopt('IntEndian','B',opt));\n[os,maxelem,systemendian]=computer;\n\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\n\n\n%%\nfunction newdata=parse_collection(id,data,obj)\n\nif(jsoncount>0 && exist('data','var')) \n    if(~iscell(data))\n       newdata=cell(1);\n       newdata{1}=data;\n       data=newdata;\n    end\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=double(data(j).x0x5F_ArraySize_);\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1); % TODO\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        count=double(parse_number());\n    end\n    if next_char ~= '}'\n        num=0;\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            %parse_char(':');\n            val = parse_value(varargin{:});\n            num=num+1;\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}' || (count>=0 && num>=count)\n                break;\n            end\n            %parse_char(',');\n        end\n    end\n    if(count==-1)\n        parse_char('}');\n    end\n\n%%-------------------------------------------------------------------------\nfunction [cid,len]=elem_info(type)\nid=strfind('iUIlLdD',type);\ndataclass={'int8','uint8','int16','int32','int64','single','double'};\nbytelen=[1,1,2,4,8,4,8];\nif(id>0)\n    cid=dataclass{id};\n    len=bytelen(id);\nelse\n    error_pos('unsupported type at position %d');\nend\n%%-------------------------------------------------------------------------\n\n\nfunction [data adv]=parse_block(type,count,varargin)\nglobal pos inStr isoct fileendian systemendian\n[cid,len]=elem_info(type);\ndatastr=inStr(pos:pos+len*count-1);\nif(isoct)\n    newdata=int8(datastr);\nelse\n    newdata=uint8(datastr);\nend\nid=strfind('iUIlLdD',type);\nif(id<=5 && fileendian~=systemendian)\n    newdata=swapbytes(typecast(newdata,cid));\nend\ndata=typecast(newdata,cid);\nadv=double(len*count);\n\n%%-------------------------------------------------------------------------\n\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim=[];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1);\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        if(next_char=='[')\n            dim=parse_array(varargin{:});\n            count=prod(double(dim));\n        else\n            count=double(parse_number());\n        end\n    end\n    if(~isempty(type))\n        if(count>=0)\n            [object adv]=parse_block(type,count,varargin{:});\n            if(~isempty(dim))\n                object=reshape(object,dim);\n            end\n            pos=pos+adv;\n            return;\n        else\n            endpos=matching_bracket(inStr,pos);\n            [cid,len]=elem_info(type);\n            count=(endpos-pos)/len;\n            [object adv]=parse_block(type,count,varargin{:});\n            pos=pos+adv;\n            parse_char(']');\n            return;\n        end\n    end\n    if next_char ~= ']'\n         while 1\n            val = parse_value(varargin{:});\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            %parse_char(',');\n         end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    if(count==-1)\n        parse_char(']');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    type=inStr(pos);\n    if type ~= 'S' && type ~= 'C' && type ~= 'H'\n        error_pos('String starting with S expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    if(type == 'C')\n        str=inStr(pos);\n        pos=pos+1;\n        return;\n    end\n    bytelen=double(parse_number());\n    if(length(inStr)>=pos+bytelen-1)\n        str=inStr(pos:pos+bytelen-1);\n        pos=pos+bytelen;\n    else\n        error_pos('End of file while expecting end of inStr');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct fileendian systemendian\n    id=strfind('iUIlLdD',inStr(pos));\n    if(isempty(id))\n        error_pos('expecting a number at position %d');\n    end\n    type={'int8','uint8','int16','int32','int64','single','double'};\n    bytelen=[1,1,2,4,8,4,8];\n    datastr=inStr(pos+1:pos+bytelen(id));\n    if(isoct)\n        newdata=int8(datastr);\n    else\n        newdata=uint8(datastr);\n    end\n    if(id<=5 && fileendian~=systemendian)\n        newdata=swapbytes(typecast(newdata,type{id}));\n    end\n    num=typecast(newdata,type{id});\n    pos = pos + bytelen(id)+1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n\n    switch(inStr(pos))\n        case {'S','C','H'}\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'i','U','I','l','L','d','D'}\n            val = parse_number(varargin{:});\n            return;\n        case 'T'\n            val = true;\n            pos = pos + 1;\n            return;\n        case 'F'\n            val = false;\n            pos = pos + 1;\n            return;\n        case {'Z','N'}\n            val = [];\n            pos = pos + 1;\n            return;\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos e1l e1r maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/mergestruct.m",
    "content": "function s=mergestruct(s1,s2)\n%\n% s=mergestruct(s1,s2)\n%\n% merge two struct objects into one\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      s1,s2: a struct object, s1 and s2 can not be arrays\n%\n% output:\n%      s: the merged struct object. fields in s1 and s2 will be combined in s.\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(~isstruct(s1) || ~isstruct(s2))\n    error('input parameters contain non-struct');\nend\nif(length(s1)>1 || length(s2)>1)\n    error('can not merge struct arrays');\nend\nfn=fieldnames(s2);\ns=s1;\nfor i=1:length(fn)              \n    s=setfield(s,fn{i},getfield(s2,fn{i}));\nend\n\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/savejson.m",
    "content": "function json=savejson(rootname,obj,varargin)\n%\n% json=savejson(rootname,obj,filename)\n%    or\n% json=savejson(rootname,obj,opt)\n% json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n% Object Notation) string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09\n%\n% $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array).\n%      filename: a string for the file name to save the output JSON data.\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n%                         of a 1D/2D array;\n%        opt.ArrayIndent [1|0]: if 1, output explicit data array with\n%                         precedent indentation; if 0, no indentation\n%        opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n%                         to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n%                         and $1 represents the sign. For those who want to use\n%                         1e999 to represent Inf, they can set opt.Inf to '$11e999'\n%        opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n%                         to represent NaN\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSONP='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%        opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n%        opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a string in the JSON format (see http://json.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      savejson('jmesh',jsonmesh)\n%      savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\n\nwhitespaces=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nif(jsonopt('Compact',0,opt)==1)\n    whitespaces=struct('tab','','newline','','sep',',');\nend\nif(~isfield(opt,'whitespaces_'))\n    opt.whitespaces_=whitespaces;\nend\n\nnl=whitespaces.newline;\n\njson=obj2json(rootname,obj,rootlevel,opt);\nif(rootisarray)\n    json=sprintf('%s%s',json,nl);\nelse\n    json=sprintf('{%s%s%s}\\n',nl,json,nl);\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=sprintf('%s(%s);%s',jsonp,json,nl);\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    if(jsonopt('SaveBinary',0,opt)==1)\n\t    fid = fopen(opt.FileName, 'wb');\n\t    fwrite(fid,json);\n    else\n\t    fid = fopen(opt.FileName, 'wt');\n\t    fwrite(fid,json,'char');\n    end\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2json(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2json(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2json(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2json(name,item,level,varargin{:});\nelse\n    txt=mat2json(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2json(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=jsonopt('whitespaces_',struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n')),varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nif(len>1)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": [%s',padding0, checkname(name,varargin{:}),nl); name=''; \n    else\n        txt=sprintf('%s[%s',padding0,nl); \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": []',padding0, checkname(name,varargin{:})); name=''; \n    else\n        txt=sprintf('%s[]',padding0); \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n    for i=1:dim(1)\n       txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:}));\n       if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    end\n    if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n    if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2json(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\npadding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1));\nnl=ws.newline;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding0,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding0,nl); end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n  for i=1:dim(1)\n    names = fieldnames(item(i,j));\n    if(~isempty(name) && len==1)\n        txt=sprintf('%s%s\"%s\": {%s',txt,padding1, checkname(name,varargin{:}),nl); \n    else\n        txt=sprintf('%s%s{%s',txt,padding1,nl); \n    end\n    if(~isempty(names))\n      for e=1:length(names)\n\t    txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:}));\n        if(e<length(names)) txt=sprintf('%s%s',txt,','); end\n        txt=sprintf('%s%s',txt,nl);\n      end\n    end\n    txt=sprintf('%s%s}',txt,padding1);\n    if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n  end\n  if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n  if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2json(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding1,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding1,nl); end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    if(isoct)\n        val=regexprep(item(e,:),'\\\\','\\\\');\n        val=regexprep(val,'\"','\\\"');\n        val=regexprep(val,'^\"','\\\"');\n    else\n        val=regexprep(item(e,:),'\\\\','\\\\\\\\');\n        val=regexprep(val,'\"','\\\\\"');\n        val=regexprep(val,'^\"','\\\\\"');\n    end\n    val=escapejsonstring(val);\n    if(len==1)\n        obj=['\"' checkname(name,varargin{:}) '\": ' '\"',val,'\"'];\n\tif(isempty(name)) obj=['\"',val,'\"']; end\n        txt=sprintf('%s%s%s%s',txt,padding1,obj);\n    else\n        txt=sprintf('%s%s%s%s',txt,padding0,['\"',val,'\"']);\n    end\n    if(e==len) sep=''; end\n    txt=sprintf('%s%s',txt,sep);\nend\nif(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2json(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:}))\n    if(isempty(name))\n    \ttxt=sprintf('%s{%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    else\n    \ttxt=sprintf('%s\"%s\": {%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    end\nelse\n    if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0)\n        numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\\[',''),']','');\n    else\n        numtxt=matdata2json(item,level+1,varargin{:});\n    end\n    if(isempty(name))\n    \ttxt=sprintf('%s%s',padding1,numtxt);\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n           \ttxt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        else\n    \t    txt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        end\n    end\n    return;\nend\ndataformat='%s%s%s%s%s';\n\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n    end\n    txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsSparse_\": ','1', sep);\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([iy(:),data'],level+2,varargin{:}), nl);\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,data],level+2,varargin{:}), nl);\n    else\n        % General case, store row and column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,iy,data],level+2,varargin{:}), nl);\n    end\nelse\n    if(isreal(item))\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json(item(:)',level+2,varargin{:}), nl);\n    else\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl);\n    end\nend\ntxt=sprintf('%s%s%s',txt,padding1,'}');\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2json(mat,level,varargin)\n\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\ntab=ws.tab;\nnl=ws.newline;\n\nif(size(mat,1)==1)\n    pre='';\n    post='';\n    level=level-1;\nelse\n    pre=sprintf('[%s',nl);\n    post=sprintf('%s%s]',nl,repmat(tab,1,level-1));\nend\n\nif(isempty(mat))\n    txt='null';\n    return;\nend\nfloatformat=jsonopt('FloatFormat','%.10g',varargin{:});\n%if(numel(mat)>1)\n    formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]];\n%else\n%    formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\\n')]];\n%end\n\nif(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1)\n    formatstr=[repmat(tab,1,level) formatstr];\nend\n\ntxt=sprintf(formatstr,mat');\ntxt(end-length(nl):end)=[];\nif(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1)\n   txt=regexprep(txt,'1','true');\n   txt=regexprep(txt,'0','false');\nend\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],\\n['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\ntxt=[pre txt post];\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n\n%%-------------------------------------------------------------------------\nfunction newstr=escapejsonstring(str)\nnewstr=str;\nisoct=exist('OCTAVE_VERSION','builtin');\nif(isoct)\n   vv=sscanf(OCTAVE_VERSION,'%f');\n   if(vv(1)>=3.8) isoct=0; end\nend\nif(isoct)\n  escapechars={'\\a','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},escapechars{i});\n  end\nelse\n  escapechars={'\\a','\\b','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\\\','\\\\\\\\'));\n  end\nend\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/saveubjson.m",
    "content": "function json=saveubjson(rootname,obj,varargin)\n%\n% json=saveubjson(rootname,obj,filename)\n%    or\n% json=saveubjson(rootname,obj,opt)\n% json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a Universal \n% Binary JSON (UBJSON) binary string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/17\n%\n% $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array)\n%      filename: a string for the file name to save the output UBJSON data\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSON='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a binary string in the UBJSON format (see http://ubjson.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      saveubjson('jsonmesh',jsonmesh)\n%      saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\njson=obj2ubjson(rootname,obj,rootlevel,opt);\nif(~rootisarray)\n    json=['{' json '}'];\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=[jsonp '(' json ')'];\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    fid = fopen(opt.FileName, 'wb');\n    fwrite(fid,json);\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2ubjson(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2ubjson(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2ubjson(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2ubjson(name,item,level,varargin{:});\nelse\n    txt=mat2ubjson(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2ubjson(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item); % let's handle 1D cell first\nif(len>1) \n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) '[']; name=''; \n    else\n        txt='['; \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; \n    else\n        txt='Z'; \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=[txt '[']; end\n    for i=1:dim(1)\n       txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})];\n    end\n    if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2ubjson(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=[txt '[']; end\n  for i=1:dim(1)\n     names = fieldnames(item(i,j));\n     if(~isempty(name) && len==1)\n        txt=[txt S_(checkname(name,varargin{:})) '{']; \n     else\n        txt=[txt '{']; \n     end\n     if(~isempty(names))\n       for e=1:length(names)\n\t     txt=[txt obj2ubjson(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})];\n       end\n     end\n     txt=[txt '}'];\n  end\n  if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2ubjson(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    val=item(e,:);\n    if(len==1)\n        obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),''];\n\tif(isempty(name)) obj=['',S_(val),'']; end\n        txt=[txt,'',obj];\n    else\n        txt=[txt,'',['',S_(val),'']];\n    end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2ubjson(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) || jsonopt('ArrayToStruct',0,varargin{:}))\n      cid=I_(uint32(max(size(item))));\n      if(isempty(name))\n    \ttxt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ];\n      else\n          if(isempty(item))\n              txt=[S_(checkname(name,varargin{:})),'Z'];\n              return;\n          else\n    \t      txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))];\n          end\n      end\nelse\n    if(isempty(name))\n    \ttxt=matdata2ubjson(item,level+1,varargin{:});\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n            numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\\[',''),']','');\n           \ttxt=[S_(checkname(name,varargin{:})) numtxt];\n        else\n    \t    txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})];\n        end\n    end\n    return;\nend\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=[txt,S_('_ArrayIsComplex_'),'T'];\n    end\n    txt=[txt,S_('_ArrayIsSparse_'),'T'];\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([iy(:),data'],level+2,varargin{:})];\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,data],level+2,varargin{:})];\n    else\n        % General case, store row and column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,iy,data],level+2,varargin{:})];\n    end\nelse\n    if(isreal(item))\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson(item(:)',level+2,varargin{:})];\n    else\n        txt=[txt,S_('_ArrayIsComplex_'),'T'];\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})];\n    end\nend\ntxt=[txt,'}'];\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2ubjson(mat,level,varargin)\nif(isempty(mat))\n    txt='Z';\n    return;\nend\nif(size(mat,1)==1)\n    level=level-1;\nend\ntype='';\nhasnegtive=(mat<0);\nif(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0)))\n    if(isempty(hasnegtive))\n       if(max(mat(:))<=2^8)\n           type='U';\n       end\n    end\n    if(isempty(type))\n        % todo - need to consider negative ones separately\n        id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]);\n        if(isempty(find(id)))\n            error('high-precision data is not yet supported');\n        end\n        key='iIlL';\n\ttype=key(find(id));\n    end\n    txt=[I_a(mat(:),type,size(mat))];\nelseif(islogical(mat))\n    logicalval='FT';\n    if(numel(mat)==1)\n        txt=logicalval(mat+1);\n    else\n        txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')];\n    end\nelse\n    if(numel(mat)==1)\n        txt=['[' D_(mat) ']'];\n    else\n        txt=D_a(mat(:),'D',size(mat));\n    end\nend\n\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n%%-------------------------------------------------------------------------\nfunction val=S_(str)\nif(length(str)==1)\n  val=['C' str];\nelse\n  val=['S' I_(int32(length(str))) str];\nend\n%%-------------------------------------------------------------------------\nfunction val=I_(num)\nif(~isinteger(num))\n    error('input is not an integer');\nend\nif(num>=0 && num<255)\n   val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')];\n   return;\nend\nkey='iIlL';\ncid={'int8','int16','int32','int64'};\nfor i=1:4\n  if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1)))\n    val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')];\n    return;\n  end\nend\nerror('unsupported integer');\n\n%%-------------------------------------------------------------------------\nfunction val=D_(num)\nif(~isfloat(num))\n    error('input is not a float');\nend\n\nif(isa(num,'single'))\n  val=['d' data2byte(num,'uint8')];\nelse\n  val=['D' data2byte(num,'uint8')];\nend\n%%-------------------------------------------------------------------------\nfunction data=I_a(num,type,dim,format)\nid=find(ismember('iUIlL',type));\n\nif(id==0)\n  error('unsupported integer array');\nend\n\n% based on UBJSON specs, all integer types are stored in big endian format\n\nif(id==1)\n  data=data2byte(swapbytes(int8(num)),'uint8');\n  blen=1;\nelseif(id==2)\n  data=data2byte(swapbytes(uint8(num)),'uint8');\n  blen=1;\nelseif(id==3)\n  data=data2byte(swapbytes(int16(num)),'uint8');\n  blen=2;\nelseif(id==4)\n  data=data2byte(swapbytes(int32(num)),'uint8');\n  blen=4;\nelseif(id==5)\n  data=data2byte(swapbytes(int64(num)),'uint8');\n  blen=8;\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/blen)) data(:)'];\n  end\n  data=['[' data(:)'];\nelse\n  data=reshape(data,blen,numel(data)/blen);\n  data(2:blen+1,:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction data=D_a(num,type,dim,format)\nid=find(ismember('dD',type));\n\nif(id==0)\n  error('unsupported float array');\nend\n\nif(id==1)\n  data=data2byte(single(num),'uint8');\nelseif(id==2)\n  data=data2byte(double(num),'uint8');\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)'];\n  end\n  data=['[' data];\nelse\n  data=reshape(data,(id*4),length(data)/(id*4));\n  data(2:(id*4+1),:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction bytes=data2byte(varargin)\nbytes=typecast(varargin{:});\nbytes=bytes(:)';\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/jsonlab/varargin2struct.m",
    "content": "function opt=varargin2struct(varargin)\n%\n% opt=varargin2struct('param1',value1,'param2',value2,...)\n%   or\n% opt=varargin2struct(...,optstruct,...)\n%\n% convert a series of input parameters into a structure\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      'param', value: the input parameters should be pairs of a string and a value\n%       optstruct: if a parameter is a struct, the fields will be merged to the output struct\n%\n% output:\n%      opt: a struct where opt.param1=value1, opt.param2=value2 ...\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nlen=length(varargin);\nopt=struct;\nif(len==0) return; end\ni=1;\nwhile(i<=len)\n    if(isstruct(varargin{i}))\n        opt=mergestruct(opt,varargin{i});\n    elseif(ischar(varargin{i}) && i<len)\n        opt=setfield(opt,varargin{i},varargin{i+1});\n        i=i+1;\n    else\n        error('input must be in the form of ...,''name'',value,... pairs or structs');\n    end\n    i=i+1;\nend\n\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/makeValidFieldName.m",
    "content": "function str = makeValidFieldName(str)\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    isoct=exist('OCTAVE_VERSION','builtin');\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n"
  },
  {
    "path": "machine-learning-ex2/ex2/lib/submitWithConfiguration.m",
    "content": "function submitWithConfiguration(conf)\n  addpath('./lib/jsonlab');\n\n  parts = parts(conf);\n\n  fprintf('== Submitting solutions | %s...\\n', conf.itemName);\n\n  tokenFile = 'token.mat';\n  if exist(tokenFile, 'file')\n    load(tokenFile);\n    [email token] = promptToken(email, token, tokenFile);\n  else\n    [email token] = promptToken('', '', tokenFile);\n  end\n\n  if isempty(token)\n    fprintf('!! Submission Cancelled\\n');\n    return\n  end\n\n  try\n    response = submitParts(conf, email, token, parts);\n  catch\n    e = lasterror();\n    fprintf( ...\n      '!! Submission failed: unexpected error: %s\\n', ...\n      e.message);\n    fprintf('!! Please try again later.\\n');\n    return\n  end\n\n  if isfield(response, 'errorMessage')\n    fprintf('!! Submission failed: %s\\n', response.errorMessage);\n  else\n    showFeedback(parts, response);\n    save(tokenFile, 'email', 'token');\n  end\nend\n\nfunction [email token] = promptToken(email, existingToken, tokenFile)\n  if (~isempty(email) && ~isempty(existingToken))\n    prompt = sprintf( ...\n      'Use token from last successful submission (%s)? (Y/n): ', ...\n      email);\n    reenter = input(prompt, 's');\n\n    if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y')\n      token = existingToken;\n      return;\n    else\n      delete(tokenFile);\n    end\n  end\n  email = input('Login (email address): ', 's');\n  token = input('Token: ', 's');\nend\n\nfunction isValid = isValidPartOptionIndex(partOptions, i)\n  isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions));\nend\n\nfunction response = submitParts(conf, email, token, parts)\n  body = makePostBody(conf, email, token, parts);\n  submissionUrl = submissionUrl();\n  params = {'jsonBody', body};\n  responseBody = urlread(submissionUrl, 'post', params);\n  response = loadjson(responseBody);\nend\n\nfunction body = makePostBody(conf, email, token, parts)\n  bodyStruct.assignmentSlug = conf.assignmentSlug;\n  bodyStruct.submitterEmail = email;\n  bodyStruct.secret = token;\n  bodyStruct.parts = makePartsStruct(conf, parts);\n\n  opt.Compact = 1;\n  body = savejson('', bodyStruct, opt);\nend\n\nfunction partsStruct = makePartsStruct(conf, parts)\n  for part = parts\n    partId = part{:}.id;\n    fieldName = makeValidFieldName(partId);\n    outputStruct.output = conf.output(partId);\n    partsStruct.(fieldName) = outputStruct;\n  end\nend\n\nfunction [parts] = parts(conf)\n  parts = {};\n  for partArray = conf.partArrays\n    part.id = partArray{:}{1};\n    part.sourceFiles = partArray{:}{2};\n    part.name = partArray{:}{3};\n    parts{end + 1} = part;\n  end\nend\n\nfunction showFeedback(parts, response)\n  fprintf('== \\n');\n  fprintf('== %43s | %9s | %-s\\n', 'Part Name', 'Score', 'Feedback');\n  fprintf('== %43s | %9s | %-s\\n', '---------', '-----', '--------');\n  for part = parts\n    score = '';\n    partFeedback = '';\n    partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id));\n    partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id));\n    score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore);\n    fprintf('== %43s | %9s | %-s\\n', part{:}.name, score, partFeedback);\n  end\n  evaluation = response.evaluation;\n  totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore);\n  fprintf('==                                   --------------------------------\\n');\n  fprintf('== %43s | %9s | %-s\\n', '', totalScore, '');\n  fprintf('== \\n');\nend\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%\n% Service configuration\n%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\nfunction submissionUrl = submissionUrl()\n  submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1';\nend\n"
  },
  {
    "path": "machine-learning-ex2/ex2/mapFeature.m",
    "content": "function out = mapFeature(X1, X2)\n% MAPFEATURE Feature mapping function to polynomial features\n%\n%   MAPFEATURE(X1, X2) maps the two input features\n%   to quadratic features used in the regularization exercise.\n%\n%   Returns a new feature array with more features, comprising of \n%   X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc..\n%\n%   Inputs X1, X2 must be the same size\n%\n\ndegree = 6;\nout = ones(size(X1(:,1)));\nfor i = 1:degree\n    for j = 0:i\n        out(:, end+1) = (X1.^(i-j)).*(X2.^j);\n    end\nend\n\n\n\nend"
  },
  {
    "path": "machine-learning-ex2/ex2/plotData.m",
    "content": "function plotData(X, y)\n%PLOTDATA Plots the data points X and y into a new figure \n%   PLOTDATA(x,y) plots the data points with + for the positive examples\n%   and o for the negative examples. X is assumed to be a Mx2 matrix.\n\n% Create New Figure\nfigure; hold on;\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Plot the positive and negative examples on a\n%               2D plot, using the option 'k+' for the positive\n%               examples and 'ko' for the negative examples.\n%\npos = find(y==1);\nneg = find(y==0);\n\nplot(X(pos,1),X(pos,2),'k+','LineWidth',2,'MarkerSize',7);\nplot(X(neg,1),X(neg,2),'ko','MarkerFaceColor','y','MarkerSize',7);\n\n\n\n\n\n\n\n\n% =========================================================================\n\n\n\nhold off;\n\nend\n"
  },
  {
    "path": "machine-learning-ex2/ex2/plotDecisionBoundary.m",
    "content": "function plotDecisionBoundary(theta, X, y)\n%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with\n%the decision boundary defined by theta\n%   PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the \n%   positive examples and o for the negative examples. X is assumed to be \n%   a either \n%   1) Mx3 matrix, where the first column is an all-ones column for the \n%      intercept.\n%   2) MxN, N>3 matrix, where the first column is all-ones\n\n% Plot Data\nplotData(X(:,2:3), y);\nhold on\n\nif size(X, 2) <= 3\n    % Only need 2 points to define a line, so choose two endpoints\n    plot_x = [min(X(:,2))-2,  max(X(:,2))+2];\n\n    % Calculate the decision boundary line\n    plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1)); % plot_xΪX1plot_yΪX2\n\n    % Plot, and adjust axes for better viewing\n    plot(plot_x, plot_y)\n    \n    % Legend, specific for the exercise\n    legend('Admitted', 'Not admitted', 'Decision Boundary')\n    axis([30, 100, 30, 100])\nelse\n    % Here is the grid range\n    u = linspace(-1, 1.5, 50);\n    v = linspace(-1, 1.5, 50);\n\n    z = zeros(length(u), length(v));\n    % Evaluate z = theta*x over the grid\n    for i = 1:length(u)\n        for j = 1:length(v)\n            z(i,j) = mapFeature(u(i), v(j))*theta;\n        end\n    end\n    z = z'; % important to transpose z before calling contour\n\n    % Plot z = 0\n    % Notice you need to specify the range [0, 0]\n    contour(u, v, z, [0, 0], 'LineWidth', 2) %ֵΪ0ĵֵ\nend\nhold off\n\nend\n"
  },
  {
    "path": "machine-learning-ex2/ex2/predict.m",
    "content": "function p = predict(theta, X)\n%PREDICT Predict whether the label is 0 or 1 using learned logistic \n%regression parameters theta\n%   p = PREDICT(theta, X) computes the predictions for X using a \n%   threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)\n\nm = size(X, 1); % Number of training examples\n\n% You need to return the following variables correctly\np = zeros(m, 1);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Complete the following code to make predictions using\n%               your learned logistic regression parameters. \n%               You should set p to a vector of 0's and 1's\n%\n\n\np = sigmoid(X*theta);\n\nfor i=1:m\n    if p(i)>0.5\n        p(i)=1;\n    else\n        p(i)=0;\n    end\nend\n\n\n\n\n% =========================================================================\n\n\nend\n"
  },
  {
    "path": "machine-learning-ex2/ex2/sigmoid.m",
    "content": "function g = sigmoid(z)\n%SIGMOID Compute sigmoid functoon\n%   J = SIGMOID(z) computes the sigmoid of z.\n\n% You need to return the following variables correctly \ng = zeros(size(z));\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the sigmoid of each value of z (z can be a matrix,\n%               vector or scalar).\n\ng = 1./(1+exp(-z));\n\n\n\n% =============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex2/ex2/submit.m",
    "content": "function submit()\n  addpath('./lib');\n\n  conf.assignmentSlug = 'logistic-regression';\n  conf.itemName = 'Logistic Regression';\n  conf.partArrays = { ...\n    { ...\n      '1', ...\n      { 'sigmoid.m' }, ...\n      'Sigmoid Function', ...\n    }, ...\n    { ...\n      '2', ...\n      { 'costFunction.m' }, ...\n      'Logistic Regression Cost', ...\n    }, ...\n    { ...\n      '3', ...\n      { 'costFunction.m' }, ...\n      'Logistic Regression Gradient', ...\n    }, ...\n    { ...\n      '4', ...\n      { 'predict.m' }, ...\n      'Predict', ...\n    }, ...\n    { ...\n      '5', ...\n      { 'costFunctionReg.m' }, ...\n      'Regularized Logistic Regression Cost', ...\n    }, ...\n    { ...\n      '6', ...\n      { 'costFunctionReg.m' }, ...\n      'Regularized Logistic Regression Gradient', ...\n    }, ...\n  };\n  conf.output = @output;\n\n  submitWithConfiguration(conf);\nend\n\nfunction out = output(partId, auxstring)\n  % Random Test Cases\n  X = [ones(20,1) (exp(1) * sin(1:1:20))' (exp(0.5) * cos(1:1:20))'];\n  y = sin(X(:,1) + X(:,2)) > 0;\n  if partId == '1'\n    out = sprintf('%0.5f ', sigmoid(X));\n  elseif partId == '2'\n    out = sprintf('%0.5f ', costFunction([0.25 0.5 -0.5]', X, y));\n  elseif partId == '3'\n    [cost, grad] = costFunction([0.25 0.5 -0.5]', X, y);\n    out = sprintf('%0.5f ', grad);\n  elseif partId == '4'\n    out = sprintf('%0.5f ', predict([0.25 0.5 -0.5]', X));\n  elseif partId == '5'\n    out = sprintf('%0.5f ', costFunctionReg([0.25 0.5 -0.5]', X, y, 0.1));\n  elseif partId == '6'\n    [cost, grad] = costFunctionReg([0.25 0.5 -0.5]', X, y, 0.1);\n    out = sprintf('%0.5f ', grad);\n  end \nend\n"
  },
  {
    "path": "machine-learning-ex3/ex3/displayData.m",
    "content": "function [h, display_array] = displayData(X, example_width)\n%DISPLAYDATA Display 2D data in a nice grid\n%   [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data\n%   stored in X in a nice grid. It returns the figure handle h and the \n%   displayed array if requested.\n\n% Set example_width automatically if not passed in\nif ~exist('example_width', 'var') || isempty(example_width) \n\texample_width = round(sqrt(size(X, 2)));\nend\n\n% Gray Image\ncolormap(gray);\n\n% Compute rows, cols\n[m n] = size(X);\nexample_height = (n / example_width);\n\n% Compute number of items to display\ndisplay_rows = floor(sqrt(m));\ndisplay_cols = ceil(m / display_rows);\n\n% Between images padding\npad = 1;\n\n% Setup blank display\ndisplay_array = - ones(pad + display_rows * (example_height + pad), ...\n                       pad + display_cols * (example_width + pad));\n\n% Copy each example into a patch on the display array\ncurr_ex = 1;\nfor j = 1:display_rows\n\tfor i = 1:display_cols\n\t\tif curr_ex > m, \n\t\t\tbreak; \n\t\tend\n\t\t% Copy the patch\n\t\t\n\t\t% Get the max value of the patch\n\t\tmax_val = max(abs(X(curr_ex, :)));\n\t\tdisplay_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...\n\t\t              pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...\n\t\t\t\t\t\treshape(X(curr_ex, :), example_height, example_width) / max_val;\n\t\tcurr_ex = curr_ex + 1;\n\tend\n\tif curr_ex > m, \n\t\tbreak; \n\tend\nend\n\n% Display Image\nh = imagesc(display_array, [-1 1]);\n\n% Do not show axis\naxis image off\n\ndrawnow;\n\nend\n"
  },
  {
    "path": "machine-learning-ex3/ex3/ex3.m",
    "content": "%% Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all\n\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the\n%  linear exercise. You will need to complete the following functions \n%  in this exericse:\n%\n%     lrCostFunction.m (logistic regression cost function)\n%     oneVsAll.m\n%     predictOneVsAll.m\n%     predict.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% Setup the parameters you will use for this part of the exercise\ninput_layer_size  = 400;  % 20x20 Input Images of Digits\nnum_labels = 10;          % 10 labels, from 1 to 10   \n                          % (note that we have mapped \"0\" to label 10)\n\n%% =========== Part 1: Loading and Visualizing Data =============\n%  We start the exercise by first loading and visualizing the dataset. \n%  You will be working with a dataset that contains handwritten digits.\n%\n\n% Load Training Data\nfprintf('Loading and Visualizing Data ...\\n')\n\nload('ex3data1.mat'); % training data stored in arrays X, y\nm = size(X, 1);\n\n% Randomly select 100 data points to display\nrand_indices = randperm(m);\nsel = X(rand_indices(1:100), :);\n\ndisplayData(sel);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ============ Part 2: Vectorize Logistic Regression ============\n%  In this part of the exercise, you will reuse your logistic regression\n%  code from the last exercise. You task here is to make sure that your\n%  regularized logistic regression implementation is vectorized. After\n%  that, you will implement one-vs-all classification for the handwritten\n%  digit dataset.\n%\n\nfprintf('\\nTraining One-vs-All Logistic Regression...\\n')\n\nlambda = 0.1;\n[all_theta] = oneVsAll(X, y, num_labels, lambda);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% ================ Part 3: Predict for One-Vs-All ================\n%  After ...\npred = predictOneVsAll(all_theta, X);\n\nfprintf('\\nTraining Set Accuracy: %f\\n', mean(double(pred == y)) * 100);\n\n\n"
  },
  {
    "path": "machine-learning-ex3/ex3/ex3_nn.m",
    "content": "%% Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks\n\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the\n%  linear exercise. You will need to complete the following functions \n%  in this exericse:\n%\n%     lrCostFunction.m (logistic regression cost function)\n%     oneVsAll.m\n%     predictOneVsAll.m\n%     predict.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% Setup the parameters you will use for this exercise\ninput_layer_size  = 400;  % 20x20 Input Images of Digits\nhidden_layer_size = 25;   % 25 hidden units\nnum_labels = 10;          % 10 labels, from 1 to 10   \n                          % (note that we have mapped \"0\" to label 10)\n\n%% =========== Part 1: Loading and Visualizing Data =============\n%  We start the exercise by first loading and visualizing the dataset. \n%  You will be working with a dataset that contains handwritten digits.\n%\n\n% Load Training Data\nfprintf('Loading and Visualizing Data ...\\n')\n\nload('ex3data1.mat');\nm = size(X, 1);\n\n% Randomly select 100 data points to display\nsel = randperm(size(X, 1));\nsel = sel(1:100);\n\ndisplayData(X(sel, :));\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ================ Part 2: Loading Pameters ================\n% In this part of the exercise, we load some pre-initialized \n% neural network parameters.\n\nfprintf('\\nLoading Saved Neural Network Parameters ...\\n')\n\n% Load the weights into variables Theta1 and Theta2\nload('ex3weights.mat');\n\n%% ================= Part 3: Implement Predict =================\n%  After training the neural network, we would like to use it to predict\n%  the labels. You will now implement the \"predict\" function to use the\n%  neural network to predict the labels of the training set. This lets\n%  you compute the training set accuracy.\n\npred = predict(Theta1, Theta2, X);\n\nfprintf('\\nTraining Set Accuracy: %f\\n', mean(double(pred == y)) * 100);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%  To give you an idea of the network's output, you can also run\n%  through the examples one at the a time to see what it is predicting.\n\n%  Randomly permute examples\nrp = randperm(m);\n\nfor i = 1:m\n    % Display \n    fprintf('\\nDisplaying Example Image\\n');\n    displayData(X(rp(i), :));\n\n    pred = predict(Theta1, Theta2, X(rp(i),:));\n    fprintf('\\nNeural Network Prediction: %d (digit %d)\\n', pred, mod(pred, 10));%mod࣬Ϊ0ӳ10\n    \n    % Pause\n    fprintf('Program paused. Press enter to continue.\\n');\n    pause;\nend\n\n"
  },
  {
    "path": "machine-learning-ex3/ex3/fmincg.m",
    "content": "function [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)\n% Minimize a continuous differentialble multivariate function. Starting point\n% is given by \"X\" (D by 1), and the function named in the string \"f\", must\n% return a function value and a vector of partial derivatives. The Polack-\n% Ribiere flavour of conjugate gradients is used to compute search directions,\n% and a line search using quadratic and cubic polynomial approximations and the\n% Wolfe-Powell stopping criteria is used together with the slope ratio method\n% for guessing initial step sizes. Additionally a bunch of checks are made to\n% make sure that exploration is taking place and that extrapolation will not\n% be unboundedly large. The \"length\" gives the length of the run: if it is\n% positive, it gives the maximum number of line searches, if negative its\n% absolute gives the maximum allowed number of function evaluations. You can\n% (optionally) give \"length\" a second component, which will indicate the\n% reduction in function value to be expected in the first line-search (defaults\n% to 1.0). The function returns when either its length is up, or if no further\n% progress can be made (ie, we are at a minimum, or so close that due to\n% numerical problems, we cannot get any closer). If the function terminates\n% within a few iterations, it could be an indication that the function value\n% and derivatives are not consistent (ie, there may be a bug in the\n% implementation of your \"f\" function). The function returns the found\n% solution \"X\", a vector of function values \"fX\" indicating the progress made\n% and \"i\" the number of iterations (line searches or function evaluations,\n% depending on the sign of \"length\") used.\n%\n% Usage: [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)\n%\n% See also: checkgrad \n%\n% Copyright (C) 2001 and 2002 by Carl Edward Rasmussen. Date 2002-02-13\n%\n%\n% (C) Copyright 1999, 2000 & 2001, Carl Edward Rasmussen\n% \n% Permission is granted for anyone to copy, use, or modify these\n% programs and accompanying documents for purposes of research or\n% education, provided this copyright notice is retained, and note is\n% made of any changes that have been made.\n% \n% These programs and documents are distributed without any warranty,\n% express or implied.  As the programs were written for research\n% purposes only, they have not been tested to the degree that would be\n% advisable in any important application.  All use of these programs is\n% entirely at the user's own risk.\n%\n% [ml-class] Changes Made:\n% 1) Function name and argument specifications\n% 2) Output display\n%\n\n% Read options\nif exist('options', 'var') && ~isempty(options) && isfield(options, 'MaxIter')\n    length = options.MaxIter;\nelse\n    length = 100;\nend\n\n\nRHO = 0.01;                            % a bunch of constants for line searches\nSIG = 0.5;       % RHO and SIG are the constants in the Wolfe-Powell conditions\nINT = 0.1;    % don't reevaluate within 0.1 of the limit of the current bracket\nEXT = 3.0;                    % extrapolate maximum 3 times the current bracket\nMAX = 20;                         % max 20 function evaluations per line search\nRATIO = 100;                                      % maximum allowed slope ratio\n\nargstr = ['feval(f, X'];                      % compose string used to call function\nfor i = 1:(nargin - 3)\n  argstr = [argstr, ',P', int2str(i)];\nend\nargstr = [argstr, ')'];\n\nif max(size(length)) == 2, red=length(2); length=length(1); else red=1; end\nS=['Iteration '];\n\ni = 0;                                            % zero the run length counter\nls_failed = 0;                             % no previous line search has failed\nfX = [];\n[f1 df1] = eval(argstr);                      % get function value and gradient\ni = i + (length<0);                                            % count epochs?!\ns = -df1;                                        % search direction is steepest\nd1 = -s'*s;                                                 % this is the slope\nz1 = red/(1-d1);                                  % initial step is red/(|s|+1)\n\nwhile i < abs(length)                                      % while not finished\n  i = i + (length>0);                                      % count iterations?!\n\n  X0 = X; f0 = f1; df0 = df1;                   % make a copy of current values\n  X = X + z1*s;                                             % begin line search\n  [f2 df2] = eval(argstr);\n  i = i + (length<0);                                          % count epochs?!\n  d2 = df2'*s;\n  f3 = f1; d3 = d1; z3 = -z1;             % initialize point 3 equal to point 1\n  if length>0, M = MAX; else M = min(MAX, -length-i); end\n  success = 0; limit = -1;                     % initialize quanteties\n  while 1\n    while ((f2 > f1+z1*RHO*d1) || (d2 > -SIG*d1)) && (M > 0) \n      limit = z1;                                         % tighten the bracket\n      if f2 > f1\n        z2 = z3 - (0.5*d3*z3*z3)/(d3*z3+f2-f3);                 % quadratic fit\n      else\n        A = 6*(f2-f3)/z3+3*(d2+d3);                                 % cubic fit\n        B = 3*(f3-f2)-z3*(d3+2*d2);\n        z2 = (sqrt(B*B-A*d2*z3*z3)-B)/A;       % numerical error possible - ok!\n      end\n      if isnan(z2) || isinf(z2)\n        z2 = z3/2;                  % if we had a numerical problem then bisect\n      end\n      z2 = max(min(z2, INT*z3),(1-INT)*z3);  % don't accept too close to limits\n      z1 = z1 + z2;                                           % update the step\n      X = X + z2*s;\n      [f2 df2] = eval(argstr);\n      M = M - 1; i = i + (length<0);                           % count epochs?!\n      d2 = df2'*s;\n      z3 = z3-z2;                    % z3 is now relative to the location of z2\n    end\n    if f2 > f1+z1*RHO*d1 || d2 > -SIG*d1\n      break;                                                % this is a failure\n    elseif d2 > SIG*d1\n      success = 1; break;                                             % success\n    elseif M == 0\n      break;                                                          % failure\n    end\n    A = 6*(f2-f3)/z3+3*(d2+d3);                      % make cubic extrapolation\n    B = 3*(f3-f2)-z3*(d3+2*d2);\n    z2 = -d2*z3*z3/(B+sqrt(B*B-A*d2*z3*z3));        % num. error possible - ok!\n    if ~isreal(z2) || isnan(z2) || isinf(z2) || z2 < 0 % num prob or wrong sign?\n      if limit < -0.5                               % if we have no upper limit\n        z2 = z1 * (EXT-1);                 % the extrapolate the maximum amount\n      else\n        z2 = (limit-z1)/2;                                   % otherwise bisect\n      end\n    elseif (limit > -0.5) && (z2+z1 > limit)         % extraplation beyond max?\n      z2 = (limit-z1)/2;                                               % bisect\n    elseif (limit < -0.5) && (z2+z1 > z1*EXT)       % extrapolation beyond limit\n      z2 = z1*(EXT-1.0);                           % set to extrapolation limit\n    elseif z2 < -z3*INT\n      z2 = -z3*INT;\n    elseif (limit > -0.5) && (z2 < (limit-z1)*(1.0-INT))  % too close to limit?\n      z2 = (limit-z1)*(1.0-INT);\n    end\n    f3 = f2; d3 = d2; z3 = -z2;                  % set point 3 equal to point 2\n    z1 = z1 + z2; X = X + z2*s;                      % update current estimates\n    [f2 df2] = eval(argstr);\n    M = M - 1; i = i + (length<0);                             % count epochs?!\n    d2 = df2'*s;\n  end                                                      % end of line search\n\n  if success                                         % if line search succeeded\n    f1 = f2; fX = [fX' f1]';\n    fprintf('%s %4i | Cost: %4.6e\\r', S, i, f1);\n    s = (df2'*df2-df1'*df2)/(df1'*df1)*s - df2;      % Polack-Ribiere direction\n    tmp = df1; df1 = df2; df2 = tmp;                         % swap derivatives\n    d2 = df1'*s;\n    if d2 > 0                                      % new slope must be negative\n      s = -df1;                              % otherwise use steepest direction\n      d2 = -s'*s;    \n    end\n    z1 = z1 * min(RATIO, d1/(d2-realmin));          % slope ratio but max RATIO\n    d1 = d2;\n    ls_failed = 0;                              % this line search did not fail\n  else\n    X = X0; f1 = f0; df1 = df0;  % restore point from before failed line search\n    if ls_failed || i > abs(length)          % line search failed twice in a row\n      break;                             % or we ran out of time, so we give up\n    end\n    tmp = df1; df1 = df2; df2 = tmp;                         % swap derivatives\n    s = -df1;                                                    % try steepest\n    d1 = -s'*s;\n    z1 = 1/(1-d1);                     \n    ls_failed = 1;                                    % this line search failed\n  end\n  if exist('OCTAVE_VERSION')\n    fflush(stdout);\n  end\nend\nfprintf('\\n');\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/AUTHORS.txt",
    "content": "The author of \"jsonlab\" toolbox is Qianqian Fang. Qianqian\nis currently an Assistant Professor at Massachusetts General Hospital, \nHarvard Medical School.\n\nAddress: Martinos Center for Biomedical Imaging, \n         Massachusetts General Hospital, \n         Harvard Medical School\n         Bldg 149, 13th St, Charlestown, MA 02129, USA\nURL: http://nmr.mgh.harvard.edu/~fangq/\nEmail: <fangq at nmr.mgh.harvard.edu> or <fangqq at gmail.com>\n\n\nThe script loadjson.m was built upon previous works by\n\n- Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n       date: 2009/11/02\n- François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n       date: 2009/03/22\n- Joel Feenstra: http://www.mathworks.com/matlabcentral/fileexchange/20565\n       date: 2008/07/03\n\n\nThis toolbox contains patches submitted by the following contributors:\n\n- Blake Johnson <bjohnso at bbn.com>\n  part of revision 341\n\n- Niclas Borlin <Niclas.Borlin at cs.umu.se>\n  various fixes in revision 394, including\n  - loadjson crashes for all-zero sparse matrix.\n  - loadjson crashes for empty sparse matrix.\n  - Non-zero size of 0-by-N and N-by-0 empty matrices is lost after savejson/loadjson.\n  - loadjson crashes for sparse real column vector.\n  - loadjson crashes for sparse complex column vector.\n  - Data is corrupted by savejson for sparse real row vector.\n  - savejson crashes for sparse complex row vector. \n\n- Yul Kang <yul.kang.on at gmail.com>\n  patches for svn revision 415.\n  - savejson saves an empty cell array as [] instead of null\n  - loadjson differentiates an empty struct from an empty array\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/ChangeLog.txt",
    "content": "============================================================================\n\n   JSONlab - a toolbox to encode/decode JSON/UBJSON files in MATLAB/Octave\n\n----------------------------------------------------------------------------\n\nJSONlab ChangeLog (key features marked by *):\n\n== JSONlab 1.0 (codename: Optimus - Final), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2015/01/02 polish help info for all major functions, update examples, finalize 1.0\n 2014/12/19 fix a bug to strictly respect NoRowBracket in savejson\n\n== JSONlab 1.0.0-RC2 (codename: Optimus - RC2), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/11/22 show progress bar in loadjson ('ShowProgress') \n 2014/11/17 add Compact option in savejson to output compact JSON format ('Compact')\n 2014/11/17 add FastArrayParser in loadjson to specify fast parser applicable levels\n 2014/09/18 start official github mirror: https://github.com/fangq/jsonlab\n\n== JSONlab 1.0.0-RC1 (codename: Optimus - RC1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/09/17  fix several compatibility issues when running on octave versions 3.2-3.8\n 2014/09/17  support 2D cell and struct arrays in both savejson and saveubjson\n 2014/08/04  escape special characters in a JSON string\n 2014/02/16  fix a bug when saving ubjson files\n\n== JSONlab 0.9.9 (codename: Optimus - beta), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/01/22  use binary read and write in saveubjson and loadubjson\n\n== JSONlab 0.9.8-1 (codename: Optimus - alpha update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2013/10/07 better round-trip conservation for empty arrays and structs (patch submitted by Yul Kang)\n\n== JSONlab 0.9.8 (codename: Optimus - alpha), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2013/08/23 *universal Binary JSON (UBJSON) support, including both saveubjson and loadubjson\n\n== JSONlab 0.9.1 (codename: Rodimus, update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2012/12/18 *handling of various empty and sparse matrices (fixes submitted by Niclas Borlin)\n\n== JSONlab 0.9.0 (codename: Rodimus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/06/17 *new format for an invalid leading char, unpacking hex code in savejson\n 2012/06/01  support JSONP in savejson\n 2012/05/25  fix the empty cell bug (reported by Cyril Davin)\n 2012/04/05  savejson can save to a file (suggested by Patrick Rapin)\n\n== JSONlab 0.8.1 (codename: Sentiel, Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/02/28  loadjson quotation mark escape bug, see http://bit.ly/yyk1nS\n 2012/01/25  patch to handle root-less objects, contributed by Blake Johnson\n\n== JSONlab 0.8.0 (codename: Sentiel), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/01/13 *speed up loadjson by 20 fold when parsing large data arrays in matlab\n 2012/01/11  remove row bracket if an array has 1 element, suggested by Mykel Kochenderfer\n 2011/12/22 *accept sequence of 'param',value input in savejson and loadjson\n 2011/11/18  fix struct array bug reported by Mykel Kochenderfer\n\n== JSONlab 0.5.1 (codename: Nexus Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/21  fix a bug in loadjson, previous code does not use any of the acceleration\n 2011/10/20  loadjson supports JSON collections - concatenated JSON objects\n\n== JSONlab 0.5.0 (codename: Nexus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/16  package and release jsonlab 0.5.0\n 2011/10/15 *add json demo and regression test, support cpx numbers, fix double quote bug\n 2011/10/11 *speed up readjson dramatically, interpret _Array* tags, show data in root level\n 2011/10/10  create jsonlab project, start jsonlab website, add online documentation\n 2011/10/07 *speed up savejson by 25x using sprintf instead of mat2str, add options support\n 2011/10/06 *savejson works for structs, cells and arrays\n 2011/09/09  derive loadjson from JSON parser from MATLAB Central, draft savejson.m\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/LICENSE_BSD.txt",
    "content": "Copyright 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are\npermitted provided that the following conditions are met:\n\n   1. Redistributions of source code must retain the above copyright notice, this list of\n      conditions and the following disclaimer.\n\n   2. Redistributions in binary form must reproduce the above copyright notice, this list\n      of conditions and the following disclaimer in the documentation and/or other materials\n      provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS \nOR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nThe views and conclusions contained in the software and documentation are those of the\nauthors and should not be interpreted as representing official policies, either expressed\nor implied, of the copyright holders.\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/README.txt",
    "content": "===============================================================================\n=                                 JSONLab                                     =\n=           An open-source MATLAB/Octave JSON encoder and decoder             =\n===============================================================================\n\n*Copyright (C) 2011-2015  Qianqian Fang <fangq at nmr.mgh.harvard.edu>\n*License: BSD License, see License_BSD.txt for details\n*Version: 1.0 (Optimus - Final)\n\n-------------------------------------------------------------------------------\n\nTable of Content:\n\nI.  Introduction\nII. Installation\nIII.Using JSONLab\nIV. Known Issues and TODOs\nV.  Contribution and feedback\n\n-------------------------------------------------------------------------------\n\nI.  Introduction\n\nJSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable, \nhuman-readable and \"[http://en.wikipedia.org/wiki/JSON fat-free]\" text format \nto represent complex and hierarchical data. It is as powerful as \n[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely \nused for data-exchange in applications, and is essential for the wild success \nof [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and \n[http://en.wikipedia.org/wiki/Web_2.0 Web2.0]. \n\nUBJSON (Universal Binary JSON) is a binary JSON format, specifically \noptimized for compact file size and better performance while keeping\nthe semantics as simple as the text-based JSON format. Using the UBJSON\nformat allows to wrap complex binary data in a flexible and extensible\nstructure, making it possible to process complex and large dataset \nwithout accuracy loss due to text conversions.\n\nWe envision that both JSON and its binary version will serve as part of \nthe mainstream data-exchange formats for scientific research in the future. \nIt will provide the flexibility and generality achieved by other popular \ngeneral-purpose file specifications, such as\n[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly \nreduced complexity and enhanced performance.\n\nJSONLab is a free and open-source implementation of a JSON/UBJSON encoder \nand a decoder in the native MATLAB language. It can be used to convert a MATLAB \ndata structure (array, struct, cell, struct array and cell array) into \nJSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB \ndata structure. JSONLab supports both MATLAB and  \n[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).\n\n-------------------------------------------------------------------------------\n\nII. Installation\n\nThe installation of JSONLab is no different than any other simple\nMATLAB toolbox. You only need to download/unzip the JSONLab package\nto a folder, and add the folder's path to MATLAB/Octave's path list\nby using the following command:\n\n    addpath('/path/to/jsonlab');\n\nIf you want to add this path permanently, you need to type \"pathtool\", \nbrowse to the jsonlab root folder and add to the list, then click \"Save\".\nThen, run \"rehash\" in MATLAB, and type \"which loadjson\", if you see an \noutput, that means JSONLab is installed for MATLAB/Octave.\n\n-------------------------------------------------------------------------------\n\nIII.Using JSONLab\n\nJSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder, \nand savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and \ntwo equivallent functions -- loadubjson and saveubjson for the binary \nJSON. The detailed help info for the four functions can be found below:\n\n=== loadjson.m ===\n<pre>\n  data=loadjson(fname,opt)\n     or\n  data=loadjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09, including previous works from \n \n          Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n             created on 2009/11/02\n          Franois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n             created on  2009/03/22\n          Joel Feenstra:\n          http://www.mathworks.com/matlabcentral/fileexchange/20565\n             created on 2008/07/03\n \n  $Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a JSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n                          speed-optimized array parser when loading an \n                          array object. The fast array parser may \n                          collapse block arrays into a single large\n                          array similar to rules defined in cell2mat; 0 to \n                          use a legacy parser; if set to a larger-than-1\n                          value, this option will specify the minimum\n                          dimension to enable the fast array parser. For\n                          example, if the input is a 3D array, setting\n                          FastArrayParser to 1 will return a 3D array;\n                          setting to 2 will return a cell array of 2D\n                          arrays; setting to 3 will return to a 2D cell\n                          array of 1D vectors; setting to 4 will return a\n                          3D cell array.\n            opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n       dat=loadjson(['examples' filesep 'example1.json'])\n       dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n</pre>\n\n=== savejson.m ===\n\n<pre>\n  json=savejson(rootname,obj,filename)\n     or\n  json=savejson(rootname,obj,opt)\n  json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n  Object Notation) string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09\n \n  $Id: savejson.m 458 2014-12-19 22:17:17Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array).\n       filename: a string for the file name to save the output JSON data.\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n                          of a 1D/2D array;\n         opt.ArrayIndent [1|0]: if 1, output explicit data array with\n                          precedent indentation; if 0, no indentation\n         opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n                          to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n                          and $1 represents the sign. For those who want to use\n                          1e999 to represent Inf, they can set opt.Inf to '$11e999'\n         opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n                          to represent NaN\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSONP='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n         opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n         opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a string in the JSON format (see http://json.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       savejson('jmesh',jsonmesh)\n       savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n </pre>\n\n=== loadubjson.m ===\n\n<pre>\n  data=loadubjson(fname,opt)\n     or\n  data=loadubjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/01\n \n  $Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a UBJSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.IntEndian [B|L]: specify the endianness of the integer fields\n                          in the UBJSON input data. B - Big-Endian format for \n                          integers (as required in the UBJSON specification); \n                          L - input integer fields are in Little-Endian order.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       obj=struct('string','value','array',[1 2 3]);\n       ubjdata=saveubjson('obj',obj);\n       dat=loadubjson(ubjdata)\n       dat=loadubjson(['examples' filesep 'example1.ubj'])\n       dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n</pre>\n\n=== saveubjson.m ===\n\n<pre>\n  json=saveubjson(rootname,obj,filename)\n     or\n  json=saveubjson(rootname,obj,opt)\n  json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a Universal \n  Binary JSON (UBJSON) binary string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/17\n \n  $Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array)\n       filename: a string for the file name to save the output UBJSON data\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSON='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a binary string in the UBJSON format (see http://ubjson.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       saveubjson('jsonmesh',jsonmesh)\n       saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n</pre>\n\n\n=== examples ===\n\nUnder the \"examples\" folder, you can find several scripts to demonstrate the\nbasic utilities of JSONLab. Running the \"demo_jsonlab_basic.m\" script, you \nwill see the conversions from MATLAB data structure to JSON text and backward.\nIn \"jsonlab_selftest.m\", we load complex JSON files downloaded from the Internet\nand validate the loadjson/savejson functions for regression testing purposes.\nSimilarly, a \"demo_ubjson_basic.m\" script is provided to test the saveubjson\nand loadubjson pairs for various matlab data structures.\n\nPlease run these examples and understand how JSONLab works before you use\nit to process your data.\n\n-------------------------------------------------------------------------------\n\nIV. Known Issues and TODOs\n\nJSONLab has several known limitations. We are striving to make it more general\nand robust. Hopefully in a few future releases, the limitations become less.\n\nHere are the known issues:\n\n# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;\n# When processing names containing multi-byte characters, Octave and MATLAB \\\ncan give different field-names; you can use feature('DefaultCharacterSet','latin1') \\\nin MATLAB to get consistant results\n# savejson can not handle class and dataset.\n# saveubjson converts a logical array into a uint8 ([U]) array\n# an unofficial N-D array count syntax is implemented in saveubjson. We are \\\nactively communicating with the UBJSON spec maintainer to investigate the \\\npossibility of making it upstream\n# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \\\nfiles, however, it can parse all UBJSON files produced by saveubjson.\n\n-------------------------------------------------------------------------------\n\nV. Contribution and feedback\n\nJSONLab is an open-source project. This means you can not only use it and modify\nit as you wish, but also you can contribute your changes back to JSONLab so\nthat everyone else can enjoy the improvement. For anyone who want to contribute,\nplease download JSONLab source code from it's subversion repository by using the\nfollowing command:\n\n svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab\n\nYou can make changes to the files as needed. Once you are satisfied with your\nchanges, and ready to share it with others, please cd the root directory of \nJSONLab, and type\n\n svn diff > yourname_featurename.patch\n\nYou then email the .patch file to JSONLab's maintainer, Qianqian Fang, at\nthe email address shown in the beginning of this file. Qianqian will review \nthe changes and commit it to the subversion if they are satisfactory.\n\nWe appreciate any suggestions and feedbacks from you. Please use iso2mesh's\nmailing list to report any questions you may have with JSONLab:\n\nhttp://groups.google.com/group/iso2mesh-users?hl=en&pli=1\n\n(Subscription to the mailing list is needed in order to post messages).\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/jsonopt.m",
    "content": "function val=jsonopt(key,default,varargin)\n%\n% val=jsonopt(key,default,optstruct)\n%\n% setting options based on a struct. The struct can be produced\n% by varargin2struct from a list of 'param','value' pairs\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n%\n% $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $\n%\n% input:\n%      key: a string with which one look up a value from a struct\n%      default: if the key does not exist, return default\n%      optstruct: a struct where each sub-field is a key \n%\n% output:\n%      val: if key exists, val=optstruct.key; otherwise val=default\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n% \n\nval=default;\nif(nargin<=2) return; end\nopt=varargin{1};\nif(isstruct(opt) && isfield(opt,key))\n    val=getfield(opt,key);\nend\n\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/loadjson.m",
    "content": "function data = loadjson(fname,varargin)\n%\n% data=loadjson(fname,opt)\n%    or\n% data=loadjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09, including previous works from \n%\n%         Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n%            created on 2009/11/02\n%         François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n%            created on  2009/03/22\n%         Joel Feenstra:\n%         http://www.mathworks.com/matlabcentral/fileexchange/20565\n%            created on 2008/07/03\n%\n% $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a JSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n%                         speed-optimized array parser when loading an \n%                         array object. The fast array parser may \n%                         collapse block arrays into a single large\n%                         array similar to rules defined in cell2mat; 0 to \n%                         use a legacy parser; if set to a larger-than-1\n%                         value, this option will specify the minimum\n%                         dimension to enable the fast array parser. For\n%                         example, if the input is a 3D array, setting\n%                         FastArrayParser to 1 will return a 3D array;\n%                         setting to 2 will return a cell array of 2D\n%                         arrays; setting to 3 will return to a 2D cell\n%                         array of 1D vectors; setting to 4 will return a\n%                         3D cell array.\n%           opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n%      dat=loadjson(['examples' filesep 'example1.json'])\n%      dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\n\nif(jsonopt('ShowProgress',0,opt)==1)\n    opt.progressbar_=waitbar(0,'loading ...');\nend\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\nif(isfield(opt,'progressbar_'))\n    close(opt.progressbar_);\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=data(j).x0x5F_ArraySize_;\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    if next_char ~= '}'\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            parse_char(':');\n            val = parse_value(varargin{:});\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}'\n                break;\n            end\n            parse_char(',');\n        end\n    end\n    parse_char('}');\n\n%%-------------------------------------------------------------------------\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim2=[];\n    arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:});\n    pbar=jsonopt('progressbar_',-1,varargin{:});\n\n    if next_char ~= ']'\n\tif(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))\n            [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos);\n            arraystr=['[' inStr(pos:endpos)];\n            arraystr=regexprep(arraystr,'\"_NaN_\"','NaN');\n            arraystr=regexprep(arraystr,'\"([-+]*)_Inf_\"','$1Inf');\n            arraystr(arraystr==sprintf('\\n'))=[];\n            arraystr(arraystr==sprintf('\\r'))=[];\n            %arraystr=regexprep(arraystr,'\\s*,',','); % this is slow,sometimes needed\n            if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D\n        \tastr=inStr((e1l+1):(e1r-1));\n        \tastr=regexprep(astr,'\"_NaN_\"','NaN');\n        \tastr=regexprep(astr,'\"([-+]*)_Inf_\"','$1Inf');\n        \tastr(astr==sprintf('\\n'))=[];\n        \tastr(astr==sprintf('\\r'))=[];\n        \tastr(astr==' ')='';\n        \tif(isempty(find(astr=='[', 1))) % array is 2D\n                    dim2=length(sscanf(astr,'%f,',[1 inf]));\n        \tend\n            else % array is 1D\n        \tastr=arraystr(2:end-1);\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]);\n        \tif(nextidx>=length(astr)-1)\n                    object=obj;\n                    pos=endpos;\n                    parse_char(']');\n                    return;\n        \tend\n            end\n            if(~isempty(dim2))\n        \tastr=arraystr;\n        \tastr(astr=='[')='';\n        \tastr(astr==']')='';\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf);\n        \tif(nextidx>=length(astr)-1)\n                    object=reshape(obj,dim2,numel(obj)/dim2)';\n                    pos=endpos;\n                    parse_char(']');\n                    if(pbar>0)\n                        waitbar(pos/length(inStr),pbar,'loading ...');\n                    end\n                    return;\n        \tend\n            end\n            arraystr=regexprep(arraystr,'\\]\\s*,','];');\n\telse\n            arraystr='[';\n\tend\n        try\n           if(isoct && regexp(arraystr,'\"','once'))\n                error('Octave eval can produce empty cells for JSON-like input');\n           end\n           object=eval(arraystr);\n           pos=endpos;\n        catch\n         while 1\n            newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1);\n            val = parse_value(newopt);\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            parse_char(',');\n         end\n        end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    parse_char(']');\n    \n    if(pbar>0)\n        waitbar(pos/length(inStr),pbar,'loading ...');\n    end\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr len  esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    if inStr(pos) ~= '\"'\n        error_pos('String starting with \" expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    str = '';\n    while pos <= len\n        while index_esc <= len_esc && esc(index_esc) < pos\n            index_esc = index_esc + 1;\n        end\n        if index_esc > len_esc\n            str = [str inStr(pos:len)];\n            pos = len + 1;\n            break;\n        else\n            str = [str inStr(pos:esc(index_esc)-1)];\n            pos = esc(index_esc);\n        end\n        nstr = length(str); switch inStr(pos)\n            case '\"'\n                pos = pos + 1;\n                if(~isempty(str))\n                    if(strcmp(str,'_Inf_'))\n                        str=Inf;\n                    elseif(strcmp(str,'-_Inf_'))\n                        str=-Inf;\n                    elseif(strcmp(str,'_NaN_'))\n                        str=NaN;\n                    end\n                end\n                return;\n            case '\\'\n                if pos+1 > len\n                    error_pos('End of file reached right after escape character');\n                end\n                pos = pos + 1;\n                switch inStr(pos)\n                    case {'\"' '\\' '/'}\n                        str(nstr+1) = inStr(pos);\n                        pos = pos + 1;\n                    case {'b' 'f' 'n' 'r' 't'}\n                        str(nstr+1) = sprintf(['\\' inStr(pos)]);\n                        pos = pos + 1;\n                    case 'u'\n                        if pos+4 > len\n                            error_pos('End of file reached in escaped unicode character');\n                        end\n                        str(nstr+(1:6)) = inStr(pos-1:pos+4);\n                        pos = pos + 5;\n                end\n            otherwise % should never happen\n                str(nstr+1) = inStr(pos), keyboard\n                pos = pos + 1;\n        end\n    end\n    error_pos('End of file while expecting end of inStr');\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct\n    currstr=inStr(pos:end);\n    numstr=0;\n    if(isoct~=0)\n        numstr=regexp(currstr,'^\\s*-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+\\-]?\\d+)?','end');\n        [num, one] = sscanf(currstr, '%f', 1);\n        delta=numstr+1;\n    else\n        [num, one, err, delta] = sscanf(currstr, '%f', 1);\n        if ~isempty(err)\n            error_pos('Error reading number at position %d');\n        end\n    end\n    pos = pos + delta-1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n    \n    pbar=jsonopt('progressbar_',-1,varargin{:});\n    if(pbar>0)\n        waitbar(pos/len,pbar,'loading ...');\n    end\n    \n    switch(inStr(pos))\n        case '\"'\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'-','0','1','2','3','4','5','6','7','8','9'}\n            val = parse_number(varargin{:});\n            return;\n        case 't'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true')\n                val = true;\n                pos = pos + 4;\n                return;\n            end\n        case 'f'\n            if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false')\n                val = false;\n                pos = pos + 5;\n                return;\n            end\n        case 'n'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null')\n                val = [];\n                pos = pos + 4;\n                return;\n            end\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/loadubjson.m",
    "content": "function data = loadubjson(fname,varargin)\n%\n% data=loadubjson(fname,opt)\n%    or\n% data=loadubjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/01\n%\n% $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a UBJSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.IntEndian [B|L]: specify the endianness of the integer fields\n%                         in the UBJSON input data. B - Big-Endian format for \n%                         integers (as required in the UBJSON specification); \n%                         L - input integer fields are in Little-Endian order.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      obj=struct('string','value','array',[1 2 3]);\n%      ubjdata=saveubjson('obj',obj);\n%      dat=loadubjson(ubjdata)\n%      dat=loadubjson(['examples' filesep 'example1.ubj'])\n%      dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken fileendian systemendian\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\nfileendian=upper(jsonopt('IntEndian','B',opt));\n[os,maxelem,systemendian]=computer;\n\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\n\n\n%%\nfunction newdata=parse_collection(id,data,obj)\n\nif(jsoncount>0 && exist('data','var')) \n    if(~iscell(data))\n       newdata=cell(1);\n       newdata{1}=data;\n       data=newdata;\n    end\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=double(data(j).x0x5F_ArraySize_);\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1); % TODO\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        count=double(parse_number());\n    end\n    if next_char ~= '}'\n        num=0;\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            %parse_char(':');\n            val = parse_value(varargin{:});\n            num=num+1;\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}' || (count>=0 && num>=count)\n                break;\n            end\n            %parse_char(',');\n        end\n    end\n    if(count==-1)\n        parse_char('}');\n    end\n\n%%-------------------------------------------------------------------------\nfunction [cid,len]=elem_info(type)\nid=strfind('iUIlLdD',type);\ndataclass={'int8','uint8','int16','int32','int64','single','double'};\nbytelen=[1,1,2,4,8,4,8];\nif(id>0)\n    cid=dataclass{id};\n    len=bytelen(id);\nelse\n    error_pos('unsupported type at position %d');\nend\n%%-------------------------------------------------------------------------\n\n\nfunction [data adv]=parse_block(type,count,varargin)\nglobal pos inStr isoct fileendian systemendian\n[cid,len]=elem_info(type);\ndatastr=inStr(pos:pos+len*count-1);\nif(isoct)\n    newdata=int8(datastr);\nelse\n    newdata=uint8(datastr);\nend\nid=strfind('iUIlLdD',type);\nif(id<=5 && fileendian~=systemendian)\n    newdata=swapbytes(typecast(newdata,cid));\nend\ndata=typecast(newdata,cid);\nadv=double(len*count);\n\n%%-------------------------------------------------------------------------\n\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim=[];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1);\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        if(next_char=='[')\n            dim=parse_array(varargin{:});\n            count=prod(double(dim));\n        else\n            count=double(parse_number());\n        end\n    end\n    if(~isempty(type))\n        if(count>=0)\n            [object adv]=parse_block(type,count,varargin{:});\n            if(~isempty(dim))\n                object=reshape(object,dim);\n            end\n            pos=pos+adv;\n            return;\n        else\n            endpos=matching_bracket(inStr,pos);\n            [cid,len]=elem_info(type);\n            count=(endpos-pos)/len;\n            [object adv]=parse_block(type,count,varargin{:});\n            pos=pos+adv;\n            parse_char(']');\n            return;\n        end\n    end\n    if next_char ~= ']'\n         while 1\n            val = parse_value(varargin{:});\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            %parse_char(',');\n         end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    if(count==-1)\n        parse_char(']');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    type=inStr(pos);\n    if type ~= 'S' && type ~= 'C' && type ~= 'H'\n        error_pos('String starting with S expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    if(type == 'C')\n        str=inStr(pos);\n        pos=pos+1;\n        return;\n    end\n    bytelen=double(parse_number());\n    if(length(inStr)>=pos+bytelen-1)\n        str=inStr(pos:pos+bytelen-1);\n        pos=pos+bytelen;\n    else\n        error_pos('End of file while expecting end of inStr');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct fileendian systemendian\n    id=strfind('iUIlLdD',inStr(pos));\n    if(isempty(id))\n        error_pos('expecting a number at position %d');\n    end\n    type={'int8','uint8','int16','int32','int64','single','double'};\n    bytelen=[1,1,2,4,8,4,8];\n    datastr=inStr(pos+1:pos+bytelen(id));\n    if(isoct)\n        newdata=int8(datastr);\n    else\n        newdata=uint8(datastr);\n    end\n    if(id<=5 && fileendian~=systemendian)\n        newdata=swapbytes(typecast(newdata,type{id}));\n    end\n    num=typecast(newdata,type{id});\n    pos = pos + bytelen(id)+1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n\n    switch(inStr(pos))\n        case {'S','C','H'}\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'i','U','I','l','L','d','D'}\n            val = parse_number(varargin{:});\n            return;\n        case 'T'\n            val = true;\n            pos = pos + 1;\n            return;\n        case 'F'\n            val = false;\n            pos = pos + 1;\n            return;\n        case {'Z','N'}\n            val = [];\n            pos = pos + 1;\n            return;\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos e1l e1r maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/mergestruct.m",
    "content": "function s=mergestruct(s1,s2)\n%\n% s=mergestruct(s1,s2)\n%\n% merge two struct objects into one\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      s1,s2: a struct object, s1 and s2 can not be arrays\n%\n% output:\n%      s: the merged struct object. fields in s1 and s2 will be combined in s.\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(~isstruct(s1) || ~isstruct(s2))\n    error('input parameters contain non-struct');\nend\nif(length(s1)>1 || length(s2)>1)\n    error('can not merge struct arrays');\nend\nfn=fieldnames(s2);\ns=s1;\nfor i=1:length(fn)              \n    s=setfield(s,fn{i},getfield(s2,fn{i}));\nend\n\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/savejson.m",
    "content": "function json=savejson(rootname,obj,varargin)\n%\n% json=savejson(rootname,obj,filename)\n%    or\n% json=savejson(rootname,obj,opt)\n% json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n% Object Notation) string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09\n%\n% $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array).\n%      filename: a string for the file name to save the output JSON data.\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n%                         of a 1D/2D array;\n%        opt.ArrayIndent [1|0]: if 1, output explicit data array with\n%                         precedent indentation; if 0, no indentation\n%        opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n%                         to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n%                         and $1 represents the sign. For those who want to use\n%                         1e999 to represent Inf, they can set opt.Inf to '$11e999'\n%        opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n%                         to represent NaN\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSONP='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%        opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n%        opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a string in the JSON format (see http://json.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      savejson('jmesh',jsonmesh)\n%      savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\n\nwhitespaces=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nif(jsonopt('Compact',0,opt)==1)\n    whitespaces=struct('tab','','newline','','sep',',');\nend\nif(~isfield(opt,'whitespaces_'))\n    opt.whitespaces_=whitespaces;\nend\n\nnl=whitespaces.newline;\n\njson=obj2json(rootname,obj,rootlevel,opt);\nif(rootisarray)\n    json=sprintf('%s%s',json,nl);\nelse\n    json=sprintf('{%s%s%s}\\n',nl,json,nl);\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=sprintf('%s(%s);%s',jsonp,json,nl);\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    if(jsonopt('SaveBinary',0,opt)==1)\n\t    fid = fopen(opt.FileName, 'wb');\n\t    fwrite(fid,json);\n    else\n\t    fid = fopen(opt.FileName, 'wt');\n\t    fwrite(fid,json,'char');\n    end\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2json(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2json(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2json(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2json(name,item,level,varargin{:});\nelse\n    txt=mat2json(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2json(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=jsonopt('whitespaces_',struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n')),varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nif(len>1)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": [%s',padding0, checkname(name,varargin{:}),nl); name=''; \n    else\n        txt=sprintf('%s[%s',padding0,nl); \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": []',padding0, checkname(name,varargin{:})); name=''; \n    else\n        txt=sprintf('%s[]',padding0); \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n    for i=1:dim(1)\n       txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:}));\n       if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    end\n    if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n    if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2json(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\npadding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1));\nnl=ws.newline;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding0,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding0,nl); end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n  for i=1:dim(1)\n    names = fieldnames(item(i,j));\n    if(~isempty(name) && len==1)\n        txt=sprintf('%s%s\"%s\": {%s',txt,padding1, checkname(name,varargin{:}),nl); \n    else\n        txt=sprintf('%s%s{%s',txt,padding1,nl); \n    end\n    if(~isempty(names))\n      for e=1:length(names)\n\t    txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:}));\n        if(e<length(names)) txt=sprintf('%s%s',txt,','); end\n        txt=sprintf('%s%s',txt,nl);\n      end\n    end\n    txt=sprintf('%s%s}',txt,padding1);\n    if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n  end\n  if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n  if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2json(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding1,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding1,nl); end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    if(isoct)\n        val=regexprep(item(e,:),'\\\\','\\\\');\n        val=regexprep(val,'\"','\\\"');\n        val=regexprep(val,'^\"','\\\"');\n    else\n        val=regexprep(item(e,:),'\\\\','\\\\\\\\');\n        val=regexprep(val,'\"','\\\\\"');\n        val=regexprep(val,'^\"','\\\\\"');\n    end\n    val=escapejsonstring(val);\n    if(len==1)\n        obj=['\"' checkname(name,varargin{:}) '\": ' '\"',val,'\"'];\n\tif(isempty(name)) obj=['\"',val,'\"']; end\n        txt=sprintf('%s%s%s%s',txt,padding1,obj);\n    else\n        txt=sprintf('%s%s%s%s',txt,padding0,['\"',val,'\"']);\n    end\n    if(e==len) sep=''; end\n    txt=sprintf('%s%s',txt,sep);\nend\nif(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2json(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:}))\n    if(isempty(name))\n    \ttxt=sprintf('%s{%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    else\n    \ttxt=sprintf('%s\"%s\": {%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    end\nelse\n    if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0)\n        numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\\[',''),']','');\n    else\n        numtxt=matdata2json(item,level+1,varargin{:});\n    end\n    if(isempty(name))\n    \ttxt=sprintf('%s%s',padding1,numtxt);\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n           \ttxt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        else\n    \t    txt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        end\n    end\n    return;\nend\ndataformat='%s%s%s%s%s';\n\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n    end\n    txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsSparse_\": ','1', sep);\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([iy(:),data'],level+2,varargin{:}), nl);\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,data],level+2,varargin{:}), nl);\n    else\n        % General case, store row and column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,iy,data],level+2,varargin{:}), nl);\n    end\nelse\n    if(isreal(item))\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json(item(:)',level+2,varargin{:}), nl);\n    else\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl);\n    end\nend\ntxt=sprintf('%s%s%s',txt,padding1,'}');\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2json(mat,level,varargin)\n\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\ntab=ws.tab;\nnl=ws.newline;\n\nif(size(mat,1)==1)\n    pre='';\n    post='';\n    level=level-1;\nelse\n    pre=sprintf('[%s',nl);\n    post=sprintf('%s%s]',nl,repmat(tab,1,level-1));\nend\n\nif(isempty(mat))\n    txt='null';\n    return;\nend\nfloatformat=jsonopt('FloatFormat','%.10g',varargin{:});\n%if(numel(mat)>1)\n    formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]];\n%else\n%    formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\\n')]];\n%end\n\nif(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1)\n    formatstr=[repmat(tab,1,level) formatstr];\nend\n\ntxt=sprintf(formatstr,mat');\ntxt(end-length(nl):end)=[];\nif(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1)\n   txt=regexprep(txt,'1','true');\n   txt=regexprep(txt,'0','false');\nend\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],\\n['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\ntxt=[pre txt post];\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n\n%%-------------------------------------------------------------------------\nfunction newstr=escapejsonstring(str)\nnewstr=str;\nisoct=exist('OCTAVE_VERSION','builtin');\nif(isoct)\n   vv=sscanf(OCTAVE_VERSION,'%f');\n   if(vv(1)>=3.8) isoct=0; end\nend\nif(isoct)\n  escapechars={'\\a','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},escapechars{i});\n  end\nelse\n  escapechars={'\\a','\\b','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\\\','\\\\\\\\'));\n  end\nend\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/saveubjson.m",
    "content": "function json=saveubjson(rootname,obj,varargin)\n%\n% json=saveubjson(rootname,obj,filename)\n%    or\n% json=saveubjson(rootname,obj,opt)\n% json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a Universal \n% Binary JSON (UBJSON) binary string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/17\n%\n% $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array)\n%      filename: a string for the file name to save the output UBJSON data\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSON='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a binary string in the UBJSON format (see http://ubjson.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      saveubjson('jsonmesh',jsonmesh)\n%      saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\njson=obj2ubjson(rootname,obj,rootlevel,opt);\nif(~rootisarray)\n    json=['{' json '}'];\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=[jsonp '(' json ')'];\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    fid = fopen(opt.FileName, 'wb');\n    fwrite(fid,json);\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2ubjson(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2ubjson(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2ubjson(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2ubjson(name,item,level,varargin{:});\nelse\n    txt=mat2ubjson(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2ubjson(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item); % let's handle 1D cell first\nif(len>1) \n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) '[']; name=''; \n    else\n        txt='['; \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; \n    else\n        txt='Z'; \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=[txt '[']; end\n    for i=1:dim(1)\n       txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})];\n    end\n    if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2ubjson(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=[txt '[']; end\n  for i=1:dim(1)\n     names = fieldnames(item(i,j));\n     if(~isempty(name) && len==1)\n        txt=[txt S_(checkname(name,varargin{:})) '{']; \n     else\n        txt=[txt '{']; \n     end\n     if(~isempty(names))\n       for e=1:length(names)\n\t     txt=[txt obj2ubjson(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})];\n       end\n     end\n     txt=[txt '}'];\n  end\n  if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2ubjson(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    val=item(e,:);\n    if(len==1)\n        obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),''];\n\tif(isempty(name)) obj=['',S_(val),'']; end\n        txt=[txt,'',obj];\n    else\n        txt=[txt,'',['',S_(val),'']];\n    end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2ubjson(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) || jsonopt('ArrayToStruct',0,varargin{:}))\n      cid=I_(uint32(max(size(item))));\n      if(isempty(name))\n    \ttxt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ];\n      else\n          if(isempty(item))\n              txt=[S_(checkname(name,varargin{:})),'Z'];\n              return;\n          else\n    \t      txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))];\n          end\n      end\nelse\n    if(isempty(name))\n    \ttxt=matdata2ubjson(item,level+1,varargin{:});\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n            numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\\[',''),']','');\n           \ttxt=[S_(checkname(name,varargin{:})) numtxt];\n        else\n    \t    txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})];\n        end\n    end\n    return;\nend\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=[txt,S_('_ArrayIsComplex_'),'T'];\n    end\n    txt=[txt,S_('_ArrayIsSparse_'),'T'];\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([iy(:),data'],level+2,varargin{:})];\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,data],level+2,varargin{:})];\n    else\n        % General case, store row and column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,iy,data],level+2,varargin{:})];\n    end\nelse\n    if(isreal(item))\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson(item(:)',level+2,varargin{:})];\n    else\n        txt=[txt,S_('_ArrayIsComplex_'),'T'];\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})];\n    end\nend\ntxt=[txt,'}'];\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2ubjson(mat,level,varargin)\nif(isempty(mat))\n    txt='Z';\n    return;\nend\nif(size(mat,1)==1)\n    level=level-1;\nend\ntype='';\nhasnegtive=(mat<0);\nif(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0)))\n    if(isempty(hasnegtive))\n       if(max(mat(:))<=2^8)\n           type='U';\n       end\n    end\n    if(isempty(type))\n        % todo - need to consider negative ones separately\n        id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]);\n        if(isempty(find(id)))\n            error('high-precision data is not yet supported');\n        end\n        key='iIlL';\n\ttype=key(find(id));\n    end\n    txt=[I_a(mat(:),type,size(mat))];\nelseif(islogical(mat))\n    logicalval='FT';\n    if(numel(mat)==1)\n        txt=logicalval(mat+1);\n    else\n        txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')];\n    end\nelse\n    if(numel(mat)==1)\n        txt=['[' D_(mat) ']'];\n    else\n        txt=D_a(mat(:),'D',size(mat));\n    end\nend\n\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n%%-------------------------------------------------------------------------\nfunction val=S_(str)\nif(length(str)==1)\n  val=['C' str];\nelse\n  val=['S' I_(int32(length(str))) str];\nend\n%%-------------------------------------------------------------------------\nfunction val=I_(num)\nif(~isinteger(num))\n    error('input is not an integer');\nend\nif(num>=0 && num<255)\n   val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')];\n   return;\nend\nkey='iIlL';\ncid={'int8','int16','int32','int64'};\nfor i=1:4\n  if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1)))\n    val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')];\n    return;\n  end\nend\nerror('unsupported integer');\n\n%%-------------------------------------------------------------------------\nfunction val=D_(num)\nif(~isfloat(num))\n    error('input is not a float');\nend\n\nif(isa(num,'single'))\n  val=['d' data2byte(num,'uint8')];\nelse\n  val=['D' data2byte(num,'uint8')];\nend\n%%-------------------------------------------------------------------------\nfunction data=I_a(num,type,dim,format)\nid=find(ismember('iUIlL',type));\n\nif(id==0)\n  error('unsupported integer array');\nend\n\n% based on UBJSON specs, all integer types are stored in big endian format\n\nif(id==1)\n  data=data2byte(swapbytes(int8(num)),'uint8');\n  blen=1;\nelseif(id==2)\n  data=data2byte(swapbytes(uint8(num)),'uint8');\n  blen=1;\nelseif(id==3)\n  data=data2byte(swapbytes(int16(num)),'uint8');\n  blen=2;\nelseif(id==4)\n  data=data2byte(swapbytes(int32(num)),'uint8');\n  blen=4;\nelseif(id==5)\n  data=data2byte(swapbytes(int64(num)),'uint8');\n  blen=8;\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/blen)) data(:)'];\n  end\n  data=['[' data(:)'];\nelse\n  data=reshape(data,blen,numel(data)/blen);\n  data(2:blen+1,:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction data=D_a(num,type,dim,format)\nid=find(ismember('dD',type));\n\nif(id==0)\n  error('unsupported float array');\nend\n\nif(id==1)\n  data=data2byte(single(num),'uint8');\nelseif(id==2)\n  data=data2byte(double(num),'uint8');\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)'];\n  end\n  data=['[' data];\nelse\n  data=reshape(data,(id*4),length(data)/(id*4));\n  data(2:(id*4+1),:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction bytes=data2byte(varargin)\nbytes=typecast(varargin{:});\nbytes=bytes(:)';\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/jsonlab/varargin2struct.m",
    "content": "function opt=varargin2struct(varargin)\n%\n% opt=varargin2struct('param1',value1,'param2',value2,...)\n%   or\n% opt=varargin2struct(...,optstruct,...)\n%\n% convert a series of input parameters into a structure\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      'param', value: the input parameters should be pairs of a string and a value\n%       optstruct: if a parameter is a struct, the fields will be merged to the output struct\n%\n% output:\n%      opt: a struct where opt.param1=value1, opt.param2=value2 ...\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nlen=length(varargin);\nopt=struct;\nif(len==0) return; end\ni=1;\nwhile(i<=len)\n    if(isstruct(varargin{i}))\n        opt=mergestruct(opt,varargin{i});\n    elseif(ischar(varargin{i}) && i<len)\n        opt=setfield(opt,varargin{i},varargin{i+1});\n        i=i+1;\n    else\n        error('input must be in the form of ...,''name'',value,... pairs or structs');\n    end\n    i=i+1;\nend\n\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/makeValidFieldName.m",
    "content": "function str = makeValidFieldName(str)\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    isoct=exist('OCTAVE_VERSION','builtin');\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lib/submitWithConfiguration.m",
    "content": "function submitWithConfiguration(conf)\n  addpath('./lib/jsonlab');\n\n  parts = parts(conf);\n\n  fprintf('== Submitting solutions | %s...\\n', conf.itemName);\n\n  tokenFile = 'token.mat';\n  if exist(tokenFile, 'file')\n    load(tokenFile);\n    [email token] = promptToken(email, token, tokenFile);\n  else\n    [email token] = promptToken('', '', tokenFile);\n  end\n\n  if isempty(token)\n    fprintf('!! Submission Cancelled\\n');\n    return\n  end\n\n  try\n    response = submitParts(conf, email, token, parts);\n  catch\n    e = lasterror();\n    fprintf( ...\n      '!! Submission failed: unexpected error: %s\\n', ...\n      e.message);\n    fprintf('!! Please try again later.\\n');\n    return\n  end\n\n  if isfield(response, 'errorMessage')\n    fprintf('!! Submission failed: %s\\n', response.errorMessage);\n  else\n    showFeedback(parts, response);\n    save(tokenFile, 'email', 'token');\n  end\nend\n\nfunction [email token] = promptToken(email, existingToken, tokenFile)\n  if (~isempty(email) && ~isempty(existingToken))\n    prompt = sprintf( ...\n      'Use token from last successful submission (%s)? (Y/n): ', ...\n      email);\n    reenter = input(prompt, 's');\n\n    if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y')\n      token = existingToken;\n      return;\n    else\n      delete(tokenFile);\n    end\n  end\n  email = input('Login (email address): ', 's');\n  token = input('Token: ', 's');\nend\n\nfunction isValid = isValidPartOptionIndex(partOptions, i)\n  isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions));\nend\n\nfunction response = submitParts(conf, email, token, parts)\n  body = makePostBody(conf, email, token, parts);\n  submissionUrl = submissionUrl();\n  params = {'jsonBody', body};\n  responseBody = urlread(submissionUrl, 'post', params);\n  response = loadjson(responseBody);\nend\n\nfunction body = makePostBody(conf, email, token, parts)\n  bodyStruct.assignmentSlug = conf.assignmentSlug;\n  bodyStruct.submitterEmail = email;\n  bodyStruct.secret = token;\n  bodyStruct.parts = makePartsStruct(conf, parts);\n\n  opt.Compact = 1;\n  body = savejson('', bodyStruct, opt);\nend\n\nfunction partsStruct = makePartsStruct(conf, parts)\n  for part = parts\n    partId = part{:}.id;\n    fieldName = makeValidFieldName(partId);\n    outputStruct.output = conf.output(partId);\n    partsStruct.(fieldName) = outputStruct;\n  end\nend\n\nfunction [parts] = parts(conf)\n  parts = {};\n  for partArray = conf.partArrays\n    part.id = partArray{:}{1};\n    part.sourceFiles = partArray{:}{2};\n    part.name = partArray{:}{3};\n    parts{end + 1} = part;\n  end\nend\n\nfunction showFeedback(parts, response)\n  fprintf('== \\n');\n  fprintf('== %43s | %9s | %-s\\n', 'Part Name', 'Score', 'Feedback');\n  fprintf('== %43s | %9s | %-s\\n', '---------', '-----', '--------');\n  for part = parts\n    score = '';\n    partFeedback = '';\n    partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id));\n    partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id));\n    score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore);\n    fprintf('== %43s | %9s | %-s\\n', part{:}.name, score, partFeedback);\n  end\n  evaluation = response.evaluation;\n  totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore);\n  fprintf('==                                   --------------------------------\\n');\n  fprintf('== %43s | %9s | %-s\\n', '', totalScore, '');\n  fprintf('== \\n');\nend\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%\n% Service configuration\n%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\nfunction submissionUrl = submissionUrl()\n  submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1';\nend\n"
  },
  {
    "path": "machine-learning-ex3/ex3/lrCostFunction.m",
    "content": "function [J, grad] = lrCostFunction(theta, X, y, lambda)\n%LRCOSTFUNCTION Compute cost and gradient for logistic regression with \n%regularization\n%   J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using\n%   theta as the parameter for regularized logistic regression and the\n%   gradient of the cost w.r.t. to the parameters. \n\n% Initialize some useful values\nm = length(y); % number of training examples\n\n% You need to return the following variables correctly \nJ = 0;\ngrad = zeros(size(theta));\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the cost of a particular choice of theta.\n%               You should set J to the cost.\n%               Compute the partial derivatives and set grad to the partial\n%               derivatives of the cost w.r.t. each parameter in theta\n%\n% Hint: The computation of the cost function and gradients can be\n%       efficiently vectorized. For example, consider the computation\n%\n%           sigmoid(X * theta)\n%\n%       Each row of the resulting matrix will contain the value of the\n%       prediction for that example. You can make use of this to vectorize\n%       the cost function and gradient computations. \n%\n% Hint: When computing the gradient of the regularized cost function, \n%       there're many possible vectorized solutions, but one solution\n%       looks like:\n%           grad = (unregularized gradient for logistic regression)\n%           temp = theta; \n%           temp(1) = 0;   % because we don't add anything for j = 0  \n%           grad = grad + YOUR_CODE_HERE (using the temp variable)\n%\n\nh = sigmoid(X*theta);\ntheta1 = theta;\ntheta1(1) = 0;\n\nJ = (-y'*log(h)-(1-y)'*log(1-h)+theta1'*theta1*lambda/2)/m;\n\ngrad = X'*(h-y)/m+lambda/m*theta1;\n\n\n% =============================================================\n\ngrad = grad(:);\n\nend\n"
  },
  {
    "path": "machine-learning-ex3/ex3/oneVsAll.m",
    "content": "function [all_theta] = oneVsAll(X, y, num_labels, lambda)\n%ONEVSALL trains multiple logistic regression classifiers and returns all\n%the classifiers in a matrix all_theta, where the i-th row of all_theta \n%corresponds to the classifier for label i\n%   [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels\n%   logisitc regression classifiers and returns each of these classifiers\n%   in a matrix all_theta, where the i-th row of all_theta corresponds \n%   to the classifier for label i\n\n% Some useful variables\nm = size(X, 1);\nn = size(X, 2);\n\n% You need to return the following variables correctly \nall_theta = zeros(n + 1 , num_labels);\n\n% Add ones to the X data matrix\nX = [ones(m, 1) X];\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: You should complete the following code to train num_labels\n%               logistic regression classifiers with regularization\n%               parameter lambda. \n%\n% Hint: theta(:) will return a column vector.\n%\n% Hint: You can use y == c to obtain a vector of 1's and 0's that tell use \n%       whether the ground truth is true/false for this class.\n%\n% Note: For this assignment, we recommend using fmincg to optimize the cost\n%       function. It is okay to use a for-loop (for c = 1:num_labels) to\n%       loop over the different classes.\n%\n%       fmincg works similarly to fminunc, but is more efficient when we\n%       are dealing with large number of parameters.\n%\n% Example Code for fmincg:\n%\n%     % Set Initial theta\n%     initial_theta = zeros(n + 1, 1);\n%     \n%     % Set options for fminunc\n%     options = optimset('GradObj', 'on', 'MaxIter', 50);\n% \n%     % Run fmincg to obtain the optimal theta\n%     % This function will return theta and the cost \n%     [theta] = ...\n%         fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...\n%                 initial_theta, options);\n%\n\nclasses = zeros(m, num_labels);\nfor i=1:num_labels\n    classes(:,i) = y==i;\nend\n\ninitial_theta = zeros(n + 1, 1);\n\n% Set options for fminunc\noptions = optimset('GradObj', 'on', 'MaxIter', 50);\n\n% Run fmincg to obtain the optimal theta\n% This function will return theta and the cost \nfor i = 1:num_labels\n    [all_theta(:,i)] = fmincg (@(t)(lrCostFunction(t, X, classes(:,i), lambda)), ...\n                  initial_theta, options);\nend\n\n\nall_theta = all_theta';\n\n\n% =========================================================================\n\n\nend\n"
  },
  {
    "path": "machine-learning-ex3/ex3/predict.m",
    "content": "function p = predict(Theta1, Theta2, X)\n%PREDICT Predict the label of an input given a trained neural network\n%   p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the\n%   trained weights of a neural network (Theta1, Theta2)\n\n% Useful values\nm = size(X, 1);\nnum_labels = size(Theta2, 1);\n\n% You need to return the following variables correctly \np = zeros(size(X, 1), 1);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Complete the following code to make predictions using\n%               your learned neural network. You should set p to a \n%               vector containing labels between 1 to num_labels.\n%\n% Hint: The max function might come in useful. In particular, the max\n%       function can also return the index of the max element, for more\n%       information see 'help max'. If your examples are in rows, then, you\n%       can use max(A, [], 2) to obtain the max for each row.\n%\np = zeros(size(X,1),1);\nprob = zeros(size(X,1),1);\n\na1 = [ones(m,1),X];\na2 = sigmoid(a1*Theta1');\na2 = [ones(m,1),a2];\na3 = sigmoid(a2*Theta2');\n\n[prob p] = max(a3,[],2);\n\n\n\n\n% =========================================================================\n\n\nend\n"
  },
  {
    "path": "machine-learning-ex3/ex3/predictOneVsAll.m",
    "content": "function p = predictOneVsAll(all_theta, X)\n%PREDICT Predict the label for a trained one-vs-all classifier. The labels \n%are in the range 1..K, where K = size(all_theta, 1). \n%  p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions\n%  for each example in the matrix X. Note that X contains the examples in\n%  rows. all_theta is a matrix where the i-th row is a trained logistic\n%  regression theta vector for the i-th class. You should set p to a vector\n%  of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2\n%  for 4 examples) \n\nm = size(X, 1);\nnum_labels = size(all_theta, 1);\n\n% You need to return the following variables correctly \np = zeros(size(X, 1), 1);\n\n% Add ones to the X data matrix\nX = [ones(m, 1) X];\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Complete the following code to make predictions using\n%               your learned logistic regression parameters (one-vs-all).\n%               You should set p to a vector of predictions (from 1 to\n%               num_labels).\n%\n% Hint: This code can be done all vectorized using the max function.\n%       In particular, the max function can also return the index of the \n%       max element, for more information see 'help max'. If your examples \n%       are in rows, then, you can use max(A, [], 2) to obtain the max \n%       for each row.\n%       \nh = sigmoid(X*all_theta');\n[prob p] = max(h,[],2);%ÿеֵڵк\n\n\n\n\n% =========================================================================\n\n\nend\n"
  },
  {
    "path": "machine-learning-ex3/ex3/sigmoid.m",
    "content": "function g = sigmoid(z)\n%SIGMOID Compute sigmoid functoon\n%   J = SIGMOID(z) computes the sigmoid of z.\n\ng = 1.0 ./ (1.0 + exp(-z));\nend\n"
  },
  {
    "path": "machine-learning-ex3/ex3/submit.m",
    "content": "function submit()\n  addpath('./lib');\n\n  conf.assignmentSlug = 'multi-class-classification-and-neural-networks';\n  conf.itemName = 'Multi-class Classification and Neural Networks';\n  conf.partArrays = { ...\n    { ...\n      '1', ...\n      { 'lrCostFunction.m' }, ...\n      'Regularized Logistic Regression', ...\n    }, ...\n    { ...\n      '2', ...\n      { 'oneVsAll.m' }, ...\n      'One-vs-All Classifier Training', ...\n    }, ...\n    { ...\n      '3', ...\n      { 'predictOneVsAll.m' }, ...\n      'One-vs-All Classifier Prediction', ...\n    }, ...\n    { ...\n      '4', ...\n      { 'predict.m' }, ...\n      'Neural Network Prediction Function' ...\n    }, ...\n  };\n  conf.output = @output;\n\n  submitWithConfiguration(conf);\nend\n\nfunction out = output(partId, auxdata)\n  % Random Test Cases\n  X = [ones(20,1) (exp(1) * sin(1:1:20))' (exp(0.5) * cos(1:1:20))'];\n  y = sin(X(:,1) + X(:,2)) > 0;\n  Xm = [ -1 -1 ; -1 -2 ; -2 -1 ; -2 -2 ; ...\n          1 1 ;  1 2 ;  2 1 ; 2 2 ; ...\n         -1 1 ;  -1 2 ;  -2 1 ; -2 2 ; ...\n          1 -1 ; 1 -2 ;  -2 -1 ; -2 -2 ];\n  ym = [ 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 ]';\n  t1 = sin(reshape(1:2:24, 4, 3));\n  t2 = cos(reshape(1:2:40, 4, 5));\n\n  if partId == '1'\n    [J, grad] = lrCostFunction([0.25 0.5 -0.5]', X, y, 0.1);\n    out = sprintf('%0.5f ', J);\n    out = [out sprintf('%0.5f ', grad)];\n  elseif partId == '2'\n    out = sprintf('%0.5f ', oneVsAll(Xm, ym, 4, 0.1));\n  elseif partId == '3'\n    out = sprintf('%0.5f ', predictOneVsAll(t1, Xm));\n  elseif partId == '4'\n    out = sprintf('%0.5f ', predict(t1, t2, Xm));\n  end \nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/checkNNGradients.m",
    "content": "function checkNNGradients(lambda)\n%CHECKNNGRADIENTS Creates a small neural network to check the\n%backpropagation gradients\n%   CHECKNNGRADIENTS(lambda) Creates a small neural network to check the\n%   backpropagation gradients, it will output the analytical gradients\n%   produced by your backprop code and the numerical gradients (computed\n%   using computeNumericalGradient). These two gradient computations should\n%   result in very similar values.\n%\n\nif ~exist('lambda', 'var') || isempty(lambda)\n    lambda = 0;\nend\n\ninput_layer_size = 3;\nhidden_layer_size = 5;\nnum_labels = 3;\nm = 5;\n\n% We generate some 'random' test data\nTheta1 = debugInitializeWeights(hidden_layer_size, input_layer_size);\nTheta2 = debugInitializeWeights(num_labels, hidden_layer_size);\n% Reusing debugInitializeWeights to generate X\nX  = debugInitializeWeights(m, input_layer_size - 1);\ny  = 1 + mod(1:m, num_labels)';\n\n% Unroll parameters\nnn_params = [Theta1(:) ; Theta2(:)];\n\n% Short hand for cost function\ncostFunc = @(p) nnCostFunction(p, input_layer_size, hidden_layer_size, ...\n                               num_labels, X, y, lambda);\n\n[cost, grad] = costFunc(nn_params);\nnumgrad = computeNumericalGradient(costFunc, nn_params);\n\n% Visually examine the two gradient computations.  The two columns\n% you get should be very similar. \ndisp([numgrad grad]);\nfprintf(['The above two columns you get should be very similar.\\n' ...\n         '(Left-Your Numerical Gradient, Right-Analytical Gradient)\\n\\n']);\n\n% Evaluate the norm of the difference between two solutions.  \n% If you have a correct implementation, and assuming you used EPSILON = 0.0001 \n% in computeNumericalGradient.m, then diff below should be less than 1e-9\ndiff = norm(numgrad-grad)/norm(numgrad+grad);\n\nfprintf(['If your backpropagation implementation is correct, then \\n' ...\n         'the relative difference will be small (less than 1e-9). \\n' ...\n         '\\nRelative Difference: %g\\n'], diff);\n\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/computeNumericalGradient.m",
    "content": "function numgrad = computeNumericalGradient(J, theta)\n%COMPUTENUMERICALGRADIENT Computes the gradient using \"finite differences\"\n%and gives us a numerical estimate of the gradient.\n%   numgrad = COMPUTENUMERICALGRADIENT(J, theta) computes the numerical\n%   gradient of the function J around theta. Calling y = J(theta) should\n%   return the function value at theta.\n\n% Notes: The following code implements numerical gradient checking, and \n%        returns the numerical gradient.It sets numgrad(i) to (a numerical \n%        approximation of) the partial derivative of J with respect to the \n%        i-th input argument, evaluated at theta. (i.e., numgrad(i) should \n%        be the (approximately) the partial derivative of J with respect \n%        to theta(i).)\n%                \n\nnumgrad = zeros(size(theta));\nperturb = zeros(size(theta));\ne = 1e-4;\nfor p = 1:numel(theta)\n    % Set perturbation vector\n    perturb(p) = e;\n    loss1 = J(theta - perturb);\n    loss2 = J(theta + perturb);\n    % Compute Numerical Gradient\n    numgrad(p) = (loss2 - loss1) / (2*e);\n    perturb(p) = 0;\nend\n\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/debugInitializeWeights.m",
    "content": "function W = debugInitializeWeights(fan_out, fan_in)\n%DEBUGINITIALIZEWEIGHTS Initialize the weights of a layer with fan_in\n%incoming connections and fan_out outgoing connections using a fixed\n%strategy, this will help you later in debugging\n%   W = DEBUGINITIALIZEWEIGHTS(fan_in, fan_out) initializes the weights \n%   of a layer with fan_in incoming connections and fan_out outgoing \n%   connections using a fix set of values\n%\n%   Note that W should be set to a matrix of size(1 + fan_in, fan_out) as\n%   the first row of W handles the \"bias\" terms\n%\n\n% Set W to zeros\nW = zeros(fan_out, 1 + fan_in);\n\n% Initialize W using \"sin\", this ensures that W is always of the same\n% values and will be useful for debugging\nW = reshape(sin(1:numel(W)), size(W)) / 10;\n\n% =========================================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/displayData.m",
    "content": "function [h, display_array] = displayData(X, example_width)\n%DISPLAYDATA Display 2D data in a nice grid\n%   [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data\n%   stored in X in a nice grid. It returns the figure handle h and the \n%   displayed array if requested.\n\n% Set example_width automatically if not passed in\nif ~exist('example_width', 'var') || isempty(example_width) \n\texample_width = round(sqrt(size(X, 2)));\nend\n\n% Gray Image\ncolormap(gray);\n\n% Compute rows, cols\n[m n] = size(X);\nexample_height = (n / example_width);\n\n% Compute number of items to display\ndisplay_rows = floor(sqrt(m));\ndisplay_cols = ceil(m / display_rows);\n\n% Between images padding\npad = 1;\n\n% Setup blank display\ndisplay_array = - ones(pad + display_rows * (example_height + pad), ...\n                       pad + display_cols * (example_width + pad));\n\n% Copy each example into a patch on the display array\ncurr_ex = 1;\nfor j = 1:display_rows\n\tfor i = 1:display_cols\n\t\tif curr_ex > m, \n\t\t\tbreak; \n\t\tend\n\t\t% Copy the patch\n\t\t\n\t\t% Get the max value of the patch\n\t\tmax_val = max(abs(X(curr_ex, :)));\n\t\tdisplay_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...\n\t\t              pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...\n\t\t\t\t\t\treshape(X(curr_ex, :), example_height, example_width) / max_val;\n\t\tcurr_ex = curr_ex + 1;\n\tend\n\tif curr_ex > m, \n\t\tbreak; \n\tend\nend\n\n% Display Image\nh = imagesc(display_array, [-1 1]);\n\n% Do not show axis\naxis image off\n\ndrawnow;\n\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/ex4.m",
    "content": "%% Machine Learning Online Class - Exercise 4 Neural Network Learning\n\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the\n%  linear exercise. You will need to complete the following functions \n%  in this exericse:\n%\n%     sigmoidGradient.m\n%     randInitializeWeights.m\n%     nnCostFunction.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% Setup the parameters you will use for this exercise\ninput_layer_size  = 400;  % 20x20 Input Images of Digits\nhidden_layer_size = 25;   % 25 hidden units\nnum_labels = 10;          % 10 labels, from 1 to 10   \n                          % (note that we have mapped \"0\" to label 10)\n\n%% =========== Part 1: Loading and Visualizing Data =============\n%  We start the exercise by first loading and visualizing the dataset. \n%  You will be working with a dataset that contains handwritten digits.\n%\n\n% Load Training Data\nfprintf('Loading and Visualizing Data ...\\n')\n\nload('ex4data1.mat');\nm = size(X, 1);\n\n% Randomly select 100 data points to display\nsel = randperm(size(X, 1));\nsel = sel(1:100);\n\ndisplayData(X(sel, :));\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% ================ Part 2: Loading Parameters ================\n% In this part of the exercise, we load some pre-initialized \n% neural network parameters.\n\nfprintf('\\nLoading Saved Neural Network Parameters ...\\n')\n\n% Load the weights into variables Theta1 and Theta2\nload('ex4weights.mat');\n\n% Unroll parameters \nnn_params = [Theta1(:) ; Theta2(:)];\n\n%% ================ Part 3: Compute Cost (Feedforward) ================\n%  To the neural network, you should first start by implementing the\n%  feedforward part of the neural network that returns the cost only. You\n%  should complete the code in nnCostFunction.m to return cost. After\n%  implementing the feedforward to compute the cost, you can verify that\n%  your implementation is correct by verifying that you get the same cost\n%  as us for the fixed debugging parameters.\n%\n%  We suggest implementing the feedforward cost *without* regularization\n%  first so that it will be easier for you to debug. Later, in part 4, you\n%  will get to implement the regularized cost.\n%\nfprintf('\\nFeedforward Using Neural Network ...\\n')\n\n% Weight regularization parameter (we set this to 0 here).\nlambda = 0.1;\n\nJ = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, ...\n                   num_labels, X, y, lambda);\n\nfprintf(['Cost at parameters (loaded from ex4weights): %f '...\n         '\\n(this value should be about 0.287629)\\n'], J);\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n%% =============== Part 4: Implement Regularization ===============\n%  Once your cost function implementation is correct, you should now\n%  continue to implement the regularization with the cost.\n%\n\nfprintf('\\nChecking Cost Function (w/ Regularization) ... \\n')\n\n% Weight regularization parameter (we set this to 1 here).\nlambda = 1;\n\nJ = nnCostFunction(nn_params, input_layer_size, hidden_layer_size, ...\n                   num_labels, X, y, lambda);\n\nfprintf(['Cost at parameters (loaded from ex4weights): %f '...\n         '\\n(this value should be about 0.383770)\\n'], J);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% ================ Part 5: Sigmoid Gradient  ================\n%  Before you start implementing the neural network, you will first\n%  implement the gradient for the sigmoid function. You should complete the\n%  code in the sigmoidGradient.m file.\n%\n\nfprintf('\\nEvaluating sigmoid gradient...\\n')\n\ng = sigmoidGradient([1 -0.5 0 0.5 1]);\nfprintf('Sigmoid gradient evaluated at [1 -0.5 0 0.5 1]:\\n  ');\nfprintf('%f ', g);\nfprintf('\\n\\n');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% ================ Part 6: Initializing Pameters ================\n%  In this part of the exercise, you will be starting to implment a two\n%  layer neural network that classifies digits. You will start by\n%  implementing a function to initialize the weights of the neural network\n%  (randInitializeWeights.m)\n\nfprintf('\\nInitializing Neural Network Parameters ...\\n')\n\ninitial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);\ninitial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);\n\n% Unroll parameters\ninitial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];\n\n\n%% =============== Part 7: Implement Backpropagation ===============\n%  Once your cost matches up with ours, you should proceed to implement the\n%  backpropagation algorithm for the neural network. You should add to the\n%  code you've written in nnCostFunction.m to return the partial\n%  derivatives of the parameters.\n%\nfprintf('\\nChecking Backpropagation... \\n');\n\n%  Check gradients by running checkNNGradients\ncheckNNGradients;\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n\n%% =============== Part 8: Implement Regularization ===============\n%  Once your backpropagation implementation is correct, you should now\n%  continue to implement the regularization with the cost and gradient.\n%\n\nfprintf('\\nChecking Backpropagation (w/ Regularization) ... \\n')\n\n%  Check gradients by running checkNNGradients\nlambda = 3;\ncheckNNGradients(lambda);\n\n% Also output the costFunction debugging values\ndebug_J  = nnCostFunction(nn_params, input_layer_size, ...\n                          hidden_layer_size, num_labels, X, y, lambda);\n\nfprintf(['\\n\\nCost at (fixed) debugging parameters (w/ lambda = 10): %f ' ...\n         '\\n(this value should be about 0.576051)\\n\\n'], debug_J);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% =================== Part 8: Training NN ===================\n%  You have now implemented all the code necessary to train a neural \n%  network. To train your neural network, we will now use \"fmincg\", which\n%  is a function which works similarly to \"fminunc\". Recall that these\n%  advanced optimizers are able to train our cost functions efficiently as\n%  long as we provide them with the gradient computations.\n%\nfprintf('\\nTraining Neural Network... \\n')\n\n%  After you have completed the assignment, change the MaxIter to a larger\n%  value to see how more training helps.\noptions = optimset('MaxIter', 50);\n\n%  You should also try different values of lambda\nlambda = 1;\n\n% Create \"short hand\" for the cost function to be minimized\ncostFunction = @(p) nnCostFunction(p, ...\n                                   input_layer_size, ...\n                                   hidden_layer_size, ...\n                                   num_labels, X, y, lambda);\n\n% Now, costFunction is a function that takes in only one argument (the\n% neural network parameters)\n[nn_params, cost] = fmincg(costFunction, initial_nn_params, options);\n\n% Obtain Theta1 and Theta2 back from nn_params\nTheta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...\n                 hidden_layer_size, (input_layer_size + 1));\n\nTheta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...\n                 num_labels, (hidden_layer_size + 1));\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% ================= Part 9: Visualize Weights =================\n%  You can now \"visualize\" what the neural network is learning by \n%  displaying the hidden units to see what features they are capturing in \n%  the data.\n\nfprintf('\\nVisualizing Neural Network... \\n')\n\ndisplayData(Theta1(:, 2:end));\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n%% ================= Part 10: Implement Predict =================\n%  After training the neural network, we would like to use it to predict\n%  the labels. You will now implement the \"predict\" function to use the\n%  neural network to predict the labels of the training set. This lets\n%  you compute the training set accuracy.\n\npred = predict(Theta1, Theta2, X);\n\nfprintf('\\nTraining Set Accuracy: %f\\n', mean(double(pred == y)) * 100);\n\n\n"
  },
  {
    "path": "machine-learning-ex4/ex4/fmincg.m",
    "content": "function [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)\n% Minimize a continuous differentialble multivariate function. Starting point\n% is given by \"X\" (D by 1), and the function named in the string \"f\", must\n% return a function value and a vector of partial derivatives. The Polack-\n% Ribiere flavour of conjugate gradients is used to compute search directions,\n% and a line search using quadratic and cubic polynomial approximations and the\n% Wolfe-Powell stopping criteria is used together with the slope ratio method\n% for guessing initial step sizes. Additionally a bunch of checks are made to\n% make sure that exploration is taking place and that extrapolation will not\n% be unboundedly large. The \"length\" gives the length of the run: if it is\n% positive, it gives the maximum number of line searches, if negative its\n% absolute gives the maximum allowed number of function evaluations. You can\n% (optionally) give \"length\" a second component, which will indicate the\n% reduction in function value to be expected in the first line-search (defaults\n% to 1.0). The function returns when either its length is up, or if no further\n% progress can be made (ie, we are at a minimum, or so close that due to\n% numerical problems, we cannot get any closer). If the function terminates\n% within a few iterations, it could be an indication that the function value\n% and derivatives are not consistent (ie, there may be a bug in the\n% implementation of your \"f\" function). The function returns the found\n% solution \"X\", a vector of function values \"fX\" indicating the progress made\n% and \"i\" the number of iterations (line searches or function evaluations,\n% depending on the sign of \"length\") used.\n%\n% Usage: [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)\n%\n% See also: checkgrad \n%\n% Copyright (C) 2001 and 2002 by Carl Edward Rasmussen. Date 2002-02-13\n%\n%\n% (C) Copyright 1999, 2000 & 2001, Carl Edward Rasmussen\n% \n% Permission is granted for anyone to copy, use, or modify these\n% programs and accompanying documents for purposes of research or\n% education, provided this copyright notice is retained, and note is\n% made of any changes that have been made.\n% \n% These programs and documents are distributed without any warranty,\n% express or implied.  As the programs were written for research\n% purposes only, they have not been tested to the degree that would be\n% advisable in any important application.  All use of these programs is\n% entirely at the user's own risk.\n%\n% [ml-class] Changes Made:\n% 1) Function name and argument specifications\n% 2) Output display\n%\n\n% Read options\nif exist('options', 'var') && ~isempty(options) && isfield(options, 'MaxIter')\n    length = options.MaxIter;\nelse\n    length = 100;\nend\n\n\nRHO = 0.01;                            % a bunch of constants for line searches\nSIG = 0.5;       % RHO and SIG are the constants in the Wolfe-Powell conditions\nINT = 0.1;    % don't reevaluate within 0.1 of the limit of the current bracket\nEXT = 3.0;                    % extrapolate maximum 3 times the current bracket\nMAX = 20;                         % max 20 function evaluations per line search\nRATIO = 100;                                      % maximum allowed slope ratio\n\nargstr = ['feval(f, X'];                      % compose string used to call function\nfor i = 1:(nargin - 3)\n  argstr = [argstr, ',P', int2str(i)];\nend\nargstr = [argstr, ')'];\n\nif max(size(length)) == 2, red=length(2); length=length(1); else red=1; end\nS=['Iteration '];\n\ni = 0;                                            % zero the run length counter\nls_failed = 0;                             % no previous line search has failed\nfX = [];\n[f1 df1] = eval(argstr);                      % get function value and gradient\ni = i + (length<0);                                            % count epochs?!\ns = -df1;                                        % search direction is steepest\nd1 = -s'*s;                                                 % this is the slope\nz1 = red/(1-d1);                                  % initial step is red/(|s|+1)\n\nwhile i < abs(length)                                      % while not finished\n  i = i + (length>0);                                      % count iterations?!\n\n  X0 = X; f0 = f1; df0 = df1;                   % make a copy of current values\n  X = X + z1*s;                                             % begin line search\n  [f2 df2] = eval(argstr);\n  i = i + (length<0);                                          % count epochs?!\n  d2 = df2'*s;\n  f3 = f1; d3 = d1; z3 = -z1;             % initialize point 3 equal to point 1\n  if length>0, M = MAX; else M = min(MAX, -length-i); end\n  success = 0; limit = -1;                     % initialize quanteties\n  while 1\n    while ((f2 > f1+z1*RHO*d1) || (d2 > -SIG*d1)) && (M > 0) \n      limit = z1;                                         % tighten the bracket\n      if f2 > f1\n        z2 = z3 - (0.5*d3*z3*z3)/(d3*z3+f2-f3);                 % quadratic fit\n      else\n        A = 6*(f2-f3)/z3+3*(d2+d3);                                 % cubic fit\n        B = 3*(f3-f2)-z3*(d3+2*d2);\n        z2 = (sqrt(B*B-A*d2*z3*z3)-B)/A;       % numerical error possible - ok!\n      end\n      if isnan(z2) || isinf(z2)\n        z2 = z3/2;                  % if we had a numerical problem then bisect\n      end\n      z2 = max(min(z2, INT*z3),(1-INT)*z3);  % don't accept too close to limits\n      z1 = z1 + z2;                                           % update the step\n      X = X + z2*s;\n      [f2 df2] = eval(argstr);\n      M = M - 1; i = i + (length<0);                           % count epochs?!\n      d2 = df2'*s;\n      z3 = z3-z2;                    % z3 is now relative to the location of z2\n    end\n    if f2 > f1+z1*RHO*d1 || d2 > -SIG*d1\n      break;                                                % this is a failure\n    elseif d2 > SIG*d1\n      success = 1; break;                                             % success\n    elseif M == 0\n      break;                                                          % failure\n    end\n    A = 6*(f2-f3)/z3+3*(d2+d3);                      % make cubic extrapolation\n    B = 3*(f3-f2)-z3*(d3+2*d2);\n    z2 = -d2*z3*z3/(B+sqrt(B*B-A*d2*z3*z3));        % num. error possible - ok!\n    if ~isreal(z2) || isnan(z2) || isinf(z2) || z2 < 0 % num prob or wrong sign?\n      if limit < -0.5                               % if we have no upper limit\n        z2 = z1 * (EXT-1);                 % the extrapolate the maximum amount\n      else\n        z2 = (limit-z1)/2;                                   % otherwise bisect\n      end\n    elseif (limit > -0.5) && (z2+z1 > limit)         % extraplation beyond max?\n      z2 = (limit-z1)/2;                                               % bisect\n    elseif (limit < -0.5) && (z2+z1 > z1*EXT)       % extrapolation beyond limit\n      z2 = z1*(EXT-1.0);                           % set to extrapolation limit\n    elseif z2 < -z3*INT\n      z2 = -z3*INT;\n    elseif (limit > -0.5) && (z2 < (limit-z1)*(1.0-INT))  % too close to limit?\n      z2 = (limit-z1)*(1.0-INT);\n    end\n    f3 = f2; d3 = d2; z3 = -z2;                  % set point 3 equal to point 2\n    z1 = z1 + z2; X = X + z2*s;                      % update current estimates\n    [f2 df2] = eval(argstr);\n    M = M - 1; i = i + (length<0);                             % count epochs?!\n    d2 = df2'*s;\n  end                                                      % end of line search\n\n  if success                                         % if line search succeeded\n    f1 = f2; fX = [fX' f1]';\n    fprintf('%s %4i | Cost: %4.6e\\r', S, i, f1);\n    s = (df2'*df2-df1'*df2)/(df1'*df1)*s - df2;      % Polack-Ribiere direction\n    tmp = df1; df1 = df2; df2 = tmp;                         % swap derivatives\n    d2 = df1'*s;\n    if d2 > 0                                      % new slope must be negative\n      s = -df1;                              % otherwise use steepest direction\n      d2 = -s'*s;    \n    end\n    z1 = z1 * min(RATIO, d1/(d2-realmin));          % slope ratio but max RATIO\n    d1 = d2;\n    ls_failed = 0;                              % this line search did not fail\n  else\n    X = X0; f1 = f0; df1 = df0;  % restore point from before failed line search\n    if ls_failed || i > abs(length)          % line search failed twice in a row\n      break;                             % or we ran out of time, so we give up\n    end\n    tmp = df1; df1 = df2; df2 = tmp;                         % swap derivatives\n    s = -df1;                                                    % try steepest\n    d1 = -s'*s;\n    z1 = 1/(1-d1);                     \n    ls_failed = 1;                                    % this line search failed\n  end\n  if exist('OCTAVE_VERSION')\n    fflush(stdout);\n  end\nend\nfprintf('\\n');\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/AUTHORS.txt",
    "content": "The author of \"jsonlab\" toolbox is Qianqian Fang. Qianqian\nis currently an Assistant Professor at Massachusetts General Hospital, \nHarvard Medical School.\n\nAddress: Martinos Center for Biomedical Imaging, \n         Massachusetts General Hospital, \n         Harvard Medical School\n         Bldg 149, 13th St, Charlestown, MA 02129, USA\nURL: http://nmr.mgh.harvard.edu/~fangq/\nEmail: <fangq at nmr.mgh.harvard.edu> or <fangqq at gmail.com>\n\n\nThe script loadjson.m was built upon previous works by\n\n- Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n       date: 2009/11/02\n- François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n       date: 2009/03/22\n- Joel Feenstra: http://www.mathworks.com/matlabcentral/fileexchange/20565\n       date: 2008/07/03\n\n\nThis toolbox contains patches submitted by the following contributors:\n\n- Blake Johnson <bjohnso at bbn.com>\n  part of revision 341\n\n- Niclas Borlin <Niclas.Borlin at cs.umu.se>\n  various fixes in revision 394, including\n  - loadjson crashes for all-zero sparse matrix.\n  - loadjson crashes for empty sparse matrix.\n  - Non-zero size of 0-by-N and N-by-0 empty matrices is lost after savejson/loadjson.\n  - loadjson crashes for sparse real column vector.\n  - loadjson crashes for sparse complex column vector.\n  - Data is corrupted by savejson for sparse real row vector.\n  - savejson crashes for sparse complex row vector. \n\n- Yul Kang <yul.kang.on at gmail.com>\n  patches for svn revision 415.\n  - savejson saves an empty cell array as [] instead of null\n  - loadjson differentiates an empty struct from an empty array\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/ChangeLog.txt",
    "content": "============================================================================\n\n   JSONlab - a toolbox to encode/decode JSON/UBJSON files in MATLAB/Octave\n\n----------------------------------------------------------------------------\n\nJSONlab ChangeLog (key features marked by *):\n\n== JSONlab 1.0 (codename: Optimus - Final), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2015/01/02 polish help info for all major functions, update examples, finalize 1.0\n 2014/12/19 fix a bug to strictly respect NoRowBracket in savejson\n\n== JSONlab 1.0.0-RC2 (codename: Optimus - RC2), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/11/22 show progress bar in loadjson ('ShowProgress') \n 2014/11/17 add Compact option in savejson to output compact JSON format ('Compact')\n 2014/11/17 add FastArrayParser in loadjson to specify fast parser applicable levels\n 2014/09/18 start official github mirror: https://github.com/fangq/jsonlab\n\n== JSONlab 1.0.0-RC1 (codename: Optimus - RC1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/09/17  fix several compatibility issues when running on octave versions 3.2-3.8\n 2014/09/17  support 2D cell and struct arrays in both savejson and saveubjson\n 2014/08/04  escape special characters in a JSON string\n 2014/02/16  fix a bug when saving ubjson files\n\n== JSONlab 0.9.9 (codename: Optimus - beta), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/01/22  use binary read and write in saveubjson and loadubjson\n\n== JSONlab 0.9.8-1 (codename: Optimus - alpha update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2013/10/07 better round-trip conservation for empty arrays and structs (patch submitted by Yul Kang)\n\n== JSONlab 0.9.8 (codename: Optimus - alpha), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2013/08/23 *universal Binary JSON (UBJSON) support, including both saveubjson and loadubjson\n\n== JSONlab 0.9.1 (codename: Rodimus, update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2012/12/18 *handling of various empty and sparse matrices (fixes submitted by Niclas Borlin)\n\n== JSONlab 0.9.0 (codename: Rodimus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/06/17 *new format for an invalid leading char, unpacking hex code in savejson\n 2012/06/01  support JSONP in savejson\n 2012/05/25  fix the empty cell bug (reported by Cyril Davin)\n 2012/04/05  savejson can save to a file (suggested by Patrick Rapin)\n\n== JSONlab 0.8.1 (codename: Sentiel, Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/02/28  loadjson quotation mark escape bug, see http://bit.ly/yyk1nS\n 2012/01/25  patch to handle root-less objects, contributed by Blake Johnson\n\n== JSONlab 0.8.0 (codename: Sentiel), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/01/13 *speed up loadjson by 20 fold when parsing large data arrays in matlab\n 2012/01/11  remove row bracket if an array has 1 element, suggested by Mykel Kochenderfer\n 2011/12/22 *accept sequence of 'param',value input in savejson and loadjson\n 2011/11/18  fix struct array bug reported by Mykel Kochenderfer\n\n== JSONlab 0.5.1 (codename: Nexus Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/21  fix a bug in loadjson, previous code does not use any of the acceleration\n 2011/10/20  loadjson supports JSON collections - concatenated JSON objects\n\n== JSONlab 0.5.0 (codename: Nexus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/16  package and release jsonlab 0.5.0\n 2011/10/15 *add json demo and regression test, support cpx numbers, fix double quote bug\n 2011/10/11 *speed up readjson dramatically, interpret _Array* tags, show data in root level\n 2011/10/10  create jsonlab project, start jsonlab website, add online documentation\n 2011/10/07 *speed up savejson by 25x using sprintf instead of mat2str, add options support\n 2011/10/06 *savejson works for structs, cells and arrays\n 2011/09/09  derive loadjson from JSON parser from MATLAB Central, draft savejson.m\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/LICENSE_BSD.txt",
    "content": "Copyright 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are\npermitted provided that the following conditions are met:\n\n   1. Redistributions of source code must retain the above copyright notice, this list of\n      conditions and the following disclaimer.\n\n   2. Redistributions in binary form must reproduce the above copyright notice, this list\n      of conditions and the following disclaimer in the documentation and/or other materials\n      provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS \nOR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nThe views and conclusions contained in the software and documentation are those of the\nauthors and should not be interpreted as representing official policies, either expressed\nor implied, of the copyright holders.\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/README.txt",
    "content": "===============================================================================\n=                                 JSONLab                                     =\n=           An open-source MATLAB/Octave JSON encoder and decoder             =\n===============================================================================\n\n*Copyright (C) 2011-2015  Qianqian Fang <fangq at nmr.mgh.harvard.edu>\n*License: BSD License, see License_BSD.txt for details\n*Version: 1.0 (Optimus - Final)\n\n-------------------------------------------------------------------------------\n\nTable of Content:\n\nI.  Introduction\nII. Installation\nIII.Using JSONLab\nIV. Known Issues and TODOs\nV.  Contribution and feedback\n\n-------------------------------------------------------------------------------\n\nI.  Introduction\n\nJSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable, \nhuman-readable and \"[http://en.wikipedia.org/wiki/JSON fat-free]\" text format \nto represent complex and hierarchical data. It is as powerful as \n[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely \nused for data-exchange in applications, and is essential for the wild success \nof [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and \n[http://en.wikipedia.org/wiki/Web_2.0 Web2.0]. \n\nUBJSON (Universal Binary JSON) is a binary JSON format, specifically \noptimized for compact file size and better performance while keeping\nthe semantics as simple as the text-based JSON format. Using the UBJSON\nformat allows to wrap complex binary data in a flexible and extensible\nstructure, making it possible to process complex and large dataset \nwithout accuracy loss due to text conversions.\n\nWe envision that both JSON and its binary version will serve as part of \nthe mainstream data-exchange formats for scientific research in the future. \nIt will provide the flexibility and generality achieved by other popular \ngeneral-purpose file specifications, such as\n[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly \nreduced complexity and enhanced performance.\n\nJSONLab is a free and open-source implementation of a JSON/UBJSON encoder \nand a decoder in the native MATLAB language. It can be used to convert a MATLAB \ndata structure (array, struct, cell, struct array and cell array) into \nJSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB \ndata structure. JSONLab supports both MATLAB and  \n[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).\n\n-------------------------------------------------------------------------------\n\nII. Installation\n\nThe installation of JSONLab is no different than any other simple\nMATLAB toolbox. You only need to download/unzip the JSONLab package\nto a folder, and add the folder's path to MATLAB/Octave's path list\nby using the following command:\n\n    addpath('/path/to/jsonlab');\n\nIf you want to add this path permanently, you need to type \"pathtool\", \nbrowse to the jsonlab root folder and add to the list, then click \"Save\".\nThen, run \"rehash\" in MATLAB, and type \"which loadjson\", if you see an \noutput, that means JSONLab is installed for MATLAB/Octave.\n\n-------------------------------------------------------------------------------\n\nIII.Using JSONLab\n\nJSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder, \nand savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and \ntwo equivallent functions -- loadubjson and saveubjson for the binary \nJSON. The detailed help info for the four functions can be found below:\n\n=== loadjson.m ===\n<pre>\n  data=loadjson(fname,opt)\n     or\n  data=loadjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09, including previous works from \n \n          Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n             created on 2009/11/02\n          Franois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n             created on  2009/03/22\n          Joel Feenstra:\n          http://www.mathworks.com/matlabcentral/fileexchange/20565\n             created on 2008/07/03\n \n  $Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a JSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n                          speed-optimized array parser when loading an \n                          array object. The fast array parser may \n                          collapse block arrays into a single large\n                          array similar to rules defined in cell2mat; 0 to \n                          use a legacy parser; if set to a larger-than-1\n                          value, this option will specify the minimum\n                          dimension to enable the fast array parser. For\n                          example, if the input is a 3D array, setting\n                          FastArrayParser to 1 will return a 3D array;\n                          setting to 2 will return a cell array of 2D\n                          arrays; setting to 3 will return to a 2D cell\n                          array of 1D vectors; setting to 4 will return a\n                          3D cell array.\n            opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n       dat=loadjson(['examples' filesep 'example1.json'])\n       dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n</pre>\n\n=== savejson.m ===\n\n<pre>\n  json=savejson(rootname,obj,filename)\n     or\n  json=savejson(rootname,obj,opt)\n  json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n  Object Notation) string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09\n \n  $Id: savejson.m 458 2014-12-19 22:17:17Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array).\n       filename: a string for the file name to save the output JSON data.\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n                          of a 1D/2D array;\n         opt.ArrayIndent [1|0]: if 1, output explicit data array with\n                          precedent indentation; if 0, no indentation\n         opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n                          to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n                          and $1 represents the sign. For those who want to use\n                          1e999 to represent Inf, they can set opt.Inf to '$11e999'\n         opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n                          to represent NaN\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSONP='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n         opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n         opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a string in the JSON format (see http://json.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       savejson('jmesh',jsonmesh)\n       savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n </pre>\n\n=== loadubjson.m ===\n\n<pre>\n  data=loadubjson(fname,opt)\n     or\n  data=loadubjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/01\n \n  $Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a UBJSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.IntEndian [B|L]: specify the endianness of the integer fields\n                          in the UBJSON input data. B - Big-Endian format for \n                          integers (as required in the UBJSON specification); \n                          L - input integer fields are in Little-Endian order.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       obj=struct('string','value','array',[1 2 3]);\n       ubjdata=saveubjson('obj',obj);\n       dat=loadubjson(ubjdata)\n       dat=loadubjson(['examples' filesep 'example1.ubj'])\n       dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n</pre>\n\n=== saveubjson.m ===\n\n<pre>\n  json=saveubjson(rootname,obj,filename)\n     or\n  json=saveubjson(rootname,obj,opt)\n  json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a Universal \n  Binary JSON (UBJSON) binary string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/17\n \n  $Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array)\n       filename: a string for the file name to save the output UBJSON data\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSON='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a binary string in the UBJSON format (see http://ubjson.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       saveubjson('jsonmesh',jsonmesh)\n       saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n</pre>\n\n\n=== examples ===\n\nUnder the \"examples\" folder, you can find several scripts to demonstrate the\nbasic utilities of JSONLab. Running the \"demo_jsonlab_basic.m\" script, you \nwill see the conversions from MATLAB data structure to JSON text and backward.\nIn \"jsonlab_selftest.m\", we load complex JSON files downloaded from the Internet\nand validate the loadjson/savejson functions for regression testing purposes.\nSimilarly, a \"demo_ubjson_basic.m\" script is provided to test the saveubjson\nand loadubjson pairs for various matlab data structures.\n\nPlease run these examples and understand how JSONLab works before you use\nit to process your data.\n\n-------------------------------------------------------------------------------\n\nIV. Known Issues and TODOs\n\nJSONLab has several known limitations. We are striving to make it more general\nand robust. Hopefully in a few future releases, the limitations become less.\n\nHere are the known issues:\n\n# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;\n# When processing names containing multi-byte characters, Octave and MATLAB \\\ncan give different field-names; you can use feature('DefaultCharacterSet','latin1') \\\nin MATLAB to get consistant results\n# savejson can not handle class and dataset.\n# saveubjson converts a logical array into a uint8 ([U]) array\n# an unofficial N-D array count syntax is implemented in saveubjson. We are \\\nactively communicating with the UBJSON spec maintainer to investigate the \\\npossibility of making it upstream\n# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \\\nfiles, however, it can parse all UBJSON files produced by saveubjson.\n\n-------------------------------------------------------------------------------\n\nV. Contribution and feedback\n\nJSONLab is an open-source project. This means you can not only use it and modify\nit as you wish, but also you can contribute your changes back to JSONLab so\nthat everyone else can enjoy the improvement. For anyone who want to contribute,\nplease download JSONLab source code from it's subversion repository by using the\nfollowing command:\n\n svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab\n\nYou can make changes to the files as needed. Once you are satisfied with your\nchanges, and ready to share it with others, please cd the root directory of \nJSONLab, and type\n\n svn diff > yourname_featurename.patch\n\nYou then email the .patch file to JSONLab's maintainer, Qianqian Fang, at\nthe email address shown in the beginning of this file. Qianqian will review \nthe changes and commit it to the subversion if they are satisfactory.\n\nWe appreciate any suggestions and feedbacks from you. Please use iso2mesh's\nmailing list to report any questions you may have with JSONLab:\n\nhttp://groups.google.com/group/iso2mesh-users?hl=en&pli=1\n\n(Subscription to the mailing list is needed in order to post messages).\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/jsonopt.m",
    "content": "function val=jsonopt(key,default,varargin)\n%\n% val=jsonopt(key,default,optstruct)\n%\n% setting options based on a struct. The struct can be produced\n% by varargin2struct from a list of 'param','value' pairs\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n%\n% $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $\n%\n% input:\n%      key: a string with which one look up a value from a struct\n%      default: if the key does not exist, return default\n%      optstruct: a struct where each sub-field is a key \n%\n% output:\n%      val: if key exists, val=optstruct.key; otherwise val=default\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n% \n\nval=default;\nif(nargin<=2) return; end\nopt=varargin{1};\nif(isstruct(opt) && isfield(opt,key))\n    val=getfield(opt,key);\nend\n\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/loadjson.m",
    "content": "function data = loadjson(fname,varargin)\n%\n% data=loadjson(fname,opt)\n%    or\n% data=loadjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09, including previous works from \n%\n%         Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n%            created on 2009/11/02\n%         François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n%            created on  2009/03/22\n%         Joel Feenstra:\n%         http://www.mathworks.com/matlabcentral/fileexchange/20565\n%            created on 2008/07/03\n%\n% $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a JSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n%                         speed-optimized array parser when loading an \n%                         array object. The fast array parser may \n%                         collapse block arrays into a single large\n%                         array similar to rules defined in cell2mat; 0 to \n%                         use a legacy parser; if set to a larger-than-1\n%                         value, this option will specify the minimum\n%                         dimension to enable the fast array parser. For\n%                         example, if the input is a 3D array, setting\n%                         FastArrayParser to 1 will return a 3D array;\n%                         setting to 2 will return a cell array of 2D\n%                         arrays; setting to 3 will return to a 2D cell\n%                         array of 1D vectors; setting to 4 will return a\n%                         3D cell array.\n%           opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n%      dat=loadjson(['examples' filesep 'example1.json'])\n%      dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\n\nif(jsonopt('ShowProgress',0,opt)==1)\n    opt.progressbar_=waitbar(0,'loading ...');\nend\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\nif(isfield(opt,'progressbar_'))\n    close(opt.progressbar_);\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=data(j).x0x5F_ArraySize_;\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    if next_char ~= '}'\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            parse_char(':');\n            val = parse_value(varargin{:});\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}'\n                break;\n            end\n            parse_char(',');\n        end\n    end\n    parse_char('}');\n\n%%-------------------------------------------------------------------------\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim2=[];\n    arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:});\n    pbar=jsonopt('progressbar_',-1,varargin{:});\n\n    if next_char ~= ']'\n\tif(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))\n            [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos);\n            arraystr=['[' inStr(pos:endpos)];\n            arraystr=regexprep(arraystr,'\"_NaN_\"','NaN');\n            arraystr=regexprep(arraystr,'\"([-+]*)_Inf_\"','$1Inf');\n            arraystr(arraystr==sprintf('\\n'))=[];\n            arraystr(arraystr==sprintf('\\r'))=[];\n            %arraystr=regexprep(arraystr,'\\s*,',','); % this is slow,sometimes needed\n            if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D\n        \tastr=inStr((e1l+1):(e1r-1));\n        \tastr=regexprep(astr,'\"_NaN_\"','NaN');\n        \tastr=regexprep(astr,'\"([-+]*)_Inf_\"','$1Inf');\n        \tastr(astr==sprintf('\\n'))=[];\n        \tastr(astr==sprintf('\\r'))=[];\n        \tastr(astr==' ')='';\n        \tif(isempty(find(astr=='[', 1))) % array is 2D\n                    dim2=length(sscanf(astr,'%f,',[1 inf]));\n        \tend\n            else % array is 1D\n        \tastr=arraystr(2:end-1);\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]);\n        \tif(nextidx>=length(astr)-1)\n                    object=obj;\n                    pos=endpos;\n                    parse_char(']');\n                    return;\n        \tend\n            end\n            if(~isempty(dim2))\n        \tastr=arraystr;\n        \tastr(astr=='[')='';\n        \tastr(astr==']')='';\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf);\n        \tif(nextidx>=length(astr)-1)\n                    object=reshape(obj,dim2,numel(obj)/dim2)';\n                    pos=endpos;\n                    parse_char(']');\n                    if(pbar>0)\n                        waitbar(pos/length(inStr),pbar,'loading ...');\n                    end\n                    return;\n        \tend\n            end\n            arraystr=regexprep(arraystr,'\\]\\s*,','];');\n\telse\n            arraystr='[';\n\tend\n        try\n           if(isoct && regexp(arraystr,'\"','once'))\n                error('Octave eval can produce empty cells for JSON-like input');\n           end\n           object=eval(arraystr);\n           pos=endpos;\n        catch\n         while 1\n            newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1);\n            val = parse_value(newopt);\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            parse_char(',');\n         end\n        end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    parse_char(']');\n    \n    if(pbar>0)\n        waitbar(pos/length(inStr),pbar,'loading ...');\n    end\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr len  esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    if inStr(pos) ~= '\"'\n        error_pos('String starting with \" expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    str = '';\n    while pos <= len\n        while index_esc <= len_esc && esc(index_esc) < pos\n            index_esc = index_esc + 1;\n        end\n        if index_esc > len_esc\n            str = [str inStr(pos:len)];\n            pos = len + 1;\n            break;\n        else\n            str = [str inStr(pos:esc(index_esc)-1)];\n            pos = esc(index_esc);\n        end\n        nstr = length(str); switch inStr(pos)\n            case '\"'\n                pos = pos + 1;\n                if(~isempty(str))\n                    if(strcmp(str,'_Inf_'))\n                        str=Inf;\n                    elseif(strcmp(str,'-_Inf_'))\n                        str=-Inf;\n                    elseif(strcmp(str,'_NaN_'))\n                        str=NaN;\n                    end\n                end\n                return;\n            case '\\'\n                if pos+1 > len\n                    error_pos('End of file reached right after escape character');\n                end\n                pos = pos + 1;\n                switch inStr(pos)\n                    case {'\"' '\\' '/'}\n                        str(nstr+1) = inStr(pos);\n                        pos = pos + 1;\n                    case {'b' 'f' 'n' 'r' 't'}\n                        str(nstr+1) = sprintf(['\\' inStr(pos)]);\n                        pos = pos + 1;\n                    case 'u'\n                        if pos+4 > len\n                            error_pos('End of file reached in escaped unicode character');\n                        end\n                        str(nstr+(1:6)) = inStr(pos-1:pos+4);\n                        pos = pos + 5;\n                end\n            otherwise % should never happen\n                str(nstr+1) = inStr(pos), keyboard\n                pos = pos + 1;\n        end\n    end\n    error_pos('End of file while expecting end of inStr');\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct\n    currstr=inStr(pos:end);\n    numstr=0;\n    if(isoct~=0)\n        numstr=regexp(currstr,'^\\s*-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+\\-]?\\d+)?','end');\n        [num, one] = sscanf(currstr, '%f', 1);\n        delta=numstr+1;\n    else\n        [num, one, err, delta] = sscanf(currstr, '%f', 1);\n        if ~isempty(err)\n            error_pos('Error reading number at position %d');\n        end\n    end\n    pos = pos + delta-1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n    \n    pbar=jsonopt('progressbar_',-1,varargin{:});\n    if(pbar>0)\n        waitbar(pos/len,pbar,'loading ...');\n    end\n    \n    switch(inStr(pos))\n        case '\"'\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'-','0','1','2','3','4','5','6','7','8','9'}\n            val = parse_number(varargin{:});\n            return;\n        case 't'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true')\n                val = true;\n                pos = pos + 4;\n                return;\n            end\n        case 'f'\n            if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false')\n                val = false;\n                pos = pos + 5;\n                return;\n            end\n        case 'n'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null')\n                val = [];\n                pos = pos + 4;\n                return;\n            end\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/loadubjson.m",
    "content": "function data = loadubjson(fname,varargin)\n%\n% data=loadubjson(fname,opt)\n%    or\n% data=loadubjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/01\n%\n% $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a UBJSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.IntEndian [B|L]: specify the endianness of the integer fields\n%                         in the UBJSON input data. B - Big-Endian format for \n%                         integers (as required in the UBJSON specification); \n%                         L - input integer fields are in Little-Endian order.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      obj=struct('string','value','array',[1 2 3]);\n%      ubjdata=saveubjson('obj',obj);\n%      dat=loadubjson(ubjdata)\n%      dat=loadubjson(['examples' filesep 'example1.ubj'])\n%      dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken fileendian systemendian\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\nfileendian=upper(jsonopt('IntEndian','B',opt));\n[os,maxelem,systemendian]=computer;\n\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\n\n\n%%\nfunction newdata=parse_collection(id,data,obj)\n\nif(jsoncount>0 && exist('data','var')) \n    if(~iscell(data))\n       newdata=cell(1);\n       newdata{1}=data;\n       data=newdata;\n    end\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=double(data(j).x0x5F_ArraySize_);\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1); % TODO\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        count=double(parse_number());\n    end\n    if next_char ~= '}'\n        num=0;\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            %parse_char(':');\n            val = parse_value(varargin{:});\n            num=num+1;\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}' || (count>=0 && num>=count)\n                break;\n            end\n            %parse_char(',');\n        end\n    end\n    if(count==-1)\n        parse_char('}');\n    end\n\n%%-------------------------------------------------------------------------\nfunction [cid,len]=elem_info(type)\nid=strfind('iUIlLdD',type);\ndataclass={'int8','uint8','int16','int32','int64','single','double'};\nbytelen=[1,1,2,4,8,4,8];\nif(id>0)\n    cid=dataclass{id};\n    len=bytelen(id);\nelse\n    error_pos('unsupported type at position %d');\nend\n%%-------------------------------------------------------------------------\n\n\nfunction [data adv]=parse_block(type,count,varargin)\nglobal pos inStr isoct fileendian systemendian\n[cid,len]=elem_info(type);\ndatastr=inStr(pos:pos+len*count-1);\nif(isoct)\n    newdata=int8(datastr);\nelse\n    newdata=uint8(datastr);\nend\nid=strfind('iUIlLdD',type);\nif(id<=5 && fileendian~=systemendian)\n    newdata=swapbytes(typecast(newdata,cid));\nend\ndata=typecast(newdata,cid);\nadv=double(len*count);\n\n%%-------------------------------------------------------------------------\n\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim=[];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1);\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        if(next_char=='[')\n            dim=parse_array(varargin{:});\n            count=prod(double(dim));\n        else\n            count=double(parse_number());\n        end\n    end\n    if(~isempty(type))\n        if(count>=0)\n            [object adv]=parse_block(type,count,varargin{:});\n            if(~isempty(dim))\n                object=reshape(object,dim);\n            end\n            pos=pos+adv;\n            return;\n        else\n            endpos=matching_bracket(inStr,pos);\n            [cid,len]=elem_info(type);\n            count=(endpos-pos)/len;\n            [object adv]=parse_block(type,count,varargin{:});\n            pos=pos+adv;\n            parse_char(']');\n            return;\n        end\n    end\n    if next_char ~= ']'\n         while 1\n            val = parse_value(varargin{:});\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            %parse_char(',');\n         end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    if(count==-1)\n        parse_char(']');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    type=inStr(pos);\n    if type ~= 'S' && type ~= 'C' && type ~= 'H'\n        error_pos('String starting with S expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    if(type == 'C')\n        str=inStr(pos);\n        pos=pos+1;\n        return;\n    end\n    bytelen=double(parse_number());\n    if(length(inStr)>=pos+bytelen-1)\n        str=inStr(pos:pos+bytelen-1);\n        pos=pos+bytelen;\n    else\n        error_pos('End of file while expecting end of inStr');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct fileendian systemendian\n    id=strfind('iUIlLdD',inStr(pos));\n    if(isempty(id))\n        error_pos('expecting a number at position %d');\n    end\n    type={'int8','uint8','int16','int32','int64','single','double'};\n    bytelen=[1,1,2,4,8,4,8];\n    datastr=inStr(pos+1:pos+bytelen(id));\n    if(isoct)\n        newdata=int8(datastr);\n    else\n        newdata=uint8(datastr);\n    end\n    if(id<=5 && fileendian~=systemendian)\n        newdata=swapbytes(typecast(newdata,type{id}));\n    end\n    num=typecast(newdata,type{id});\n    pos = pos + bytelen(id)+1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n\n    switch(inStr(pos))\n        case {'S','C','H'}\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'i','U','I','l','L','d','D'}\n            val = parse_number(varargin{:});\n            return;\n        case 'T'\n            val = true;\n            pos = pos + 1;\n            return;\n        case 'F'\n            val = false;\n            pos = pos + 1;\n            return;\n        case {'Z','N'}\n            val = [];\n            pos = pos + 1;\n            return;\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos e1l e1r maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/mergestruct.m",
    "content": "function s=mergestruct(s1,s2)\n%\n% s=mergestruct(s1,s2)\n%\n% merge two struct objects into one\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      s1,s2: a struct object, s1 and s2 can not be arrays\n%\n% output:\n%      s: the merged struct object. fields in s1 and s2 will be combined in s.\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(~isstruct(s1) || ~isstruct(s2))\n    error('input parameters contain non-struct');\nend\nif(length(s1)>1 || length(s2)>1)\n    error('can not merge struct arrays');\nend\nfn=fieldnames(s2);\ns=s1;\nfor i=1:length(fn)              \n    s=setfield(s,fn{i},getfield(s2,fn{i}));\nend\n\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/savejson.m",
    "content": "function json=savejson(rootname,obj,varargin)\n%\n% json=savejson(rootname,obj,filename)\n%    or\n% json=savejson(rootname,obj,opt)\n% json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n% Object Notation) string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09\n%\n% $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array).\n%      filename: a string for the file name to save the output JSON data.\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n%                         of a 1D/2D array;\n%        opt.ArrayIndent [1|0]: if 1, output explicit data array with\n%                         precedent indentation; if 0, no indentation\n%        opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n%                         to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n%                         and $1 represents the sign. For those who want to use\n%                         1e999 to represent Inf, they can set opt.Inf to '$11e999'\n%        opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n%                         to represent NaN\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSONP='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%        opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n%        opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a string in the JSON format (see http://json.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      savejson('jmesh',jsonmesh)\n%      savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\n\nwhitespaces=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nif(jsonopt('Compact',0,opt)==1)\n    whitespaces=struct('tab','','newline','','sep',',');\nend\nif(~isfield(opt,'whitespaces_'))\n    opt.whitespaces_=whitespaces;\nend\n\nnl=whitespaces.newline;\n\njson=obj2json(rootname,obj,rootlevel,opt);\nif(rootisarray)\n    json=sprintf('%s%s',json,nl);\nelse\n    json=sprintf('{%s%s%s}\\n',nl,json,nl);\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=sprintf('%s(%s);%s',jsonp,json,nl);\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    if(jsonopt('SaveBinary',0,opt)==1)\n\t    fid = fopen(opt.FileName, 'wb');\n\t    fwrite(fid,json);\n    else\n\t    fid = fopen(opt.FileName, 'wt');\n\t    fwrite(fid,json,'char');\n    end\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2json(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2json(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2json(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2json(name,item,level,varargin{:});\nelse\n    txt=mat2json(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2json(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=jsonopt('whitespaces_',struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n')),varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nif(len>1)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": [%s',padding0, checkname(name,varargin{:}),nl); name=''; \n    else\n        txt=sprintf('%s[%s',padding0,nl); \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": []',padding0, checkname(name,varargin{:})); name=''; \n    else\n        txt=sprintf('%s[]',padding0); \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n    for i=1:dim(1)\n       txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:}));\n       if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    end\n    if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n    if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2json(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\npadding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1));\nnl=ws.newline;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding0,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding0,nl); end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n  for i=1:dim(1)\n    names = fieldnames(item(i,j));\n    if(~isempty(name) && len==1)\n        txt=sprintf('%s%s\"%s\": {%s',txt,padding1, checkname(name,varargin{:}),nl); \n    else\n        txt=sprintf('%s%s{%s',txt,padding1,nl); \n    end\n    if(~isempty(names))\n      for e=1:length(names)\n\t    txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:}));\n        if(e<length(names)) txt=sprintf('%s%s',txt,','); end\n        txt=sprintf('%s%s',txt,nl);\n      end\n    end\n    txt=sprintf('%s%s}',txt,padding1);\n    if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n  end\n  if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n  if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2json(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding1,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding1,nl); end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    if(isoct)\n        val=regexprep(item(e,:),'\\\\','\\\\');\n        val=regexprep(val,'\"','\\\"');\n        val=regexprep(val,'^\"','\\\"');\n    else\n        val=regexprep(item(e,:),'\\\\','\\\\\\\\');\n        val=regexprep(val,'\"','\\\\\"');\n        val=regexprep(val,'^\"','\\\\\"');\n    end\n    val=escapejsonstring(val);\n    if(len==1)\n        obj=['\"' checkname(name,varargin{:}) '\": ' '\"',val,'\"'];\n\tif(isempty(name)) obj=['\"',val,'\"']; end\n        txt=sprintf('%s%s%s%s',txt,padding1,obj);\n    else\n        txt=sprintf('%s%s%s%s',txt,padding0,['\"',val,'\"']);\n    end\n    if(e==len) sep=''; end\n    txt=sprintf('%s%s',txt,sep);\nend\nif(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2json(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:}))\n    if(isempty(name))\n    \ttxt=sprintf('%s{%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    else\n    \ttxt=sprintf('%s\"%s\": {%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    end\nelse\n    if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0)\n        numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\\[',''),']','');\n    else\n        numtxt=matdata2json(item,level+1,varargin{:});\n    end\n    if(isempty(name))\n    \ttxt=sprintf('%s%s',padding1,numtxt);\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n           \ttxt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        else\n    \t    txt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        end\n    end\n    return;\nend\ndataformat='%s%s%s%s%s';\n\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n    end\n    txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsSparse_\": ','1', sep);\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([iy(:),data'],level+2,varargin{:}), nl);\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,data],level+2,varargin{:}), nl);\n    else\n        % General case, store row and column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,iy,data],level+2,varargin{:}), nl);\n    end\nelse\n    if(isreal(item))\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json(item(:)',level+2,varargin{:}), nl);\n    else\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl);\n    end\nend\ntxt=sprintf('%s%s%s',txt,padding1,'}');\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2json(mat,level,varargin)\n\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\ntab=ws.tab;\nnl=ws.newline;\n\nif(size(mat,1)==1)\n    pre='';\n    post='';\n    level=level-1;\nelse\n    pre=sprintf('[%s',nl);\n    post=sprintf('%s%s]',nl,repmat(tab,1,level-1));\nend\n\nif(isempty(mat))\n    txt='null';\n    return;\nend\nfloatformat=jsonopt('FloatFormat','%.10g',varargin{:});\n%if(numel(mat)>1)\n    formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]];\n%else\n%    formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\\n')]];\n%end\n\nif(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1)\n    formatstr=[repmat(tab,1,level) formatstr];\nend\n\ntxt=sprintf(formatstr,mat');\ntxt(end-length(nl):end)=[];\nif(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1)\n   txt=regexprep(txt,'1','true');\n   txt=regexprep(txt,'0','false');\nend\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],\\n['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\ntxt=[pre txt post];\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n\n%%-------------------------------------------------------------------------\nfunction newstr=escapejsonstring(str)\nnewstr=str;\nisoct=exist('OCTAVE_VERSION','builtin');\nif(isoct)\n   vv=sscanf(OCTAVE_VERSION,'%f');\n   if(vv(1)>=3.8) isoct=0; end\nend\nif(isoct)\n  escapechars={'\\a','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},escapechars{i});\n  end\nelse\n  escapechars={'\\a','\\b','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\\\','\\\\\\\\'));\n  end\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/saveubjson.m",
    "content": "function json=saveubjson(rootname,obj,varargin)\n%\n% json=saveubjson(rootname,obj,filename)\n%    or\n% json=saveubjson(rootname,obj,opt)\n% json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a Universal \n% Binary JSON (UBJSON) binary string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/17\n%\n% $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array)\n%      filename: a string for the file name to save the output UBJSON data\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSON='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a binary string in the UBJSON format (see http://ubjson.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      saveubjson('jsonmesh',jsonmesh)\n%      saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\njson=obj2ubjson(rootname,obj,rootlevel,opt);\nif(~rootisarray)\n    json=['{' json '}'];\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=[jsonp '(' json ')'];\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    fid = fopen(opt.FileName, 'wb');\n    fwrite(fid,json);\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2ubjson(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2ubjson(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2ubjson(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2ubjson(name,item,level,varargin{:});\nelse\n    txt=mat2ubjson(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2ubjson(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item); % let's handle 1D cell first\nif(len>1) \n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) '[']; name=''; \n    else\n        txt='['; \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; \n    else\n        txt='Z'; \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=[txt '[']; end\n    for i=1:dim(1)\n       txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})];\n    end\n    if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2ubjson(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=[txt '[']; end\n  for i=1:dim(1)\n     names = fieldnames(item(i,j));\n     if(~isempty(name) && len==1)\n        txt=[txt S_(checkname(name,varargin{:})) '{']; \n     else\n        txt=[txt '{']; \n     end\n     if(~isempty(names))\n       for e=1:length(names)\n\t     txt=[txt obj2ubjson(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})];\n       end\n     end\n     txt=[txt '}'];\n  end\n  if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2ubjson(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    val=item(e,:);\n    if(len==1)\n        obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),''];\n\tif(isempty(name)) obj=['',S_(val),'']; end\n        txt=[txt,'',obj];\n    else\n        txt=[txt,'',['',S_(val),'']];\n    end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2ubjson(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) || jsonopt('ArrayToStruct',0,varargin{:}))\n      cid=I_(uint32(max(size(item))));\n      if(isempty(name))\n    \ttxt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ];\n      else\n          if(isempty(item))\n              txt=[S_(checkname(name,varargin{:})),'Z'];\n              return;\n          else\n    \t      txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))];\n          end\n      end\nelse\n    if(isempty(name))\n    \ttxt=matdata2ubjson(item,level+1,varargin{:});\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n            numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\\[',''),']','');\n           \ttxt=[S_(checkname(name,varargin{:})) numtxt];\n        else\n    \t    txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})];\n        end\n    end\n    return;\nend\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=[txt,S_('_ArrayIsComplex_'),'T'];\n    end\n    txt=[txt,S_('_ArrayIsSparse_'),'T'];\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([iy(:),data'],level+2,varargin{:})];\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,data],level+2,varargin{:})];\n    else\n        % General case, store row and column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,iy,data],level+2,varargin{:})];\n    end\nelse\n    if(isreal(item))\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson(item(:)',level+2,varargin{:})];\n    else\n        txt=[txt,S_('_ArrayIsComplex_'),'T'];\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})];\n    end\nend\ntxt=[txt,'}'];\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2ubjson(mat,level,varargin)\nif(isempty(mat))\n    txt='Z';\n    return;\nend\nif(size(mat,1)==1)\n    level=level-1;\nend\ntype='';\nhasnegtive=(mat<0);\nif(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0)))\n    if(isempty(hasnegtive))\n       if(max(mat(:))<=2^8)\n           type='U';\n       end\n    end\n    if(isempty(type))\n        % todo - need to consider negative ones separately\n        id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]);\n        if(isempty(find(id)))\n            error('high-precision data is not yet supported');\n        end\n        key='iIlL';\n\ttype=key(find(id));\n    end\n    txt=[I_a(mat(:),type,size(mat))];\nelseif(islogical(mat))\n    logicalval='FT';\n    if(numel(mat)==1)\n        txt=logicalval(mat+1);\n    else\n        txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')];\n    end\nelse\n    if(numel(mat)==1)\n        txt=['[' D_(mat) ']'];\n    else\n        txt=D_a(mat(:),'D',size(mat));\n    end\nend\n\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n%%-------------------------------------------------------------------------\nfunction val=S_(str)\nif(length(str)==1)\n  val=['C' str];\nelse\n  val=['S' I_(int32(length(str))) str];\nend\n%%-------------------------------------------------------------------------\nfunction val=I_(num)\nif(~isinteger(num))\n    error('input is not an integer');\nend\nif(num>=0 && num<255)\n   val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')];\n   return;\nend\nkey='iIlL';\ncid={'int8','int16','int32','int64'};\nfor i=1:4\n  if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1)))\n    val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')];\n    return;\n  end\nend\nerror('unsupported integer');\n\n%%-------------------------------------------------------------------------\nfunction val=D_(num)\nif(~isfloat(num))\n    error('input is not a float');\nend\n\nif(isa(num,'single'))\n  val=['d' data2byte(num,'uint8')];\nelse\n  val=['D' data2byte(num,'uint8')];\nend\n%%-------------------------------------------------------------------------\nfunction data=I_a(num,type,dim,format)\nid=find(ismember('iUIlL',type));\n\nif(id==0)\n  error('unsupported integer array');\nend\n\n% based on UBJSON specs, all integer types are stored in big endian format\n\nif(id==1)\n  data=data2byte(swapbytes(int8(num)),'uint8');\n  blen=1;\nelseif(id==2)\n  data=data2byte(swapbytes(uint8(num)),'uint8');\n  blen=1;\nelseif(id==3)\n  data=data2byte(swapbytes(int16(num)),'uint8');\n  blen=2;\nelseif(id==4)\n  data=data2byte(swapbytes(int32(num)),'uint8');\n  blen=4;\nelseif(id==5)\n  data=data2byte(swapbytes(int64(num)),'uint8');\n  blen=8;\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/blen)) data(:)'];\n  end\n  data=['[' data(:)'];\nelse\n  data=reshape(data,blen,numel(data)/blen);\n  data(2:blen+1,:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction data=D_a(num,type,dim,format)\nid=find(ismember('dD',type));\n\nif(id==0)\n  error('unsupported float array');\nend\n\nif(id==1)\n  data=data2byte(single(num),'uint8');\nelseif(id==2)\n  data=data2byte(double(num),'uint8');\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)'];\n  end\n  data=['[' data];\nelse\n  data=reshape(data,(id*4),length(data)/(id*4));\n  data(2:(id*4+1),:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction bytes=data2byte(varargin)\nbytes=typecast(varargin{:});\nbytes=bytes(:)';\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/jsonlab/varargin2struct.m",
    "content": "function opt=varargin2struct(varargin)\n%\n% opt=varargin2struct('param1',value1,'param2',value2,...)\n%   or\n% opt=varargin2struct(...,optstruct,...)\n%\n% convert a series of input parameters into a structure\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      'param', value: the input parameters should be pairs of a string and a value\n%       optstruct: if a parameter is a struct, the fields will be merged to the output struct\n%\n% output:\n%      opt: a struct where opt.param1=value1, opt.param2=value2 ...\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nlen=length(varargin);\nopt=struct;\nif(len==0) return; end\ni=1;\nwhile(i<=len)\n    if(isstruct(varargin{i}))\n        opt=mergestruct(opt,varargin{i});\n    elseif(ischar(varargin{i}) && i<len)\n        opt=setfield(opt,varargin{i},varargin{i+1});\n        i=i+1;\n    else\n        error('input must be in the form of ...,''name'',value,... pairs or structs');\n    end\n    i=i+1;\nend\n\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/makeValidFieldName.m",
    "content": "function str = makeValidFieldName(str)\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    isoct=exist('OCTAVE_VERSION','builtin');\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n"
  },
  {
    "path": "machine-learning-ex4/ex4/lib/submitWithConfiguration.m",
    "content": "function submitWithConfiguration(conf)\n  addpath('./lib/jsonlab');\n\n  parts = parts(conf);\n\n  fprintf('== Submitting solutions | %s...\\n', conf.itemName);\n\n  tokenFile = 'token.mat';\n  if exist(tokenFile, 'file')\n    load(tokenFile);\n    [email token] = promptToken(email, token, tokenFile);\n  else\n    [email token] = promptToken('', '', tokenFile);\n  end\n\n  if isempty(token)\n    fprintf('!! Submission Cancelled\\n');\n    return\n  end\n\n  try\n    response = submitParts(conf, email, token, parts);\n  catch\n    e = lasterror();\n    fprintf( ...\n      '!! Submission failed: unexpected error: %s\\n', ...\n      e.message);\n    fprintf('!! Please try again later.\\n');\n    return\n  end\n\n  if isfield(response, 'errorMessage')\n    fprintf('!! Submission failed: %s\\n', response.errorMessage);\n  else\n    showFeedback(parts, response);\n    save(tokenFile, 'email', 'token');\n  end\nend\n\nfunction [email token] = promptToken(email, existingToken, tokenFile)\n  if (~isempty(email) && ~isempty(existingToken))\n    prompt = sprintf( ...\n      'Use token from last successful submission (%s)? (Y/n): ', ...\n      email);\n    reenter = input(prompt, 's');\n\n    if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y')\n      token = existingToken;\n      return;\n    else\n      delete(tokenFile);\n    end\n  end\n  email = input('Login (email address): ', 's');\n  token = input('Token: ', 's');\nend\n\nfunction isValid = isValidPartOptionIndex(partOptions, i)\n  isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions));\nend\n\nfunction response = submitParts(conf, email, token, parts)\n  body = makePostBody(conf, email, token, parts);\n  submissionUrl = submissionUrl();\n  params = {'jsonBody', body};\n  responseBody = urlread(submissionUrl, 'post', params);\n  response = loadjson(responseBody);\nend\n\nfunction body = makePostBody(conf, email, token, parts)\n  bodyStruct.assignmentSlug = conf.assignmentSlug;\n  bodyStruct.submitterEmail = email;\n  bodyStruct.secret = token;\n  bodyStruct.parts = makePartsStruct(conf, parts);\n\n  opt.Compact = 1;\n  body = savejson('', bodyStruct, opt);\nend\n\nfunction partsStruct = makePartsStruct(conf, parts)\n  for part = parts\n    partId = part{:}.id;\n    fieldName = makeValidFieldName(partId);\n    outputStruct.output = conf.output(partId);\n    partsStruct.(fieldName) = outputStruct;\n  end\nend\n\nfunction [parts] = parts(conf)\n  parts = {};\n  for partArray = conf.partArrays\n    part.id = partArray{:}{1};\n    part.sourceFiles = partArray{:}{2};\n    part.name = partArray{:}{3};\n    parts{end + 1} = part;\n  end\nend\n\nfunction showFeedback(parts, response)\n  fprintf('== \\n');\n  fprintf('== %43s | %9s | %-s\\n', 'Part Name', 'Score', 'Feedback');\n  fprintf('== %43s | %9s | %-s\\n', '---------', '-----', '--------');\n  for part = parts\n    score = '';\n    partFeedback = '';\n    partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id));\n    partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id));\n    score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore);\n    fprintf('== %43s | %9s | %-s\\n', part{:}.name, score, partFeedback);\n  end\n  evaluation = response.evaluation;\n  totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore);\n  fprintf('==                                   --------------------------------\\n');\n  fprintf('== %43s | %9s | %-s\\n', '', totalScore, '');\n  fprintf('== \\n');\nend\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%\n% Service configuration\n%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\nfunction submissionUrl = submissionUrl()\n  submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1';\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/nnCostFunction.m",
    "content": "function [J grad] = nnCostFunction(nn_params, ...\n                                   input_layer_size, ...\n                                   hidden_layer_size, ...\n                                   num_labels, ...\n                                   X, y, lambda)\n%NNCOSTFUNCTION Implements the neural network cost function for a two layer\n%neural network which performs classification\n%   [J grad] = NNCOSTFUNCTON(nn_params, hidden_layer_size, num_labels, ...\n%   X, y, lambda) computes the cost and gradient of the neural network. The\n%   parameters for the neural network are \"unrolled\" into the vector\n%   nn_params and need to be converted back into the weight matrices. \n% \n%   The returned parameter grad should be a \"unrolled\" vector of the\n%   partial derivatives of the neural network.\n%\n\n% Reshape nn_params back into the parameters Theta1 and Theta2, the weight matrices\n% for our 2 layer neural network\nTheta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...\n                 hidden_layer_size, (input_layer_size + 1));    %ԭʼTheta1\n\nTheta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...\n                 num_labels, (hidden_layer_size + 1));\n\n% Setup some useful variables\nm = size(X, 1); %\n         \n% You need to return the following variables correctly \nJ = 0;\nTheta1_grad = zeros(size(Theta1));\nTheta2_grad = zeros(size(Theta2));\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: You should complete the code by working through the\n%               following parts.\n%\n% Part 1: Feedforward the neural network and return the cost in the\n%         variable J. After implementing Part 1, you can verify that your\n%         cost function computation is correct by verifying the cost\n%         computed in ex4.m\n%\n% Part 2: Implement the backpropagation algorithm to compute the gradients\n%         Theta1_grad and Theta2_grad. You should return the partial derivatives of\n%         the cost function with respect to Theta1 and Theta2 in Theta1_grad and\n%         Theta2_grad, respectively. After implementing Part 2, you can check\n%         that your implementation is correct by running checkNNGradients\n%\n%         Note: The vector y passed into the function is a vector of labels\n%               containing values from 1..K. You need to map this vector into a \n%               binary vector of 1's and 0's to be used with the neural network\n%               cost function.\n%\n%         Hint: We recommend implementing backpropagation using a for-loop\n%               over the training examples if you are implementing it for the \n%               first time.\n%\n% Part 3: Implement regularization with the cost function and gradients.\n%\n%         Hint: You can implement this around the code for\n%               backpropagation. That is, you can compute the gradients for\n%               the regularization separately and then add them to Theta1_grad\n%               and Theta2_grad from Part 2.\n%\n\nTheta1_x = Theta1(:,(2:end));   %ȥtheta1(0)\nTheta2_x = Theta2(:,(2:end));   %ȥtheta2(0)\nregterm = [Theta1_x(:);Theta2_x(:)]'*[Theta1_x(:);Theta2_x(:)]; %thetaƽ\n\nclass_y = zeros(m,num_labels);  %ӳΪ0,1ҪJʹ\nfor i = 1:num_labels\n    class_y(:,i) = y==i;\nend\n\n%  򴫲\na1 = [ones(m,1),X];\nz2 = a1*Theta1';\na2 = sigmoid(z2);\na2 = [ones(m,1),a2];\nz3 = a2*Theta2';\nh = sigmoid(z3);\n\n%  ۺ\nJ = -((class_y(:)'*(log(h(:)))) + ((1-class_y(:))'*(log(1-h(:))))-(lambda*regterm/2))/m;\n\n%   򴫲ݶ\nfor i = 1:m\n    delta3(i,:) = h(i,:)-class_y(i,:);  %\n    Theta2_grad = Theta2_grad+delta3(i,:)'*a2(i,:); %ǰһ㣬\n    delta2(i,:) = (delta3(i,:)*Theta2_x).*sigmoidGradient(z2(i,:));\n    Theta1_grad = Theta1_grad+delta2(i,:)'*a1(i,:);\nend\n\n\n\n\n    \nTheta1(:,1) = 0;\nTheta2(:,1) = 0;\n\n% -------------------------------------------------------------\n\n% =========================================================================\n\n% Unroll gradients\ngrad = ([Theta1_grad(:);Theta2_grad(:)]+lambda*[Theta1(:);Theta2(:)])/m;\n\n\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/predict.m",
    "content": "function p = predict(Theta1, Theta2, X)\n%PREDICT Predict the label of an input given a trained neural network\n%   p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the\n%   trained weights of a neural network (Theta1, Theta2)\n\n% Useful values\nm = size(X, 1);\nnum_labels = size(Theta2, 1);\n\n% You need to return the following variables correctly \np = zeros(size(X, 1), 1);\n\nh1 = sigmoid([ones(m, 1) X] * Theta1');\nh2 = sigmoid([ones(m, 1) h1] * Theta2');\n[dummy, p] = max(h2, [], 2);\n\n% =========================================================================\n\n\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/randInitializeWeights.m",
    "content": "function W = randInitializeWeights(L_in, L_out)\n%RANDINITIALIZEWEIGHTS Randomly initialize the weights of a layer with L_in\n%incoming connections and L_out outgoing connections\n%   W = RANDINITIALIZEWEIGHTS(L_in, L_out) randomly initializes the weights \n%   of a layer with L_in incoming connections and L_out outgoing \n%   connections. \n%\n%   Note that W should be set to a matrix of size(L_out, 1 + L_in) as\n%   the column row of W handles the \"bias\" terms\n%\n\n% You need to return the following variables correctly \nW = zeros(L_out, 1 + L_in);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Initialize W randomly so that we break the symmetry while\n%               training the neural network.\n%\n% Note: The first row of W corresponds to the parameters for the bias units\n%\n\nepsilon_init = (6/(L_in))^0.5;\n\nW = rand(L_out,1+L_in)*2*epsilon_init-epsilon_init;\n\n\n\n\n\n\n\n% =========================================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/sigmoid.m",
    "content": "function g = sigmoid(z)\n%SIGMOID Compute sigmoid functoon\n%   J = SIGMOID(z) computes the sigmoid of z.\n\ng = 1.0 ./ (1.0 + exp(-z));\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/sigmoidGradient.m",
    "content": "function g = sigmoidGradient(z)\n%SIGMOIDGRADIENT returns the gradient of the sigmoid function\n%evaluated at z\n%   g = SIGMOIDGRADIENT(z) computes the gradient of the sigmoid function\n%   evaluated at z. This should work regardless if z is a matrix or a\n%   vector. In particular, if z is a vector or matrix, you should return\n%   the gradient for each element.\n\ng = zeros(size(z));\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the gradient of the sigmoid function evaluated at\n%               each value of z (z can be a matrix, vector or scalar).\n\n\n\n\ng = sigmoid(z).*(1-sigmoid(z));\n\n\n\n\n\n\n\n\n\n% =============================================================\n\n\n\n\nend\n"
  },
  {
    "path": "machine-learning-ex4/ex4/submit.m",
    "content": "function submit()\n  addpath('./lib');\n\n  conf.assignmentSlug = 'neural-network-learning';\n  conf.itemName = 'Neural Networks Learning';\n  conf.partArrays = { ...\n    { ...\n      '1', ...\n      { 'nnCostFunction.m' }, ...\n      'Feedforward and Cost Function', ...\n    }, ...\n    { ...\n      '2', ...\n      { 'nnCostFunction.m' }, ...\n      'Regularized Cost Function', ...\n    }, ...\n    { ...\n      '3', ...\n      { 'sigmoidGradient.m' }, ...\n      'Sigmoid Gradient', ...\n    }, ...\n    { ...\n      '4', ...\n      { 'nnCostFunction.m' }, ...\n      'Neural Network Gradient (Backpropagation)', ...\n    }, ...\n    { ...\n      '5', ...\n      { 'nnCostFunction.m' }, ...\n      'Regularized Gradient', ...\n    }, ...\n  };\n  conf.output = @output;\n\n  submitWithConfiguration(conf);\nend\n\nfunction out = output(partId, auxstring)\n  % Random Test Cases\n  X = reshape(3 * sin(1:1:30), 3, 10);\n  Xm = reshape(sin(1:32), 16, 2) / 5;\n  ym = 1 + mod(1:16,4)';\n  t1 = sin(reshape(1:2:24, 4, 3));\n  t2 = cos(reshape(1:2:40, 4, 5));\n  t  = [t1(:) ; t2(:)];\n  if partId == '1'\n    [J] = nnCostFunction(t, 2, 4, 4, Xm, ym, 0);\n    out = sprintf('%0.5f ', J);\n  elseif partId == '2'\n    [J] = nnCostFunction(t, 2, 4, 4, Xm, ym, 1.5);\n    out = sprintf('%0.5f ', J);\n  elseif partId == '3'\n    out = sprintf('%0.5f ', sigmoidGradient(X));\n  elseif partId == '4'\n    [J, grad] = nnCostFunction(t, 2, 4, 4, Xm, ym, 0);\n    out = sprintf('%0.5f ', J);\n    out = [out sprintf('%0.5f ', grad)];\n  elseif partId == '5'\n    [J, grad] = nnCostFunction(t, 2, 4, 4, Xm, ym, 1.5);\n    out = sprintf('%0.5f ', J);\n    out = [out sprintf('%0.5f ', grad)];\n  end \nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/ex5.m",
    "content": "%% Machine Learning Online Class\n%  Exercise 5 | Regularized Linear Regression and Bias-Variance\n%\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the\n%  exercise. You will need to complete the following functions:\n%\n%     linearRegCostFunction.m\n%     learningCurve.m\n%     validationCurve.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% =========== Part 1: Loading and Visualizing Data =============\n%  We start the exercise by first loading and visualizing the dataset. \n%  The following code will load the dataset into your environment and plot\n%  the data.\n%\n\n% Load Training Data\nfprintf('Loading and Visualizing Data ...\\n')\n\n% Load from ex5data1: \n% You will have X, y, Xval, yval, Xtest, ytest in your environment\nload ('ex5data1.mat');\n\n% m = Number of examples\nm = size(X, 1);\n\n% Plot training data\nplot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);\nxlabel('Change in water level (x)');\nylabel('Water flowing out of the dam (y)');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =========== Part 2: Regularized Linear Regression Cost =============\n%  You should now implement the cost function for regularized linear \n%  regression. \n%\n\ntheta = [1 ; 1];\nJ = linearRegCostFunction([ones(m, 1) X], y, theta, 1);\n\nfprintf(['Cost at theta = [1 ; 1]: %f '...\n         '\\n(this value should be about 303.993192)\\n'], J);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =========== Part 3: Regularized Linear Regression Gradient =============\n%  You should now implement the gradient for regularized linear \n%  regression.\n%\n\ntheta = [1 ; 1];\n[J, grad] = linearRegCostFunction([ones(m, 1) X], y, theta, 1);\n\nfprintf(['Gradient at theta = [1 ; 1]:  [%f; %f] '...\n         '\\n(this value should be about [-15.303016; 598.250744])\\n'], ...\n         grad(1), grad(2));\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% =========== Part 4: Train Linear Regression =============\n%  Once you have implemented the cost and gradient correctly, the\n%  trainLinearReg function will use your cost function to train \n%  regularized linear regression.\n% \n%  Write Up Note: The data is non-linear, so this will not give a great \n%                 fit.\n%\n\n%  Train linear regression with lambda = 0\nlambda = 0;\n[theta] = trainLinearReg([ones(m, 1) X], y, lambda);\n\n%  Plot fit over the data\nplot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);\nxlabel('Change in water level (x)');\nylabel('Water flowing out of the dam (y)');\nhold on;\nplot(X, [ones(m, 1) X]*theta, '--', 'LineWidth', 2)\nhold off;\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% =========== Part 5: Learning Curve for Linear Regression =============\n%  Next, you should implement the learningCurve function. \n%\n%  Write Up Note: Since the model is underfitting the data, we expect to\n%                 see a graph with \"high bias\" -- slide 8 in ML-advice.pdf \n%\n\nlambda = 0;\n[error_train, error_val] = ...\n    learningCurve([ones(m, 1) X], y, ...\n                  [ones(size(Xval, 1), 1) Xval], yval, ...\n                  lambda);\n\nplot(1:m, error_train, 1:m, error_val);\ntitle('Learning curve for linear regression')\nlegend('Train', 'Cross Validation')\nxlabel('Number of training examples')\nylabel('Error')\naxis([0 13 0 150])\n\nfprintf('# Training Examples\\tTrain Error\\tCross Validation Error\\n');\nfor i = 1:m\n    fprintf('  \\t%d\\t\\t%f\\t%f\\n', i, error_train(i), error_val(i));\nend\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =========== Part 6: Feature Mapping for Polynomial Regression =============\n%  One solution to this is to use polynomial regression. You should now\n%  complete polyFeatures to map each example into its powers\n%\n\np = 8;\n\n% Map X onto Polynomial Features and Normalize\nX_poly = polyFeatures(X, p);\n[X_poly, mu, sigma] = featureNormalize(X_poly);  % Normalize\nX_poly = [ones(m, 1), X_poly];                   % Add Ones\n\n% Map X_poly_test and normalize (using mu and sigma)\nX_poly_test = polyFeatures(Xtest, p);\nX_poly_test = bsxfun(@minus, X_poly_test, mu);\nX_poly_test = bsxfun(@rdivide, X_poly_test, sigma);\nX_poly_test = [ones(size(X_poly_test, 1), 1), X_poly_test];         % Add Ones\n\n% Map X_poly_val and normalize (using mu and sigma)\nX_poly_val = polyFeatures(Xval, p);\nX_poly_val = bsxfun(@minus, X_poly_val, mu);\nX_poly_val = bsxfun(@rdivide, X_poly_val, sigma);\nX_poly_val = [ones(size(X_poly_val, 1), 1), X_poly_val];           % Add Ones\n\nfprintf('Normalized Training Example 1:\\n');\nfprintf('  %f  \\n', X_poly(1, :));\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n\n\n%% =========== Part 7: Learning Curve for Polynomial Regression =============\n%  Now, you will get to experiment with polynomial regression with multiple\n%  values of lambda. The code below runs polynomial regression with \n%  lambda = 0. You should try running the code with different values of\n%  lambda to see how the fit and learning curve change.\n%\n\nlambda = 0;\n[theta] = trainLinearReg(X_poly, y, lambda);\n\n% Plot training data and fit\nfigure(1);\nplot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);\nplotFit(min(X), max(X), mu, sigma, theta, p);\nxlabel('Change in water level (x)');\nylabel('Water flowing out of the dam (y)');\ntitle (sprintf('Polynomial Regression Fit (lambda = %f)', lambda));\n\nfigure(2);\n[error_train, error_val] = ...\n    learningCurve(X_poly, y, X_poly_val, yval, lambda);\nplot(1:m, error_train, 1:m, error_val);\n\ntitle(sprintf('Polynomial Regression Learning Curve (lambda = %f)', lambda));\nxlabel('Number of training examples')\nylabel('Error')\naxis([0 13 0 100])\nlegend('Train', 'Cross Validation')\n\nfprintf('Polynomial Regression (lambda = %f)\\n\\n', lambda);\nfprintf('# Training Examples\\tTrain Error\\tCross Validation Error\\n');\nfor i = 1:m\n    fprintf('  \\t%d\\t\\t%f\\t%f\\n', i, error_train(i), error_val(i));\nend\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =========== Part 8: Validation for Selecting Lambda =============\n%  You will now implement validationCurve to test various values of \n%  lambda on a validation set. You will then use this to select the\n%  \"best\" lambda value.\n%\n\n[lambda_vec, error_train, error_val] = ...\n    validationCurve(X_poly, y, X_poly_val, yval);\n\nclose all;\nplot(lambda_vec, error_train, lambda_vec, error_val);\nlegend('Train', 'Cross Validation');\nxlabel('lambda');\nylabel('Error');\n\nfprintf('lambda\\t\\tTrain Error\\tValidation Error\\n');\nfor i = 1:length(lambda_vec)\n\tfprintf(' %f\\t%f\\t%f\\n', ...\n            lambda_vec(i), error_train(i), error_val(i));\nend\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n"
  },
  {
    "path": "machine-learning-ex5/ex5/featureNormalize.m",
    "content": "function [X_norm, mu, sigma] = featureNormalize(X)\n%FEATURENORMALIZE Normalizes the features in X \n%   FEATURENORMALIZE(X) returns a normalized version of X where\n%   the mean value of each feature is 0 and the standard deviation\n%   is 1. This is often a good preprocessing step to do when\n%   working with learning algorithms.\n\nmu = mean(X);\nX_norm = bsxfun(@minus, X, mu);\n\nsigma = std(X_norm);\nX_norm = bsxfun(@rdivide, X_norm, sigma);\n\n\n% ============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/fmincg.m",
    "content": "function [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)\n% Minimize a continuous differentialble multivariate function. Starting point\n% is given by \"X\" (D by 1), and the function named in the string \"f\", must\n% return a function value and a vector of partial derivatives. The Polack-\n% Ribiere flavour of conjugate gradients is used to compute search directions,\n% and a line search using quadratic and cubic polynomial approximations and the\n% Wolfe-Powell stopping criteria is used together with the slope ratio method\n% for guessing initial step sizes. Additionally a bunch of checks are made to\n% make sure that exploration is taking place and that extrapolation will not\n% be unboundedly large. The \"length\" gives the length of the run: if it is\n% positive, it gives the maximum number of line searches, if negative its\n% absolute gives the maximum allowed number of function evaluations. You can\n% (optionally) give \"length\" a second component, which will indicate the\n% reduction in function value to be expected in the first line-search (defaults\n% to 1.0). The function returns when either its length is up, or if no further\n% progress can be made (ie, we are at a minimum, or so close that due to\n% numerical problems, we cannot get any closer). If the function terminates\n% within a few iterations, it could be an indication that the function value\n% and derivatives are not consistent (ie, there may be a bug in the\n% implementation of your \"f\" function). The function returns the found\n% solution \"X\", a vector of function values \"fX\" indicating the progress made\n% and \"i\" the number of iterations (line searches or function evaluations,\n% depending on the sign of \"length\") used.\n%\n% Usage: [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)\n%\n% See also: checkgrad \n%\n% Copyright (C) 2001 and 2002 by Carl Edward Rasmussen. Date 2002-02-13\n%\n%\n% (C) Copyright 1999, 2000 & 2001, Carl Edward Rasmussen\n% \n% Permission is granted for anyone to copy, use, or modify these\n% programs and accompanying documents for purposes of research or\n% education, provided this copyright notice is retained, and note is\n% made of any changes that have been made.\n% \n% These programs and documents are distributed without any warranty,\n% express or implied.  As the programs were written for research\n% purposes only, they have not been tested to the degree that would be\n% advisable in any important application.  All use of these programs is\n% entirely at the user's own risk.\n%\n% [ml-class] Changes Made:\n% 1) Function name and argument specifications\n% 2) Output display\n%\n\n% Read options\nif exist('options', 'var') && ~isempty(options) && isfield(options, 'MaxIter')\n    length = options.MaxIter;\nelse\n    length = 100;\nend\n\n\nRHO = 0.01;                            % a bunch of constants for line searches\nSIG = 0.5;       % RHO and SIG are the constants in the Wolfe-Powell conditions\nINT = 0.1;    % don't reevaluate within 0.1 of the limit of the current bracket\nEXT = 3.0;                    % extrapolate maximum 3 times the current bracket\nMAX = 20;                         % max 20 function evaluations per line search\nRATIO = 100;                                      % maximum allowed slope ratio\n\nargstr = ['feval(f, X'];                      % compose string used to call function\nfor i = 1:(nargin - 3)\n  argstr = [argstr, ',P', int2str(i)];\nend\nargstr = [argstr, ')'];\n\nif max(size(length)) == 2, red=length(2); length=length(1); else red=1; end\nS=['Iteration '];\n\ni = 0;                                            % zero the run length counter\nls_failed = 0;                             % no previous line search has failed\nfX = [];\n[f1 df1] = eval(argstr);                      % get function value and gradient\ni = i + (length<0);                                            % count epochs?!\ns = -df1;                                        % search direction is steepest\nd1 = -s'*s;                                                 % this is the slope\nz1 = red/(1-d1);                                  % initial step is red/(|s|+1)\n\nwhile i < abs(length)                                      % while not finished\n  i = i + (length>0);                                      % count iterations?!\n\n  X0 = X; f0 = f1; df0 = df1;                   % make a copy of current values\n  X = X + z1*s;                                             % begin line search\n  [f2 df2] = eval(argstr);\n  i = i + (length<0);                                          % count epochs?!\n  d2 = df2'*s;\n  f3 = f1; d3 = d1; z3 = -z1;             % initialize point 3 equal to point 1\n  if length>0, M = MAX; else M = min(MAX, -length-i); end\n  success = 0; limit = -1;                     % initialize quanteties\n  while 1\n    while ((f2 > f1+z1*RHO*d1) || (d2 > -SIG*d1)) && (M > 0) \n      limit = z1;                                         % tighten the bracket\n      if f2 > f1\n        z2 = z3 - (0.5*d3*z3*z3)/(d3*z3+f2-f3);                 % quadratic fit\n      else\n        A = 6*(f2-f3)/z3+3*(d2+d3);                                 % cubic fit\n        B = 3*(f3-f2)-z3*(d3+2*d2);\n        z2 = (sqrt(B*B-A*d2*z3*z3)-B)/A;       % numerical error possible - ok!\n      end\n      if isnan(z2) || isinf(z2)\n        z2 = z3/2;                  % if we had a numerical problem then bisect\n      end\n      z2 = max(min(z2, INT*z3),(1-INT)*z3);  % don't accept too close to limits\n      z1 = z1 + z2;                                           % update the step\n      X = X + z2*s;\n      [f2 df2] = eval(argstr);\n      M = M - 1; i = i + (length<0);                           % count epochs?!\n      d2 = df2'*s;\n      z3 = z3-z2;                    % z3 is now relative to the location of z2\n    end\n    if f2 > f1+z1*RHO*d1 || d2 > -SIG*d1\n      break;                                                % this is a failure\n    elseif d2 > SIG*d1\n      success = 1; break;                                             % success\n    elseif M == 0\n      break;                                                          % failure\n    end\n    A = 6*(f2-f3)/z3+3*(d2+d3);                      % make cubic extrapolation\n    B = 3*(f3-f2)-z3*(d3+2*d2);\n    z2 = -d2*z3*z3/(B+sqrt(B*B-A*d2*z3*z3));        % num. error possible - ok!\n    if ~isreal(z2) || isnan(z2) || isinf(z2) || z2 < 0 % num prob or wrong sign?\n      if limit < -0.5                               % if we have no upper limit\n        z2 = z1 * (EXT-1);                 % the extrapolate the maximum amount\n      else\n        z2 = (limit-z1)/2;                                   % otherwise bisect\n      end\n    elseif (limit > -0.5) && (z2+z1 > limit)         % extraplation beyond max?\n      z2 = (limit-z1)/2;                                               % bisect\n    elseif (limit < -0.5) && (z2+z1 > z1*EXT)       % extrapolation beyond limit\n      z2 = z1*(EXT-1.0);                           % set to extrapolation limit\n    elseif z2 < -z3*INT\n      z2 = -z3*INT;\n    elseif (limit > -0.5) && (z2 < (limit-z1)*(1.0-INT))  % too close to limit?\n      z2 = (limit-z1)*(1.0-INT);\n    end\n    f3 = f2; d3 = d2; z3 = -z2;                  % set point 3 equal to point 2\n    z1 = z1 + z2; X = X + z2*s;                      % update current estimates\n    [f2 df2] = eval(argstr);\n    M = M - 1; i = i + (length<0);                             % count epochs?!\n    d2 = df2'*s;\n  end                                                      % end of line search\n\n  if success                                         % if line search succeeded\n    f1 = f2; fX = [fX' f1]';\n    fprintf('%s %4i | Cost: %4.6e\\r', S, i, f1);\n    s = (df2'*df2-df1'*df2)/(df1'*df1)*s - df2;      % Polack-Ribiere direction\n    tmp = df1; df1 = df2; df2 = tmp;                         % swap derivatives\n    d2 = df1'*s;\n    if d2 > 0                                      % new slope must be negative\n      s = -df1;                              % otherwise use steepest direction\n      d2 = -s'*s;    \n    end\n    z1 = z1 * min(RATIO, d1/(d2-realmin));          % slope ratio but max RATIO\n    d1 = d2;\n    ls_failed = 0;                              % this line search did not fail\n  else\n    X = X0; f1 = f0; df1 = df0;  % restore point from before failed line search\n    if ls_failed || i > abs(length)          % line search failed twice in a row\n      break;                             % or we ran out of time, so we give up\n    end\n    tmp = df1; df1 = df2; df2 = tmp;                         % swap derivatives\n    s = -df1;                                                    % try steepest\n    d1 = -s'*s;\n    z1 = 1/(1-d1);                     \n    ls_failed = 1;                                    % this line search failed\n  end\n  if exist('OCTAVE_VERSION')\n    fflush(stdout);\n  end\nend\nfprintf('\\n');\n"
  },
  {
    "path": "machine-learning-ex5/ex5/learningCurve.m",
    "content": "function [error_train, error_val] = ...\n    learningCurve(X, y, Xval, yval, lambda)\n%LEARNINGCURVE Generates the train and cross validation set errors needed \n%to plot a learning curve\n%   [error_train, error_val] = ...\n%       LEARNINGCURVE(X, y, Xval, yval, lambda) returns the train and\n%       cross validation set errors for a learning curve. In particular, \n%       it returns two vectors of the same length - error_train and \n%       error_val. Then, error_train(i) contains the training error for\n%       i examples (and similarly for error_val(i)).\n%\n%   In this function, you will compute the train and test errors for\n%   dataset sizes from 1 up to m. In practice, when working with larger\n%   datasets, you might want to do this in larger intervals.\n%\n\n% Number of training examples\nm = size(X, 1);\n\n% You need to return these values correctly\nerror_train = zeros(m, 1);\nerror_val   = zeros(m, 1);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Fill in this function to return training errors in \n%               error_train and the cross validation errors in error_val. \n%               i.e., error_train(i) and \n%               error_val(i) should give you the errors\n%               obtained after training on i examples.\n%\n% Note: You should evaluate the training error on the first i training\n%       examples (i.e., X(1:i, :) and y(1:i)).\n%\n%       For the cross-validation error, you should instead evaluate on\n%       the _entire_ cross validation set (Xval and yval).\n%\n% Note: If you are using your cost function (linearRegCostFunction)\n%       to compute the training and cross validation error, you should \n%       call the function with the lambda argument set to 0. \n%       Do note that you will still need to use lambda when running\n%       the training to obtain the theta parameters.\n%\n% Hint: You can loop over the examples with the following:\n%\n%       for i = 1:m\n%           % Compute train/cross validation errors using training examples \n%           % X(1:i, :) and y(1:i), storing the result in \n%           % error_train(i) and error_val(i)\n%           ....\n%           \n%       end\n%\n\n% ---------------------- Sample Solution ----------------------\n\nfor i = 1:m\n    [theta] = trainLinearReg(X(1:i,:),y(1:i),lambda);\n    h_train(1:i,i) = X(1:i,:)*theta;\n    h_val(:,i) = Xval*theta;\n    error_train(i) = (((h_train(1:i,i)-y(1:i))'*(h_train(1:i,i)-y(1:i)))/(2*i));\n    error_val(i) = (((h_val(:,i)-yval)'*(h_val(:,i)-yval))/(2*length(yval)));\nend\n\n\n\n\n% -------------------------------------------------------------\n\n% =========================================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/AUTHORS.txt",
    "content": "The author of \"jsonlab\" toolbox is Qianqian Fang. Qianqian\nis currently an Assistant Professor at Massachusetts General Hospital, \nHarvard Medical School.\n\nAddress: Martinos Center for Biomedical Imaging, \n         Massachusetts General Hospital, \n         Harvard Medical School\n         Bldg 149, 13th St, Charlestown, MA 02129, USA\nURL: http://nmr.mgh.harvard.edu/~fangq/\nEmail: <fangq at nmr.mgh.harvard.edu> or <fangqq at gmail.com>\n\n\nThe script loadjson.m was built upon previous works by\n\n- Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n       date: 2009/11/02\n- François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n       date: 2009/03/22\n- Joel Feenstra: http://www.mathworks.com/matlabcentral/fileexchange/20565\n       date: 2008/07/03\n\n\nThis toolbox contains patches submitted by the following contributors:\n\n- Blake Johnson <bjohnso at bbn.com>\n  part of revision 341\n\n- Niclas Borlin <Niclas.Borlin at cs.umu.se>\n  various fixes in revision 394, including\n  - loadjson crashes for all-zero sparse matrix.\n  - loadjson crashes for empty sparse matrix.\n  - Non-zero size of 0-by-N and N-by-0 empty matrices is lost after savejson/loadjson.\n  - loadjson crashes for sparse real column vector.\n  - loadjson crashes for sparse complex column vector.\n  - Data is corrupted by savejson for sparse real row vector.\n  - savejson crashes for sparse complex row vector. \n\n- Yul Kang <yul.kang.on at gmail.com>\n  patches for svn revision 415.\n  - savejson saves an empty cell array as [] instead of null\n  - loadjson differentiates an empty struct from an empty array\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/ChangeLog.txt",
    "content": "============================================================================\n\n   JSONlab - a toolbox to encode/decode JSON/UBJSON files in MATLAB/Octave\n\n----------------------------------------------------------------------------\n\nJSONlab ChangeLog (key features marked by *):\n\n== JSONlab 1.0 (codename: Optimus - Final), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2015/01/02 polish help info for all major functions, update examples, finalize 1.0\n 2014/12/19 fix a bug to strictly respect NoRowBracket in savejson\n\n== JSONlab 1.0.0-RC2 (codename: Optimus - RC2), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/11/22 show progress bar in loadjson ('ShowProgress') \n 2014/11/17 add Compact option in savejson to output compact JSON format ('Compact')\n 2014/11/17 add FastArrayParser in loadjson to specify fast parser applicable levels\n 2014/09/18 start official github mirror: https://github.com/fangq/jsonlab\n\n== JSONlab 1.0.0-RC1 (codename: Optimus - RC1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/09/17  fix several compatibility issues when running on octave versions 3.2-3.8\n 2014/09/17  support 2D cell and struct arrays in both savejson and saveubjson\n 2014/08/04  escape special characters in a JSON string\n 2014/02/16  fix a bug when saving ubjson files\n\n== JSONlab 0.9.9 (codename: Optimus - beta), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/01/22  use binary read and write in saveubjson and loadubjson\n\n== JSONlab 0.9.8-1 (codename: Optimus - alpha update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2013/10/07 better round-trip conservation for empty arrays and structs (patch submitted by Yul Kang)\n\n== JSONlab 0.9.8 (codename: Optimus - alpha), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2013/08/23 *universal Binary JSON (UBJSON) support, including both saveubjson and loadubjson\n\n== JSONlab 0.9.1 (codename: Rodimus, update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2012/12/18 *handling of various empty and sparse matrices (fixes submitted by Niclas Borlin)\n\n== JSONlab 0.9.0 (codename: Rodimus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/06/17 *new format for an invalid leading char, unpacking hex code in savejson\n 2012/06/01  support JSONP in savejson\n 2012/05/25  fix the empty cell bug (reported by Cyril Davin)\n 2012/04/05  savejson can save to a file (suggested by Patrick Rapin)\n\n== JSONlab 0.8.1 (codename: Sentiel, Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/02/28  loadjson quotation mark escape bug, see http://bit.ly/yyk1nS\n 2012/01/25  patch to handle root-less objects, contributed by Blake Johnson\n\n== JSONlab 0.8.0 (codename: Sentiel), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/01/13 *speed up loadjson by 20 fold when parsing large data arrays in matlab\n 2012/01/11  remove row bracket if an array has 1 element, suggested by Mykel Kochenderfer\n 2011/12/22 *accept sequence of 'param',value input in savejson and loadjson\n 2011/11/18  fix struct array bug reported by Mykel Kochenderfer\n\n== JSONlab 0.5.1 (codename: Nexus Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/21  fix a bug in loadjson, previous code does not use any of the acceleration\n 2011/10/20  loadjson supports JSON collections - concatenated JSON objects\n\n== JSONlab 0.5.0 (codename: Nexus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/16  package and release jsonlab 0.5.0\n 2011/10/15 *add json demo and regression test, support cpx numbers, fix double quote bug\n 2011/10/11 *speed up readjson dramatically, interpret _Array* tags, show data in root level\n 2011/10/10  create jsonlab project, start jsonlab website, add online documentation\n 2011/10/07 *speed up savejson by 25x using sprintf instead of mat2str, add options support\n 2011/10/06 *savejson works for structs, cells and arrays\n 2011/09/09  derive loadjson from JSON parser from MATLAB Central, draft savejson.m\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/LICENSE_BSD.txt",
    "content": "Copyright 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are\npermitted provided that the following conditions are met:\n\n   1. Redistributions of source code must retain the above copyright notice, this list of\n      conditions and the following disclaimer.\n\n   2. Redistributions in binary form must reproduce the above copyright notice, this list\n      of conditions and the following disclaimer in the documentation and/or other materials\n      provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS \nOR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nThe views and conclusions contained in the software and documentation are those of the\nauthors and should not be interpreted as representing official policies, either expressed\nor implied, of the copyright holders.\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/README.txt",
    "content": "===============================================================================\n=                                 JSONLab                                     =\n=           An open-source MATLAB/Octave JSON encoder and decoder             =\n===============================================================================\n\n*Copyright (C) 2011-2015  Qianqian Fang <fangq at nmr.mgh.harvard.edu>\n*License: BSD License, see License_BSD.txt for details\n*Version: 1.0 (Optimus - Final)\n\n-------------------------------------------------------------------------------\n\nTable of Content:\n\nI.  Introduction\nII. Installation\nIII.Using JSONLab\nIV. Known Issues and TODOs\nV.  Contribution and feedback\n\n-------------------------------------------------------------------------------\n\nI.  Introduction\n\nJSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable, \nhuman-readable and \"[http://en.wikipedia.org/wiki/JSON fat-free]\" text format \nto represent complex and hierarchical data. It is as powerful as \n[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely \nused for data-exchange in applications, and is essential for the wild success \nof [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and \n[http://en.wikipedia.org/wiki/Web_2.0 Web2.0]. \n\nUBJSON (Universal Binary JSON) is a binary JSON format, specifically \noptimized for compact file size and better performance while keeping\nthe semantics as simple as the text-based JSON format. Using the UBJSON\nformat allows to wrap complex binary data in a flexible and extensible\nstructure, making it possible to process complex and large dataset \nwithout accuracy loss due to text conversions.\n\nWe envision that both JSON and its binary version will serve as part of \nthe mainstream data-exchange formats for scientific research in the future. \nIt will provide the flexibility and generality achieved by other popular \ngeneral-purpose file specifications, such as\n[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly \nreduced complexity and enhanced performance.\n\nJSONLab is a free and open-source implementation of a JSON/UBJSON encoder \nand a decoder in the native MATLAB language. It can be used to convert a MATLAB \ndata structure (array, struct, cell, struct array and cell array) into \nJSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB \ndata structure. JSONLab supports both MATLAB and  \n[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).\n\n-------------------------------------------------------------------------------\n\nII. Installation\n\nThe installation of JSONLab is no different than any other simple\nMATLAB toolbox. You only need to download/unzip the JSONLab package\nto a folder, and add the folder's path to MATLAB/Octave's path list\nby using the following command:\n\n    addpath('/path/to/jsonlab');\n\nIf you want to add this path permanently, you need to type \"pathtool\", \nbrowse to the jsonlab root folder and add to the list, then click \"Save\".\nThen, run \"rehash\" in MATLAB, and type \"which loadjson\", if you see an \noutput, that means JSONLab is installed for MATLAB/Octave.\n\n-------------------------------------------------------------------------------\n\nIII.Using JSONLab\n\nJSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder, \nand savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and \ntwo equivallent functions -- loadubjson and saveubjson for the binary \nJSON. The detailed help info for the four functions can be found below:\n\n=== loadjson.m ===\n<pre>\n  data=loadjson(fname,opt)\n     or\n  data=loadjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09, including previous works from \n \n          Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n             created on 2009/11/02\n          Franois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n             created on  2009/03/22\n          Joel Feenstra:\n          http://www.mathworks.com/matlabcentral/fileexchange/20565\n             created on 2008/07/03\n \n  $Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a JSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n                          speed-optimized array parser when loading an \n                          array object. The fast array parser may \n                          collapse block arrays into a single large\n                          array similar to rules defined in cell2mat; 0 to \n                          use a legacy parser; if set to a larger-than-1\n                          value, this option will specify the minimum\n                          dimension to enable the fast array parser. For\n                          example, if the input is a 3D array, setting\n                          FastArrayParser to 1 will return a 3D array;\n                          setting to 2 will return a cell array of 2D\n                          arrays; setting to 3 will return to a 2D cell\n                          array of 1D vectors; setting to 4 will return a\n                          3D cell array.\n            opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n       dat=loadjson(['examples' filesep 'example1.json'])\n       dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n</pre>\n\n=== savejson.m ===\n\n<pre>\n  json=savejson(rootname,obj,filename)\n     or\n  json=savejson(rootname,obj,opt)\n  json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n  Object Notation) string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09\n \n  $Id: savejson.m 458 2014-12-19 22:17:17Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array).\n       filename: a string for the file name to save the output JSON data.\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n                          of a 1D/2D array;\n         opt.ArrayIndent [1|0]: if 1, output explicit data array with\n                          precedent indentation; if 0, no indentation\n         opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n                          to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n                          and $1 represents the sign. For those who want to use\n                          1e999 to represent Inf, they can set opt.Inf to '$11e999'\n         opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n                          to represent NaN\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSONP='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n         opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n         opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a string in the JSON format (see http://json.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       savejson('jmesh',jsonmesh)\n       savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n </pre>\n\n=== loadubjson.m ===\n\n<pre>\n  data=loadubjson(fname,opt)\n     or\n  data=loadubjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/01\n \n  $Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a UBJSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.IntEndian [B|L]: specify the endianness of the integer fields\n                          in the UBJSON input data. B - Big-Endian format for \n                          integers (as required in the UBJSON specification); \n                          L - input integer fields are in Little-Endian order.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       obj=struct('string','value','array',[1 2 3]);\n       ubjdata=saveubjson('obj',obj);\n       dat=loadubjson(ubjdata)\n       dat=loadubjson(['examples' filesep 'example1.ubj'])\n       dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n</pre>\n\n=== saveubjson.m ===\n\n<pre>\n  json=saveubjson(rootname,obj,filename)\n     or\n  json=saveubjson(rootname,obj,opt)\n  json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a Universal \n  Binary JSON (UBJSON) binary string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/17\n \n  $Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array)\n       filename: a string for the file name to save the output UBJSON data\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSON='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a binary string in the UBJSON format (see http://ubjson.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       saveubjson('jsonmesh',jsonmesh)\n       saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n</pre>\n\n\n=== examples ===\n\nUnder the \"examples\" folder, you can find several scripts to demonstrate the\nbasic utilities of JSONLab. Running the \"demo_jsonlab_basic.m\" script, you \nwill see the conversions from MATLAB data structure to JSON text and backward.\nIn \"jsonlab_selftest.m\", we load complex JSON files downloaded from the Internet\nand validate the loadjson/savejson functions for regression testing purposes.\nSimilarly, a \"demo_ubjson_basic.m\" script is provided to test the saveubjson\nand loadubjson pairs for various matlab data structures.\n\nPlease run these examples and understand how JSONLab works before you use\nit to process your data.\n\n-------------------------------------------------------------------------------\n\nIV. Known Issues and TODOs\n\nJSONLab has several known limitations. We are striving to make it more general\nand robust. Hopefully in a few future releases, the limitations become less.\n\nHere are the known issues:\n\n# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;\n# When processing names containing multi-byte characters, Octave and MATLAB \\\ncan give different field-names; you can use feature('DefaultCharacterSet','latin1') \\\nin MATLAB to get consistant results\n# savejson can not handle class and dataset.\n# saveubjson converts a logical array into a uint8 ([U]) array\n# an unofficial N-D array count syntax is implemented in saveubjson. We are \\\nactively communicating with the UBJSON spec maintainer to investigate the \\\npossibility of making it upstream\n# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \\\nfiles, however, it can parse all UBJSON files produced by saveubjson.\n\n-------------------------------------------------------------------------------\n\nV. Contribution and feedback\n\nJSONLab is an open-source project. This means you can not only use it and modify\nit as you wish, but also you can contribute your changes back to JSONLab so\nthat everyone else can enjoy the improvement. For anyone who want to contribute,\nplease download JSONLab source code from it's subversion repository by using the\nfollowing command:\n\n svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab\n\nYou can make changes to the files as needed. Once you are satisfied with your\nchanges, and ready to share it with others, please cd the root directory of \nJSONLab, and type\n\n svn diff > yourname_featurename.patch\n\nYou then email the .patch file to JSONLab's maintainer, Qianqian Fang, at\nthe email address shown in the beginning of this file. Qianqian will review \nthe changes and commit it to the subversion if they are satisfactory.\n\nWe appreciate any suggestions and feedbacks from you. Please use iso2mesh's\nmailing list to report any questions you may have with JSONLab:\n\nhttp://groups.google.com/group/iso2mesh-users?hl=en&pli=1\n\n(Subscription to the mailing list is needed in order to post messages).\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/jsonopt.m",
    "content": "function val=jsonopt(key,default,varargin)\n%\n% val=jsonopt(key,default,optstruct)\n%\n% setting options based on a struct. The struct can be produced\n% by varargin2struct from a list of 'param','value' pairs\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n%\n% $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $\n%\n% input:\n%      key: a string with which one look up a value from a struct\n%      default: if the key does not exist, return default\n%      optstruct: a struct where each sub-field is a key \n%\n% output:\n%      val: if key exists, val=optstruct.key; otherwise val=default\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n% \n\nval=default;\nif(nargin<=2) return; end\nopt=varargin{1};\nif(isstruct(opt) && isfield(opt,key))\n    val=getfield(opt,key);\nend\n\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/loadjson.m",
    "content": "function data = loadjson(fname,varargin)\n%\n% data=loadjson(fname,opt)\n%    or\n% data=loadjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09, including previous works from \n%\n%         Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n%            created on 2009/11/02\n%         François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n%            created on  2009/03/22\n%         Joel Feenstra:\n%         http://www.mathworks.com/matlabcentral/fileexchange/20565\n%            created on 2008/07/03\n%\n% $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a JSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n%                         speed-optimized array parser when loading an \n%                         array object. The fast array parser may \n%                         collapse block arrays into a single large\n%                         array similar to rules defined in cell2mat; 0 to \n%                         use a legacy parser; if set to a larger-than-1\n%                         value, this option will specify the minimum\n%                         dimension to enable the fast array parser. For\n%                         example, if the input is a 3D array, setting\n%                         FastArrayParser to 1 will return a 3D array;\n%                         setting to 2 will return a cell array of 2D\n%                         arrays; setting to 3 will return to a 2D cell\n%                         array of 1D vectors; setting to 4 will return a\n%                         3D cell array.\n%           opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n%      dat=loadjson(['examples' filesep 'example1.json'])\n%      dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\n\nif(jsonopt('ShowProgress',0,opt)==1)\n    opt.progressbar_=waitbar(0,'loading ...');\nend\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\nif(isfield(opt,'progressbar_'))\n    close(opt.progressbar_);\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=data(j).x0x5F_ArraySize_;\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    if next_char ~= '}'\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            parse_char(':');\n            val = parse_value(varargin{:});\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}'\n                break;\n            end\n            parse_char(',');\n        end\n    end\n    parse_char('}');\n\n%%-------------------------------------------------------------------------\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim2=[];\n    arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:});\n    pbar=jsonopt('progressbar_',-1,varargin{:});\n\n    if next_char ~= ']'\n\tif(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))\n            [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos);\n            arraystr=['[' inStr(pos:endpos)];\n            arraystr=regexprep(arraystr,'\"_NaN_\"','NaN');\n            arraystr=regexprep(arraystr,'\"([-+]*)_Inf_\"','$1Inf');\n            arraystr(arraystr==sprintf('\\n'))=[];\n            arraystr(arraystr==sprintf('\\r'))=[];\n            %arraystr=regexprep(arraystr,'\\s*,',','); % this is slow,sometimes needed\n            if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D\n        \tastr=inStr((e1l+1):(e1r-1));\n        \tastr=regexprep(astr,'\"_NaN_\"','NaN');\n        \tastr=regexprep(astr,'\"([-+]*)_Inf_\"','$1Inf');\n        \tastr(astr==sprintf('\\n'))=[];\n        \tastr(astr==sprintf('\\r'))=[];\n        \tastr(astr==' ')='';\n        \tif(isempty(find(astr=='[', 1))) % array is 2D\n                    dim2=length(sscanf(astr,'%f,',[1 inf]));\n        \tend\n            else % array is 1D\n        \tastr=arraystr(2:end-1);\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]);\n        \tif(nextidx>=length(astr)-1)\n                    object=obj;\n                    pos=endpos;\n                    parse_char(']');\n                    return;\n        \tend\n            end\n            if(~isempty(dim2))\n        \tastr=arraystr;\n        \tastr(astr=='[')='';\n        \tastr(astr==']')='';\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf);\n        \tif(nextidx>=length(astr)-1)\n                    object=reshape(obj,dim2,numel(obj)/dim2)';\n                    pos=endpos;\n                    parse_char(']');\n                    if(pbar>0)\n                        waitbar(pos/length(inStr),pbar,'loading ...');\n                    end\n                    return;\n        \tend\n            end\n            arraystr=regexprep(arraystr,'\\]\\s*,','];');\n\telse\n            arraystr='[';\n\tend\n        try\n           if(isoct && regexp(arraystr,'\"','once'))\n                error('Octave eval can produce empty cells for JSON-like input');\n           end\n           object=eval(arraystr);\n           pos=endpos;\n        catch\n         while 1\n            newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1);\n            val = parse_value(newopt);\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            parse_char(',');\n         end\n        end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    parse_char(']');\n    \n    if(pbar>0)\n        waitbar(pos/length(inStr),pbar,'loading ...');\n    end\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr len  esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    if inStr(pos) ~= '\"'\n        error_pos('String starting with \" expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    str = '';\n    while pos <= len\n        while index_esc <= len_esc && esc(index_esc) < pos\n            index_esc = index_esc + 1;\n        end\n        if index_esc > len_esc\n            str = [str inStr(pos:len)];\n            pos = len + 1;\n            break;\n        else\n            str = [str inStr(pos:esc(index_esc)-1)];\n            pos = esc(index_esc);\n        end\n        nstr = length(str); switch inStr(pos)\n            case '\"'\n                pos = pos + 1;\n                if(~isempty(str))\n                    if(strcmp(str,'_Inf_'))\n                        str=Inf;\n                    elseif(strcmp(str,'-_Inf_'))\n                        str=-Inf;\n                    elseif(strcmp(str,'_NaN_'))\n                        str=NaN;\n                    end\n                end\n                return;\n            case '\\'\n                if pos+1 > len\n                    error_pos('End of file reached right after escape character');\n                end\n                pos = pos + 1;\n                switch inStr(pos)\n                    case {'\"' '\\' '/'}\n                        str(nstr+1) = inStr(pos);\n                        pos = pos + 1;\n                    case {'b' 'f' 'n' 'r' 't'}\n                        str(nstr+1) = sprintf(['\\' inStr(pos)]);\n                        pos = pos + 1;\n                    case 'u'\n                        if pos+4 > len\n                            error_pos('End of file reached in escaped unicode character');\n                        end\n                        str(nstr+(1:6)) = inStr(pos-1:pos+4);\n                        pos = pos + 5;\n                end\n            otherwise % should never happen\n                str(nstr+1) = inStr(pos), keyboard\n                pos = pos + 1;\n        end\n    end\n    error_pos('End of file while expecting end of inStr');\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct\n    currstr=inStr(pos:end);\n    numstr=0;\n    if(isoct~=0)\n        numstr=regexp(currstr,'^\\s*-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+\\-]?\\d+)?','end');\n        [num, one] = sscanf(currstr, '%f', 1);\n        delta=numstr+1;\n    else\n        [num, one, err, delta] = sscanf(currstr, '%f', 1);\n        if ~isempty(err)\n            error_pos('Error reading number at position %d');\n        end\n    end\n    pos = pos + delta-1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n    \n    pbar=jsonopt('progressbar_',-1,varargin{:});\n    if(pbar>0)\n        waitbar(pos/len,pbar,'loading ...');\n    end\n    \n    switch(inStr(pos))\n        case '\"'\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'-','0','1','2','3','4','5','6','7','8','9'}\n            val = parse_number(varargin{:});\n            return;\n        case 't'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true')\n                val = true;\n                pos = pos + 4;\n                return;\n            end\n        case 'f'\n            if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false')\n                val = false;\n                pos = pos + 5;\n                return;\n            end\n        case 'n'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null')\n                val = [];\n                pos = pos + 4;\n                return;\n            end\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/loadubjson.m",
    "content": "function data = loadubjson(fname,varargin)\n%\n% data=loadubjson(fname,opt)\n%    or\n% data=loadubjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/01\n%\n% $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a UBJSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.IntEndian [B|L]: specify the endianness of the integer fields\n%                         in the UBJSON input data. B - Big-Endian format for \n%                         integers (as required in the UBJSON specification); \n%                         L - input integer fields are in Little-Endian order.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      obj=struct('string','value','array',[1 2 3]);\n%      ubjdata=saveubjson('obj',obj);\n%      dat=loadubjson(ubjdata)\n%      dat=loadubjson(['examples' filesep 'example1.ubj'])\n%      dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken fileendian systemendian\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\nfileendian=upper(jsonopt('IntEndian','B',opt));\n[os,maxelem,systemendian]=computer;\n\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\n\n\n%%\nfunction newdata=parse_collection(id,data,obj)\n\nif(jsoncount>0 && exist('data','var')) \n    if(~iscell(data))\n       newdata=cell(1);\n       newdata{1}=data;\n       data=newdata;\n    end\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=double(data(j).x0x5F_ArraySize_);\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1); % TODO\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        count=double(parse_number());\n    end\n    if next_char ~= '}'\n        num=0;\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            %parse_char(':');\n            val = parse_value(varargin{:});\n            num=num+1;\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}' || (count>=0 && num>=count)\n                break;\n            end\n            %parse_char(',');\n        end\n    end\n    if(count==-1)\n        parse_char('}');\n    end\n\n%%-------------------------------------------------------------------------\nfunction [cid,len]=elem_info(type)\nid=strfind('iUIlLdD',type);\ndataclass={'int8','uint8','int16','int32','int64','single','double'};\nbytelen=[1,1,2,4,8,4,8];\nif(id>0)\n    cid=dataclass{id};\n    len=bytelen(id);\nelse\n    error_pos('unsupported type at position %d');\nend\n%%-------------------------------------------------------------------------\n\n\nfunction [data adv]=parse_block(type,count,varargin)\nglobal pos inStr isoct fileendian systemendian\n[cid,len]=elem_info(type);\ndatastr=inStr(pos:pos+len*count-1);\nif(isoct)\n    newdata=int8(datastr);\nelse\n    newdata=uint8(datastr);\nend\nid=strfind('iUIlLdD',type);\nif(id<=5 && fileendian~=systemendian)\n    newdata=swapbytes(typecast(newdata,cid));\nend\ndata=typecast(newdata,cid);\nadv=double(len*count);\n\n%%-------------------------------------------------------------------------\n\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim=[];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1);\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        if(next_char=='[')\n            dim=parse_array(varargin{:});\n            count=prod(double(dim));\n        else\n            count=double(parse_number());\n        end\n    end\n    if(~isempty(type))\n        if(count>=0)\n            [object adv]=parse_block(type,count,varargin{:});\n            if(~isempty(dim))\n                object=reshape(object,dim);\n            end\n            pos=pos+adv;\n            return;\n        else\n            endpos=matching_bracket(inStr,pos);\n            [cid,len]=elem_info(type);\n            count=(endpos-pos)/len;\n            [object adv]=parse_block(type,count,varargin{:});\n            pos=pos+adv;\n            parse_char(']');\n            return;\n        end\n    end\n    if next_char ~= ']'\n         while 1\n            val = parse_value(varargin{:});\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            %parse_char(',');\n         end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    if(count==-1)\n        parse_char(']');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    type=inStr(pos);\n    if type ~= 'S' && type ~= 'C' && type ~= 'H'\n        error_pos('String starting with S expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    if(type == 'C')\n        str=inStr(pos);\n        pos=pos+1;\n        return;\n    end\n    bytelen=double(parse_number());\n    if(length(inStr)>=pos+bytelen-1)\n        str=inStr(pos:pos+bytelen-1);\n        pos=pos+bytelen;\n    else\n        error_pos('End of file while expecting end of inStr');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct fileendian systemendian\n    id=strfind('iUIlLdD',inStr(pos));\n    if(isempty(id))\n        error_pos('expecting a number at position %d');\n    end\n    type={'int8','uint8','int16','int32','int64','single','double'};\n    bytelen=[1,1,2,4,8,4,8];\n    datastr=inStr(pos+1:pos+bytelen(id));\n    if(isoct)\n        newdata=int8(datastr);\n    else\n        newdata=uint8(datastr);\n    end\n    if(id<=5 && fileendian~=systemendian)\n        newdata=swapbytes(typecast(newdata,type{id}));\n    end\n    num=typecast(newdata,type{id});\n    pos = pos + bytelen(id)+1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n\n    switch(inStr(pos))\n        case {'S','C','H'}\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'i','U','I','l','L','d','D'}\n            val = parse_number(varargin{:});\n            return;\n        case 'T'\n            val = true;\n            pos = pos + 1;\n            return;\n        case 'F'\n            val = false;\n            pos = pos + 1;\n            return;\n        case {'Z','N'}\n            val = [];\n            pos = pos + 1;\n            return;\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos e1l e1r maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/mergestruct.m",
    "content": "function s=mergestruct(s1,s2)\n%\n% s=mergestruct(s1,s2)\n%\n% merge two struct objects into one\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      s1,s2: a struct object, s1 and s2 can not be arrays\n%\n% output:\n%      s: the merged struct object. fields in s1 and s2 will be combined in s.\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(~isstruct(s1) || ~isstruct(s2))\n    error('input parameters contain non-struct');\nend\nif(length(s1)>1 || length(s2)>1)\n    error('can not merge struct arrays');\nend\nfn=fieldnames(s2);\ns=s1;\nfor i=1:length(fn)              \n    s=setfield(s,fn{i},getfield(s2,fn{i}));\nend\n\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/savejson.m",
    "content": "function json=savejson(rootname,obj,varargin)\n%\n% json=savejson(rootname,obj,filename)\n%    or\n% json=savejson(rootname,obj,opt)\n% json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n% Object Notation) string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09\n%\n% $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array).\n%      filename: a string for the file name to save the output JSON data.\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n%                         of a 1D/2D array;\n%        opt.ArrayIndent [1|0]: if 1, output explicit data array with\n%                         precedent indentation; if 0, no indentation\n%        opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n%                         to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n%                         and $1 represents the sign. For those who want to use\n%                         1e999 to represent Inf, they can set opt.Inf to '$11e999'\n%        opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n%                         to represent NaN\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSONP='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%        opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n%        opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a string in the JSON format (see http://json.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      savejson('jmesh',jsonmesh)\n%      savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\n\nwhitespaces=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nif(jsonopt('Compact',0,opt)==1)\n    whitespaces=struct('tab','','newline','','sep',',');\nend\nif(~isfield(opt,'whitespaces_'))\n    opt.whitespaces_=whitespaces;\nend\n\nnl=whitespaces.newline;\n\njson=obj2json(rootname,obj,rootlevel,opt);\nif(rootisarray)\n    json=sprintf('%s%s',json,nl);\nelse\n    json=sprintf('{%s%s%s}\\n',nl,json,nl);\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=sprintf('%s(%s);%s',jsonp,json,nl);\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    if(jsonopt('SaveBinary',0,opt)==1)\n\t    fid = fopen(opt.FileName, 'wb');\n\t    fwrite(fid,json);\n    else\n\t    fid = fopen(opt.FileName, 'wt');\n\t    fwrite(fid,json,'char');\n    end\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2json(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2json(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2json(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2json(name,item,level,varargin{:});\nelse\n    txt=mat2json(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2json(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=jsonopt('whitespaces_',struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n')),varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nif(len>1)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": [%s',padding0, checkname(name,varargin{:}),nl); name=''; \n    else\n        txt=sprintf('%s[%s',padding0,nl); \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": []',padding0, checkname(name,varargin{:})); name=''; \n    else\n        txt=sprintf('%s[]',padding0); \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n    for i=1:dim(1)\n       txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:}));\n       if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    end\n    if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n    if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2json(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\npadding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1));\nnl=ws.newline;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding0,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding0,nl); end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n  for i=1:dim(1)\n    names = fieldnames(item(i,j));\n    if(~isempty(name) && len==1)\n        txt=sprintf('%s%s\"%s\": {%s',txt,padding1, checkname(name,varargin{:}),nl); \n    else\n        txt=sprintf('%s%s{%s',txt,padding1,nl); \n    end\n    if(~isempty(names))\n      for e=1:length(names)\n\t    txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:}));\n        if(e<length(names)) txt=sprintf('%s%s',txt,','); end\n        txt=sprintf('%s%s',txt,nl);\n      end\n    end\n    txt=sprintf('%s%s}',txt,padding1);\n    if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n  end\n  if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n  if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2json(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding1,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding1,nl); end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    if(isoct)\n        val=regexprep(item(e,:),'\\\\','\\\\');\n        val=regexprep(val,'\"','\\\"');\n        val=regexprep(val,'^\"','\\\"');\n    else\n        val=regexprep(item(e,:),'\\\\','\\\\\\\\');\n        val=regexprep(val,'\"','\\\\\"');\n        val=regexprep(val,'^\"','\\\\\"');\n    end\n    val=escapejsonstring(val);\n    if(len==1)\n        obj=['\"' checkname(name,varargin{:}) '\": ' '\"',val,'\"'];\n\tif(isempty(name)) obj=['\"',val,'\"']; end\n        txt=sprintf('%s%s%s%s',txt,padding1,obj);\n    else\n        txt=sprintf('%s%s%s%s',txt,padding0,['\"',val,'\"']);\n    end\n    if(e==len) sep=''; end\n    txt=sprintf('%s%s',txt,sep);\nend\nif(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2json(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:}))\n    if(isempty(name))\n    \ttxt=sprintf('%s{%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    else\n    \ttxt=sprintf('%s\"%s\": {%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    end\nelse\n    if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0)\n        numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\\[',''),']','');\n    else\n        numtxt=matdata2json(item,level+1,varargin{:});\n    end\n    if(isempty(name))\n    \ttxt=sprintf('%s%s',padding1,numtxt);\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n           \ttxt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        else\n    \t    txt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        end\n    end\n    return;\nend\ndataformat='%s%s%s%s%s';\n\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n    end\n    txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsSparse_\": ','1', sep);\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([iy(:),data'],level+2,varargin{:}), nl);\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,data],level+2,varargin{:}), nl);\n    else\n        % General case, store row and column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,iy,data],level+2,varargin{:}), nl);\n    end\nelse\n    if(isreal(item))\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json(item(:)',level+2,varargin{:}), nl);\n    else\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl);\n    end\nend\ntxt=sprintf('%s%s%s',txt,padding1,'}');\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2json(mat,level,varargin)\n\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\ntab=ws.tab;\nnl=ws.newline;\n\nif(size(mat,1)==1)\n    pre='';\n    post='';\n    level=level-1;\nelse\n    pre=sprintf('[%s',nl);\n    post=sprintf('%s%s]',nl,repmat(tab,1,level-1));\nend\n\nif(isempty(mat))\n    txt='null';\n    return;\nend\nfloatformat=jsonopt('FloatFormat','%.10g',varargin{:});\n%if(numel(mat)>1)\n    formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]];\n%else\n%    formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\\n')]];\n%end\n\nif(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1)\n    formatstr=[repmat(tab,1,level) formatstr];\nend\n\ntxt=sprintf(formatstr,mat');\ntxt(end-length(nl):end)=[];\nif(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1)\n   txt=regexprep(txt,'1','true');\n   txt=regexprep(txt,'0','false');\nend\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],\\n['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\ntxt=[pre txt post];\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n\n%%-------------------------------------------------------------------------\nfunction newstr=escapejsonstring(str)\nnewstr=str;\nisoct=exist('OCTAVE_VERSION','builtin');\nif(isoct)\n   vv=sscanf(OCTAVE_VERSION,'%f');\n   if(vv(1)>=3.8) isoct=0; end\nend\nif(isoct)\n  escapechars={'\\a','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},escapechars{i});\n  end\nelse\n  escapechars={'\\a','\\b','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\\\','\\\\\\\\'));\n  end\nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/saveubjson.m",
    "content": "function json=saveubjson(rootname,obj,varargin)\n%\n% json=saveubjson(rootname,obj,filename)\n%    or\n% json=saveubjson(rootname,obj,opt)\n% json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a Universal \n% Binary JSON (UBJSON) binary string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/17\n%\n% $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array)\n%      filename: a string for the file name to save the output UBJSON data\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSON='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a binary string in the UBJSON format (see http://ubjson.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      saveubjson('jsonmesh',jsonmesh)\n%      saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\njson=obj2ubjson(rootname,obj,rootlevel,opt);\nif(~rootisarray)\n    json=['{' json '}'];\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=[jsonp '(' json ')'];\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    fid = fopen(opt.FileName, 'wb');\n    fwrite(fid,json);\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2ubjson(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2ubjson(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2ubjson(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2ubjson(name,item,level,varargin{:});\nelse\n    txt=mat2ubjson(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2ubjson(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item); % let's handle 1D cell first\nif(len>1) \n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) '[']; name=''; \n    else\n        txt='['; \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; \n    else\n        txt='Z'; \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=[txt '[']; end\n    for i=1:dim(1)\n       txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})];\n    end\n    if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2ubjson(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=[txt '[']; end\n  for i=1:dim(1)\n     names = fieldnames(item(i,j));\n     if(~isempty(name) && len==1)\n        txt=[txt S_(checkname(name,varargin{:})) '{']; \n     else\n        txt=[txt '{']; \n     end\n     if(~isempty(names))\n       for e=1:length(names)\n\t     txt=[txt obj2ubjson(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})];\n       end\n     end\n     txt=[txt '}'];\n  end\n  if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2ubjson(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    val=item(e,:);\n    if(len==1)\n        obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),''];\n\tif(isempty(name)) obj=['',S_(val),'']; end\n        txt=[txt,'',obj];\n    else\n        txt=[txt,'',['',S_(val),'']];\n    end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2ubjson(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) || jsonopt('ArrayToStruct',0,varargin{:}))\n      cid=I_(uint32(max(size(item))));\n      if(isempty(name))\n    \ttxt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ];\n      else\n          if(isempty(item))\n              txt=[S_(checkname(name,varargin{:})),'Z'];\n              return;\n          else\n    \t      txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))];\n          end\n      end\nelse\n    if(isempty(name))\n    \ttxt=matdata2ubjson(item,level+1,varargin{:});\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n            numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\\[',''),']','');\n           \ttxt=[S_(checkname(name,varargin{:})) numtxt];\n        else\n    \t    txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})];\n        end\n    end\n    return;\nend\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=[txt,S_('_ArrayIsComplex_'),'T'];\n    end\n    txt=[txt,S_('_ArrayIsSparse_'),'T'];\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([iy(:),data'],level+2,varargin{:})];\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,data],level+2,varargin{:})];\n    else\n        % General case, store row and column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,iy,data],level+2,varargin{:})];\n    end\nelse\n    if(isreal(item))\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson(item(:)',level+2,varargin{:})];\n    else\n        txt=[txt,S_('_ArrayIsComplex_'),'T'];\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})];\n    end\nend\ntxt=[txt,'}'];\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2ubjson(mat,level,varargin)\nif(isempty(mat))\n    txt='Z';\n    return;\nend\nif(size(mat,1)==1)\n    level=level-1;\nend\ntype='';\nhasnegtive=(mat<0);\nif(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0)))\n    if(isempty(hasnegtive))\n       if(max(mat(:))<=2^8)\n           type='U';\n       end\n    end\n    if(isempty(type))\n        % todo - need to consider negative ones separately\n        id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]);\n        if(isempty(find(id)))\n            error('high-precision data is not yet supported');\n        end\n        key='iIlL';\n\ttype=key(find(id));\n    end\n    txt=[I_a(mat(:),type,size(mat))];\nelseif(islogical(mat))\n    logicalval='FT';\n    if(numel(mat)==1)\n        txt=logicalval(mat+1);\n    else\n        txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')];\n    end\nelse\n    if(numel(mat)==1)\n        txt=['[' D_(mat) ']'];\n    else\n        txt=D_a(mat(:),'D',size(mat));\n    end\nend\n\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n%%-------------------------------------------------------------------------\nfunction val=S_(str)\nif(length(str)==1)\n  val=['C' str];\nelse\n  val=['S' I_(int32(length(str))) str];\nend\n%%-------------------------------------------------------------------------\nfunction val=I_(num)\nif(~isinteger(num))\n    error('input is not an integer');\nend\nif(num>=0 && num<255)\n   val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')];\n   return;\nend\nkey='iIlL';\ncid={'int8','int16','int32','int64'};\nfor i=1:4\n  if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1)))\n    val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')];\n    return;\n  end\nend\nerror('unsupported integer');\n\n%%-------------------------------------------------------------------------\nfunction val=D_(num)\nif(~isfloat(num))\n    error('input is not a float');\nend\n\nif(isa(num,'single'))\n  val=['d' data2byte(num,'uint8')];\nelse\n  val=['D' data2byte(num,'uint8')];\nend\n%%-------------------------------------------------------------------------\nfunction data=I_a(num,type,dim,format)\nid=find(ismember('iUIlL',type));\n\nif(id==0)\n  error('unsupported integer array');\nend\n\n% based on UBJSON specs, all integer types are stored in big endian format\n\nif(id==1)\n  data=data2byte(swapbytes(int8(num)),'uint8');\n  blen=1;\nelseif(id==2)\n  data=data2byte(swapbytes(uint8(num)),'uint8');\n  blen=1;\nelseif(id==3)\n  data=data2byte(swapbytes(int16(num)),'uint8');\n  blen=2;\nelseif(id==4)\n  data=data2byte(swapbytes(int32(num)),'uint8');\n  blen=4;\nelseif(id==5)\n  data=data2byte(swapbytes(int64(num)),'uint8');\n  blen=8;\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/blen)) data(:)'];\n  end\n  data=['[' data(:)'];\nelse\n  data=reshape(data,blen,numel(data)/blen);\n  data(2:blen+1,:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction data=D_a(num,type,dim,format)\nid=find(ismember('dD',type));\n\nif(id==0)\n  error('unsupported float array');\nend\n\nif(id==1)\n  data=data2byte(single(num),'uint8');\nelseif(id==2)\n  data=data2byte(double(num),'uint8');\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)'];\n  end\n  data=['[' data];\nelse\n  data=reshape(data,(id*4),length(data)/(id*4));\n  data(2:(id*4+1),:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction bytes=data2byte(varargin)\nbytes=typecast(varargin{:});\nbytes=bytes(:)';\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/jsonlab/varargin2struct.m",
    "content": "function opt=varargin2struct(varargin)\n%\n% opt=varargin2struct('param1',value1,'param2',value2,...)\n%   or\n% opt=varargin2struct(...,optstruct,...)\n%\n% convert a series of input parameters into a structure\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      'param', value: the input parameters should be pairs of a string and a value\n%       optstruct: if a parameter is a struct, the fields will be merged to the output struct\n%\n% output:\n%      opt: a struct where opt.param1=value1, opt.param2=value2 ...\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nlen=length(varargin);\nopt=struct;\nif(len==0) return; end\ni=1;\nwhile(i<=len)\n    if(isstruct(varargin{i}))\n        opt=mergestruct(opt,varargin{i});\n    elseif(ischar(varargin{i}) && i<len)\n        opt=setfield(opt,varargin{i},varargin{i+1});\n        i=i+1;\n    else\n        error('input must be in the form of ...,''name'',value,... pairs or structs');\n    end\n    i=i+1;\nend\n\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/makeValidFieldName.m",
    "content": "function str = makeValidFieldName(str)\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    isoct=exist('OCTAVE_VERSION','builtin');\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n"
  },
  {
    "path": "machine-learning-ex5/ex5/lib/submitWithConfiguration.m",
    "content": "function submitWithConfiguration(conf)\n  addpath('./lib/jsonlab');\n\n  parts = parts(conf);\n\n  fprintf('== Submitting solutions | %s...\\n', conf.itemName);\n\n  tokenFile = 'token.mat';\n  if exist(tokenFile, 'file')\n    load(tokenFile);\n    [email token] = promptToken(email, token, tokenFile);\n  else\n    [email token] = promptToken('', '', tokenFile);\n  end\n\n  if isempty(token)\n    fprintf('!! Submission Cancelled\\n');\n    return\n  end\n\n  try\n    response = submitParts(conf, email, token, parts);\n  catch\n    e = lasterror();\n    fprintf( ...\n      '!! Submission failed: unexpected error: %s\\n', ...\n      e.message);\n    fprintf('!! Please try again later.\\n');\n    return\n  end\n\n  if isfield(response, 'errorMessage')\n    fprintf('!! Submission failed: %s\\n', response.errorMessage);\n  else\n    showFeedback(parts, response);\n    save(tokenFile, 'email', 'token');\n  end\nend\n\nfunction [email token] = promptToken(email, existingToken, tokenFile)\n  if (~isempty(email) && ~isempty(existingToken))\n    prompt = sprintf( ...\n      'Use token from last successful submission (%s)? (Y/n): ', ...\n      email);\n    reenter = input(prompt, 's');\n\n    if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y')\n      token = existingToken;\n      return;\n    else\n      delete(tokenFile);\n    end\n  end\n  email = input('Login (email address): ', 's');\n  token = input('Token: ', 's');\nend\n\nfunction isValid = isValidPartOptionIndex(partOptions, i)\n  isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions));\nend\n\nfunction response = submitParts(conf, email, token, parts)\n  body = makePostBody(conf, email, token, parts);\n  submissionUrl = submissionUrl();\n  params = {'jsonBody', body};\n  responseBody = urlread(submissionUrl, 'post', params);\n  response = loadjson(responseBody);\nend\n\nfunction body = makePostBody(conf, email, token, parts)\n  bodyStruct.assignmentSlug = conf.assignmentSlug;\n  bodyStruct.submitterEmail = email;\n  bodyStruct.secret = token;\n  bodyStruct.parts = makePartsStruct(conf, parts);\n\n  opt.Compact = 1;\n  body = savejson('', bodyStruct, opt);\nend\n\nfunction partsStruct = makePartsStruct(conf, parts)\n  for part = parts\n    partId = part{:}.id;\n    fieldName = makeValidFieldName(partId);\n    outputStruct.output = conf.output(partId);\n    partsStruct.(fieldName) = outputStruct;\n  end\nend\n\nfunction [parts] = parts(conf)\n  parts = {};\n  for partArray = conf.partArrays\n    part.id = partArray{:}{1};\n    part.sourceFiles = partArray{:}{2};\n    part.name = partArray{:}{3};\n    parts{end + 1} = part;\n  end\nend\n\nfunction showFeedback(parts, response)\n  fprintf('== \\n');\n  fprintf('== %43s | %9s | %-s\\n', 'Part Name', 'Score', 'Feedback');\n  fprintf('== %43s | %9s | %-s\\n', '---------', '-----', '--------');\n  for part = parts\n    score = '';\n    partFeedback = '';\n    partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id));\n    partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id));\n    score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore);\n    fprintf('== %43s | %9s | %-s\\n', part{:}.name, score, partFeedback);\n  end\n  evaluation = response.evaluation;\n  totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore);\n  fprintf('==                                   --------------------------------\\n');\n  fprintf('== %43s | %9s | %-s\\n', '', totalScore, '');\n  fprintf('== \\n');\nend\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%\n% Service configuration\n%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\nfunction submissionUrl = submissionUrl()\n  submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1';\nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/linearRegCostFunction.m",
    "content": "function [J, grad] = linearRegCostFunction(X, y, theta, lambda)\n%LINEARREGCOSTFUNCTION Compute cost and gradient for regularized linear \n%regression with multiple variables\n%   [J, grad] = LINEARREGCOSTFUNCTION(X, y, theta, lambda) computes the \n%   cost of using theta as the parameter for linear regression to fit the \n%   data points in X and y. Returns the cost in J and the gradient in grad\n\n% Initialize some useful values\nm = length(y); % number of training examples\n\n% You need to return the following variables correctly \nJ = 0;\ngrad = zeros(size(theta));\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the cost and gradient of regularized linear \n%               regression for a particular choice of theta.\n%\n%               You should set J to the cost and grad to the gradient.\n%\ntheta1 = theta;\ntheta1(1) = 0;\nregterm = theta1'*theta1;\nh = X*theta;\nJ = ((h-y)'*(h-y)+lambda*regterm)/(2*m);\n\n\ngrad = (X'*(h-y)+lambda*theta1)/m;\n\n\n\n% =========================================================================\n\ngrad = grad(:);\n\nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/plotFit.m",
    "content": "function plotFit(min_x, max_x, mu, sigma, theta, p)\n%PLOTFIT Plots a learned polynomial regression fit over an existing figure.\n%Also works with linear regression.\n%   PLOTFIT(min_x, max_x, mu, sigma, theta, p) plots the learned polynomial\n%   fit with power p and feature normalization (mu, sigma).\n\n% Hold on to the current figure\nhold on;\n\n% We plot a range slightly bigger than the min and max values to get\n% an idea of how the fit will vary outside the range of the data points\nx = (min_x - 15: 0.05 : max_x + 25)';\n\n% Map the X values \nX_poly = polyFeatures(x, p);\nX_poly = bsxfun(@minus, X_poly, mu);\nX_poly = bsxfun(@rdivide, X_poly, sigma);\n\n% Add ones\nX_poly = [ones(size(x, 1), 1) X_poly];\n\n% Plot\nplot(x, X_poly * theta, '--', 'LineWidth', 2)\n\n% Hold off to the current figure\nhold off\n\nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/polyFeatures.m",
    "content": "function [X_poly] = polyFeatures(X, p)\n%POLYFEATURES Maps X (1D vector) into the p-th power\n%   [X_poly] = POLYFEATURES(X, p) takes a data matrix X (size m x 1) and\n%   maps each example into its polynomial features where\n%   X_poly(i, :) = [X(i) X(i).^2 X(i).^3 ...  X(i).^p];\n%\n\n\n% You need to return the following variables correctly.\nX_poly = zeros(numel(X), p);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Given a vector X, return a matrix X_poly where the p-th \n%               column of X contains the values of X to the p-th power.\n%\n% \nX_poly = X;\n\nfor i = 2:p\n    X_poly = [X_poly X.^i];\nend\n\n\n% =========================================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/submit.m",
    "content": "function submit()\n  addpath('./lib');\n\n  conf.assignmentSlug = 'regularized-linear-regression-and-bias-variance';\n  conf.itemName = 'Regularized Linear Regression and Bias/Variance';\n  conf.partArrays = { ...\n    { ...\n      '1', ...\n      { 'linearRegCostFunction.m' }, ...\n      'Regularized Linear Regression Cost Function', ...\n    }, ...\n    { ...\n      '2', ...\n      { 'linearRegCostFunction.m' }, ...\n      'Regularized Linear Regression Gradient', ...\n    }, ...\n    { ...\n      '3', ...\n      { 'learningCurve.m' }, ...\n      'Learning Curve', ...\n    }, ...\n    { ...\n      '4', ...\n      { 'polyFeatures.m' }, ...\n      'Polynomial Feature Mapping', ...\n    }, ...\n    { ...\n      '5', ...\n      { 'validationCurve.m' }, ...\n      'Validation Curve', ...\n    }, ...\n  };\n  conf.output = @output;\n\n  submitWithConfiguration(conf);\nend\n\nfunction out = output(partId, auxstring)\n  % Random Test Cases\n  X = [ones(10,1) sin(1:1.5:15)' cos(1:1.5:15)'];\n  y = sin(1:3:30)';\n  Xval = [ones(10,1) sin(0:1.5:14)' cos(0:1.5:14)'];\n  yval = sin(1:10)';\n  if partId == '1'\n    [J] = linearRegCostFunction(X, y, [0.1 0.2 0.3]', 0.5);\n    out = sprintf('%0.5f ', J);\n  elseif partId == '2'\n    [J, grad] = linearRegCostFunction(X, y, [0.1 0.2 0.3]', 0.5);\n    out = sprintf('%0.5f ', grad);\n  elseif partId == '3'\n    [error_train, error_val] = ...\n        learningCurve(X, y, Xval, yval, 1);\n    out = sprintf('%0.5f ', [error_train(:); error_val(:)]);\n  elseif partId == '4'\n    [X_poly] = polyFeatures(X(2,:)', 8);\n    out = sprintf('%0.5f ', X_poly);\n  elseif partId == '5'\n    [lambda_vec, error_train, error_val] = ...\n        validationCurve(X, y, Xval, yval);\n    out = sprintf('%0.5f ', ...\n        [lambda_vec(:); error_train(:); error_val(:)]);\n  end \nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/trainLinearReg.m",
    "content": "function [theta] = trainLinearReg(X, y, lambda)\n%TRAINLINEARREG Trains linear regression given a dataset (X, y) and a\n%regularization parameter lambda\n%   [theta] = TRAINLINEARREG (X, y, lambda) trains linear regression using\n%   the dataset (X, y) and regularization parameter lambda. Returns the\n%   trained parameters theta.\n%\n\n% Initialize Theta\ninitial_theta = zeros(size(X, 2), 1); \n\n% Create \"short hand\" for the cost function to be minimized\ncostFunction = @(t) linearRegCostFunction(X, y, t, lambda);\n\n% Now, costFunction is a function that takes in only one argument\noptions = optimset('MaxIter', 200, 'GradObj', 'on');\n\n% Minimize using fmincg\ntheta = fmincg(costFunction, initial_theta, options);\n\nend\n"
  },
  {
    "path": "machine-learning-ex5/ex5/validationCurve.m",
    "content": "function [lambda_vec, error_train, error_val] = ...\n    validationCurve(X, y, Xval, yval)\n%VALIDATIONCURVE Generate the train and validation errors needed to\n%plot a validation curve that we can use to select lambda\n%   [lambda_vec, error_train, error_val] = ...\n%       VALIDATIONCURVE(X, y, Xval, yval) returns the train\n%       and validation errors (in error_train, error_val)\n%       for different values of lambda. You are given the training set (X,\n%       y) and validation set (Xval, yval).\n%\n\n% Selected values of lambda (you should not change this)\nlambda_vec = [0 0.001 0.003 0.01 0.03 0.1 0.3 1 3 10]';\n\n% You need to return these variables correctly.\nerror_train = zeros(length(lambda_vec), 1);\nerror_val = zeros(length(lambda_vec), 1);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Fill in this function to return training errors in \n%               error_train and the validation errors in error_val. The \n%               vector lambda_vec contains the different lambda parameters \n%               to use for each calculation of the errors, i.e, \n%               error_train(i), and error_val(i) should give \n%               you the errors obtained after training with \n%               lambda = lambda_vec(i)\n%\n% Note: You can loop over lambda_vec with the following:\n%\n%       for i = 1:length(lambda_vec)\n%           lambda = lambda_vec(i);\n%           % Compute train / val errors when training linear \n%           % regression with regularization parameter lambda\n%           % You should store the result in error_train(i)\n%           % and error_val(i)\n%           ....\n%           \n%       end\n%\n%\n\nfor i = 1:length(lambda_vec)\n    [theta(:,i)] = trainLinearReg(X, y, lambda_vec(i));\n    h_train(:,i) = X*theta(:,i);\n    h_val(:,i) = Xval*theta(:,i);\n    error_train(i) = (((h_train(:,i)-y)'*(h_train(:,i)-y))/(2*length(y))) ;\n    error_val(i) = (((h_val(:,i)-yval)'*(h_val(:,i)-yval))/(2*length(yval))) ;\nend\n\n\n\n\n\n\n\n\n% =========================================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/dataset3Params.m",
    "content": "function [C, sigma] = dataset3Params(X, y, Xval, yval)\n%EX6PARAMS returns your choice of C and sigma for Part 3 of the exercise\n%where you select the optimal (C, sigma) learning parameters to use for SVM\n%with RBF kernel\n%   [C, sigma] = EX6PARAMS(X, y, Xval, yval) returns your choice of C and \n%   sigma. You should complete this function to return the optimal C and \n%   sigma based on a cross-validation set.\n%\n\n% You need to return the following variables correctly.\nC = 1;\nsigma = 0.3;\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Fill in this function to return the optimal C and sigma\n%               learning parameters found using the cross validation set.\n%               You can use svmPredict to predict the labels on the cross\n%               validation set. For example, \n%                   predictions = svmPredict(model, Xval);\n%               will return the predictions on the cross validation set.\n%\n%  Note: You can compute the prediction error using \n%        mean(double(predictions ~= yval))\n%\n\nCTest = [0.01 0.03 0.1 0.3 1 3 10 30];\nsigmaTest = [0.01 0.03 0.1 0.3 1,3 10 30];\nerrors = zeros(length(CTest),length(sigmaTest));\n\nfor i = 1:length(CTest)\n    for j = 1:length(sigmaTest)\n        model= svmTrain(X, y, CTest(i), @(x1, x2) gaussianKernel(x1, x2, sigmaTest(j)));\n        predictions = svmPredict(model, Xval);\n        errors(i,j) = mean(double(predictions ~= yval));\n        clear model;\n        clear predictions;\n    end\nend\n\n[dummy,ind] = min(errors(:));\n[i,j] = ind2sub([size(errors,1) size(errors,2)],ind);\nC = CTest(i);\nsigma = sigmaTest(j);\n\n\n\n% =========================================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/emailFeatures.m",
    "content": "function x = emailFeatures(word_indices)\n%EMAILFEATURES takes in a word_indices vector and produces a feature vector\n%from the word indices\n%   x = EMAILFEATURES(word_indices) takes in a word_indices vector and \n%   produces a feature vector from the word indices. \n\n% Total number of words in the dictionary\nn = 1899;\n\n% You need to return the following variables correctly.\nx = zeros(n, 1);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Fill in this function to return a feature vector for the\n%               given email (word_indices). To help make it easier to \n%               process the emails, we have have already pre-processed each\n%               email and converted each word in the email into an index in\n%               a fixed dictionary (of 1899 words). The variable\n%               word_indices contains the list of indices of the words\n%               which occur in one email.\n% \n%               Concretely, if an email has the text:\n%\n%                  The quick brown fox jumped over the lazy dog.\n%\n%               Then, the word_indices vector for this text might look \n%               like:\n%               \n%                   60  100   33   44   10     53  60  58   5\n%\n%               where, we have mapped each word onto a number, for example:\n%\n%                   the   -- 60\n%                   quick -- 100\n%                   ...\n%\n%              (note: the above numbers are just an example and are not the\n%               actual mappings).\n%\n%              Your task is take one such word_indices vector and construct\n%              a binary feature vector that indicates whether a particular\n%              word occurs in the email. That is, x(i) = 1 when word i\n%              is present in the email. Concretely, if the word 'the' (say,\n%              index 60) appears in the email, then x(60) = 1. The feature\n%              vector should look like:\n%\n%              x = [ 0 0 0 0 1 0 0 0 ... 0 0 0 0 1 ... 0 0 0 1 0 ..];\n%\n%\n\n\nfor i = 1:size(word_indices,1)\n    x(word_indices(i)) = 1;\nend\n\n\n\n\n\n% =========================================================================\n    \n\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/emailSample1.txt",
    "content": "> Anyone knows how much it costs to host a web portal ?\n>\nWell, it depends on how many visitors you're expecting.\nThis can be anywhere from less than 10 bucks a month to a couple of $100. \nYou should checkout http://www.rackspace.com/ or perhaps Amazon EC2 \nif youre running something big..\n\nTo unsubscribe yourself from this mailing list, send an email to:\ngroupname-unsubscribe@egroups.com\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/emailSample2.txt",
    "content": "Folks,\n \nmy first time posting - have a bit of Unix experience, but am new to Linux.\n\n \nJust got a new PC at home - Dell box with Windows XP. Added a second hard disk\nfor Linux. Partitioned the disk and have installed Suse 7.2 from CD, which went\nfine except it didn't pick up my monitor.\n \nI have a Dell branded E151FPp 15\" LCD flat panel monitor and a nVidia GeForce4\nTi4200 video card, both of which are probably too new to feature in Suse's default\nset. I downloaded a driver from the nVidia website and installed it using RPM.\nThen I ran Sax2 (as was recommended in some postings I found on the net), but\nit still doesn't feature my video card in the available list. What next?\n \nAnother problem. I have a Dell branded keyboard and if I hit Caps-Lock twice,\nthe whole machine crashes (in Linux, not Windows) - even the on/off switch is\ninactive, leaving me to reach for the power cable instead.\n \nIf anyone can help me in any way with these probs., I'd be really grateful -\nI've searched the 'net but have run out of ideas.\n \nOr should I be going for a different version of Linux such as RedHat? Opinions\nwelcome.\n \nThanks a lot,\nPeter\n\n-- \nIrish Linux Users' Group: ilug@linux.ie\nhttp://www.linux.ie/mailman/listinfo/ilug for (un)subscription information.\nList maintainer: listmaster@linux.ie\n\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/ex6.m",
    "content": "%% Machine Learning Online Class\n%  Exercise 6 | Support Vector Machines\n%\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the\n%  exercise. You will need to complete the following functions:\n%\n%     gaussianKernel.m\n%     dataset3Params.m\n%     processEmail.m\n%     emailFeatures.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% =============== Part 1: Loading and Visualizing Data ================\n%  We start the exercise by first loading and visualizing the dataset. \n%  The following code will load the dataset into your environment and plot\n%  the data.\n%\n\nfprintf('Loading and Visualizing Data ...\\n')\n\n% Load from ex6data1: \n% You will have X, y in your environment\nload('ex6data1.mat');\n\n% Plot training data\nplotData(X, y);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ==================== Part 2: Training Linear SVM ====================\n%  The following code will train a linear SVM on the dataset and plot the\n%  decision boundary learned.\n%\n\n% Load from ex6data1: \n% You will have X, y in your environment\nload('ex6data1.mat');\n\nfprintf('\\nTraining Linear SVM ...\\n')\n\n% You should try to change the C value below and see how the decision\n% boundary varies (e.g., try C = 1000)\nC = 1;\nmodel = svmTrain(X, y, C, @linearKernel, 1e-3, 20);\nvisualizeBoundaryLinear(X, y, model);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =============== Part 3: Implementing Gaussian Kernel ===============\n%  You will now implement the Gaussian kernel to use\n%  with the SVM. You should complete the code in gaussianKernel.m\n%\nfprintf('\\nEvaluating the Gaussian Kernel ...\\n')\n\nx1 = [1 2 1]; x2 = [0 4 -1]; sigma = 2;\nsim = gaussianKernel(x1, x2, sigma);\n\nfprintf(['Gaussian Kernel between x1 = [1; 2; 1], x2 = [0; 4; -1], sigma = 0.5 :' ...\n         '\\n\\t%f\\n(this value should be about 0.324652)\\n'], sim);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =============== Part 4: Visualizing Dataset 2 ================\n%  The following code will load the next dataset into your environment and \n%  plot the data. \n%\n\nfprintf('Loading and Visualizing Data ...\\n')\n\n% Load from ex6data2: \n% You will have X, y in your environment\nload('ex6data2.mat');\n\n% Plot training data\nplotData(X, y);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ========== Part 5: Training SVM with RBF Kernel (Dataset 2) ==========\n%  After you have implemented the kernel, we can now use it to train the \n%  SVM classifier.\n% \nfprintf('\\nTraining SVM with RBF Kernel (this may take 1 to 2 minutes) ...\\n');\n\n% Load from ex6data2: \n% You will have X, y in your environment\nload('ex6data2.mat');\n\n% SVM Parameters\nC = 1; sigma = 0.1;\n\n% We set the tolerance and max_passes lower here so that the code will run\n% faster. However, in practice, you will want to run the training to\n% convergence.\nmodel= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma)); \nvisualizeBoundary(X, y, model);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =============== Part 6: Visualizing Dataset 3 ================\n%  The following code will load the next dataset into your environment and \n%  plot the data. \n%\n\nfprintf('Loading and Visualizing Data ...\\n')\n\n% Load from ex6data3: \n% You will have X, y in your environment\nload('ex6data3.mat');\n\n% Plot training data\nplotData(X, y);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ========== Part 7: Training SVM with RBF Kernel (Dataset 3) ==========\n\n%  This is a different dataset that you can use to experiment with. Try\n%  different values of C and sigma here.\n% \n\n% Load from ex6data3: \n% You will have X, y in your environment\nload('ex6data3.mat');\n\n% Try different SVM Parameters here\n[C, sigma] = dataset3Params(X, y, Xval, yval);\n\n% Train the SVM\nmodel= svmTrain(X, y, C, @(x1, x2) gaussianKernel(x1, x2, sigma));\nvisualizeBoundary(X, y, model);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/ex6_spam.m",
    "content": "%% Machine Learning Online Class\n%  Exercise 6 | Spam Classification with SVMs\n%\n%  Instructions\n%  ------------\n% \n%  This file contains code that helps you get started on the\n%  exercise. You will need to complete the following functions:\n%\n%     gaussianKernel.m\n%     dataset3Params.m\n%     processEmail.m\n%     emailFeatures.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% ==================== Part 1: Email Preprocessing ====================\n%  To use an SVM to classify emails into Spam v.s. Non-Spam, you first need\n%  to convert each email into a vector of features. In this part, you will\n%  implement the preprocessing steps for each email. You should\n%  complete the code in processEmail.m to produce a word indices vector\n%  for a given email.\n\nfprintf('\\nPreprocessing sample email (emailSample1.txt)\\n');\n\n% Extract Features\nfile_contents = readFile('emailSample1.txt');\nword_indices  = processEmail(file_contents);\n\n% Print Stats\nfprintf('Word Indices: \\n');\nfprintf(' %d', word_indices);\nfprintf('\\n\\n');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ==================== Part 2: Feature Extraction ====================\n%  Now, you will convert each email into a vector of features in R^n. \n%  You should complete the code in emailFeatures.m to produce a feature\n%  vector for a given email.\n\nfprintf('\\nExtracting features from sample email (emailSample1.txt)\\n');\n\n% Extract Features\nfile_contents = readFile('emailSample1.txt');\nword_indices  = processEmail(file_contents);\nfeatures      = emailFeatures(word_indices);\n\n% Print Stats\nfprintf('Length of feature vector: %d\\n', length(features));\nfprintf('Number of non-zero entries: %d\\n', sum(features > 0));\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =========== Part 3: Train Linear SVM for Spam Classification ========\n%  In this section, you will train a linear classifier to determine if an\n%  email is Spam or Not-Spam.\n\n% Load the Spam Email dataset\n% You will have X, y in your environment\nload('spamTrain.mat');\n\nfprintf('\\nTraining Linear SVM (Spam Classification)\\n')\nfprintf('(this may take 1 to 2 minutes) ...\\n')\n\nC = 0.1;\nmodel = svmTrain(X, y, C, @linearKernel);\n\np = svmPredict(model, X);\n\nfprintf('Training Accuracy: %f\\n', mean(double(p == y)) * 100);\n\n%% =================== Part 4: Test Spam Classification ================\n%  After training the classifier, we can evaluate it on a test set. We have\n%  included a test set in spamTest.mat\n\n% Load the test dataset\n% You will have Xtest, ytest in your environment\nload('spamTest.mat');\n\nfprintf('\\nEvaluating the trained Linear SVM on a test set ...\\n')\n\np = svmPredict(model, Xtest);\n\nfprintf('Test Accuracy: %f\\n', mean(double(p == ytest)) * 100);\npause;\n\n\n%% ================= Part 5: Top Predictors of Spam ====================\n%  Since the model we are training is a linear SVM, we can inspect the\n%  weights learned by the model to understand better how it is determining\n%  whether an email is spam or not. The following code finds the words with\n%  the highest weights in the classifier. Informally, the classifier\n%  'thinks' that these words are the most likely indicators of spam.\n%\n\n% Sort the weights and obtin the vocabulary list\n[weight, idx] = sort(model.w, 'descend');\nvocabList = getVocabList();\n\nfprintf('\\nTop predictors of spam: \\n');\nfor i = 1:15\n    fprintf(' %-15s (%f) \\n', vocabList{idx(i)}, weight(i));\nend\n\nfprintf('\\n\\n');\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n%% =================== Part 6: Try Your Own Emails =====================\n%  Now that you've trained the spam classifier, you can use it on your own\n%  emails! In the starter code, we have included spamSample1.txt,\n%  spamSample2.txt, emailSample1.txt and emailSample2.txt as examples. \n%  The following code reads in one of these emails and then uses your \n%  learned SVM classifier to determine whether the email is Spam or \n%  Not Spam\n\n% Set the file to be read in (change this to spamSample2.txt,\n% emailSample1.txt or emailSample2.txt to see different predictions on\n% different emails types). Try your own emails as well!\nfilename = 'spamSample1.txt';\n\n% Read and predict\nfile_contents = readFile(filename);\nword_indices  = processEmail(file_contents);\nx             = emailFeatures(word_indices);\np = svmPredict(model, x);\n\nfprintf('\\nProcessed %s\\n\\nSpam Classification: %d\\n', filename, p);\nfprintf('(1 indicates spam, 0 indicates not spam)\\n\\n');\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/gaussianKernel.m",
    "content": "function sim = gaussianKernel(x1, x2, sigma)\n%RBFKERNEL returns a radial basis function kernel between x1 and x2\n%   sim = gaussianKernel(x1, x2) returns a gaussian kernel between x1 and x2\n%   and returns the value in sim\n\n% Ensure that x1 and x2 are column vectors\nx1 = x1(:); x2 = x2(:);\n\n% You need to return the following variables correctly.\nsim = 0;\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Fill in this function to return the similarity between x1\n%               and x2 computed using a Gaussian kernel with bandwidth\n%               sigma\n%\n%\ntemp = (x1-x2)'*(x1-x2);\n\nsim = exp(-temp/(2*(sigma^2)));\n\n\n\n\n% =============================================================\n    \nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/getVocabList.m",
    "content": "function vocabList = getVocabList()\n%GETVOCABLIST reads the fixed vocabulary list in vocab.txt and returns a\n%cell array of the words\n%   vocabList = GETVOCABLIST() reads the fixed vocabulary list in vocab.txt \n%   and returns a cell array of the words in vocabList.\n\n\n%% Read the fixed vocabulary list\nfid = fopen('vocab.txt');\n\n% Store all dictionary words in cell array vocab{}\nn = 1899;  % Total number of words in the dictionary\n\n% For ease of implementation, we use a struct to map the strings => integers\n% In practice, you'll want to use some form of hashmap\nvocabList = cell(n, 1);\nfor i = 1:n\n    % Word Index (can ignore since it will be = i)\n    fscanf(fid, '%d', 1);\n    % Actual Word\n    vocabList{i} = fscanf(fid, '%s', 1);\nend\nfclose(fid);\n\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/AUTHORS.txt",
    "content": "The author of \"jsonlab\" toolbox is Qianqian Fang. Qianqian\nis currently an Assistant Professor at Massachusetts General Hospital, \nHarvard Medical School.\n\nAddress: Martinos Center for Biomedical Imaging, \n         Massachusetts General Hospital, \n         Harvard Medical School\n         Bldg 149, 13th St, Charlestown, MA 02129, USA\nURL: http://nmr.mgh.harvard.edu/~fangq/\nEmail: <fangq at nmr.mgh.harvard.edu> or <fangqq at gmail.com>\n\n\nThe script loadjson.m was built upon previous works by\n\n- Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n       date: 2009/11/02\n- François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n       date: 2009/03/22\n- Joel Feenstra: http://www.mathworks.com/matlabcentral/fileexchange/20565\n       date: 2008/07/03\n\n\nThis toolbox contains patches submitted by the following contributors:\n\n- Blake Johnson <bjohnso at bbn.com>\n  part of revision 341\n\n- Niclas Borlin <Niclas.Borlin at cs.umu.se>\n  various fixes in revision 394, including\n  - loadjson crashes for all-zero sparse matrix.\n  - loadjson crashes for empty sparse matrix.\n  - Non-zero size of 0-by-N and N-by-0 empty matrices is lost after savejson/loadjson.\n  - loadjson crashes for sparse real column vector.\n  - loadjson crashes for sparse complex column vector.\n  - Data is corrupted by savejson for sparse real row vector.\n  - savejson crashes for sparse complex row vector. \n\n- Yul Kang <yul.kang.on at gmail.com>\n  patches for svn revision 415.\n  - savejson saves an empty cell array as [] instead of null\n  - loadjson differentiates an empty struct from an empty array\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/ChangeLog.txt",
    "content": "============================================================================\n\n   JSONlab - a toolbox to encode/decode JSON/UBJSON files in MATLAB/Octave\n\n----------------------------------------------------------------------------\n\nJSONlab ChangeLog (key features marked by *):\n\n== JSONlab 1.0 (codename: Optimus - Final), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2015/01/02 polish help info for all major functions, update examples, finalize 1.0\n 2014/12/19 fix a bug to strictly respect NoRowBracket in savejson\n\n== JSONlab 1.0.0-RC2 (codename: Optimus - RC2), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/11/22 show progress bar in loadjson ('ShowProgress') \n 2014/11/17 add Compact option in savejson to output compact JSON format ('Compact')\n 2014/11/17 add FastArrayParser in loadjson to specify fast parser applicable levels\n 2014/09/18 start official github mirror: https://github.com/fangq/jsonlab\n\n== JSONlab 1.0.0-RC1 (codename: Optimus - RC1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/09/17  fix several compatibility issues when running on octave versions 3.2-3.8\n 2014/09/17  support 2D cell and struct arrays in both savejson and saveubjson\n 2014/08/04  escape special characters in a JSON string\n 2014/02/16  fix a bug when saving ubjson files\n\n== JSONlab 0.9.9 (codename: Optimus - beta), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/01/22  use binary read and write in saveubjson and loadubjson\n\n== JSONlab 0.9.8-1 (codename: Optimus - alpha update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2013/10/07 better round-trip conservation for empty arrays and structs (patch submitted by Yul Kang)\n\n== JSONlab 0.9.8 (codename: Optimus - alpha), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2013/08/23 *universal Binary JSON (UBJSON) support, including both saveubjson and loadubjson\n\n== JSONlab 0.9.1 (codename: Rodimus, update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2012/12/18 *handling of various empty and sparse matrices (fixes submitted by Niclas Borlin)\n\n== JSONlab 0.9.0 (codename: Rodimus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/06/17 *new format for an invalid leading char, unpacking hex code in savejson\n 2012/06/01  support JSONP in savejson\n 2012/05/25  fix the empty cell bug (reported by Cyril Davin)\n 2012/04/05  savejson can save to a file (suggested by Patrick Rapin)\n\n== JSONlab 0.8.1 (codename: Sentiel, Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/02/28  loadjson quotation mark escape bug, see http://bit.ly/yyk1nS\n 2012/01/25  patch to handle root-less objects, contributed by Blake Johnson\n\n== JSONlab 0.8.0 (codename: Sentiel), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/01/13 *speed up loadjson by 20 fold when parsing large data arrays in matlab\n 2012/01/11  remove row bracket if an array has 1 element, suggested by Mykel Kochenderfer\n 2011/12/22 *accept sequence of 'param',value input in savejson and loadjson\n 2011/11/18  fix struct array bug reported by Mykel Kochenderfer\n\n== JSONlab 0.5.1 (codename: Nexus Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/21  fix a bug in loadjson, previous code does not use any of the acceleration\n 2011/10/20  loadjson supports JSON collections - concatenated JSON objects\n\n== JSONlab 0.5.0 (codename: Nexus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/16  package and release jsonlab 0.5.0\n 2011/10/15 *add json demo and regression test, support cpx numbers, fix double quote bug\n 2011/10/11 *speed up readjson dramatically, interpret _Array* tags, show data in root level\n 2011/10/10  create jsonlab project, start jsonlab website, add online documentation\n 2011/10/07 *speed up savejson by 25x using sprintf instead of mat2str, add options support\n 2011/10/06 *savejson works for structs, cells and arrays\n 2011/09/09  derive loadjson from JSON parser from MATLAB Central, draft savejson.m\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/LICENSE_BSD.txt",
    "content": "Copyright 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are\npermitted provided that the following conditions are met:\n\n   1. Redistributions of source code must retain the above copyright notice, this list of\n      conditions and the following disclaimer.\n\n   2. Redistributions in binary form must reproduce the above copyright notice, this list\n      of conditions and the following disclaimer in the documentation and/or other materials\n      provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS \nOR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nThe views and conclusions contained in the software and documentation are those of the\nauthors and should not be interpreted as representing official policies, either expressed\nor implied, of the copyright holders.\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/README.txt",
    "content": "===============================================================================\n=                                 JSONLab                                     =\n=           An open-source MATLAB/Octave JSON encoder and decoder             =\n===============================================================================\n\n*Copyright (C) 2011-2015  Qianqian Fang <fangq at nmr.mgh.harvard.edu>\n*License: BSD License, see License_BSD.txt for details\n*Version: 1.0 (Optimus - Final)\n\n-------------------------------------------------------------------------------\n\nTable of Content:\n\nI.  Introduction\nII. Installation\nIII.Using JSONLab\nIV. Known Issues and TODOs\nV.  Contribution and feedback\n\n-------------------------------------------------------------------------------\n\nI.  Introduction\n\nJSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable, \nhuman-readable and \"[http://en.wikipedia.org/wiki/JSON fat-free]\" text format \nto represent complex and hierarchical data. It is as powerful as \n[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely \nused for data-exchange in applications, and is essential for the wild success \nof [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and \n[http://en.wikipedia.org/wiki/Web_2.0 Web2.0]. \n\nUBJSON (Universal Binary JSON) is a binary JSON format, specifically \noptimized for compact file size and better performance while keeping\nthe semantics as simple as the text-based JSON format. Using the UBJSON\nformat allows to wrap complex binary data in a flexible and extensible\nstructure, making it possible to process complex and large dataset \nwithout accuracy loss due to text conversions.\n\nWe envision that both JSON and its binary version will serve as part of \nthe mainstream data-exchange formats for scientific research in the future. \nIt will provide the flexibility and generality achieved by other popular \ngeneral-purpose file specifications, such as\n[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly \nreduced complexity and enhanced performance.\n\nJSONLab is a free and open-source implementation of a JSON/UBJSON encoder \nand a decoder in the native MATLAB language. It can be used to convert a MATLAB \ndata structure (array, struct, cell, struct array and cell array) into \nJSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB \ndata structure. JSONLab supports both MATLAB and  \n[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).\n\n-------------------------------------------------------------------------------\n\nII. Installation\n\nThe installation of JSONLab is no different than any other simple\nMATLAB toolbox. You only need to download/unzip the JSONLab package\nto a folder, and add the folder's path to MATLAB/Octave's path list\nby using the following command:\n\n    addpath('/path/to/jsonlab');\n\nIf you want to add this path permanently, you need to type \"pathtool\", \nbrowse to the jsonlab root folder and add to the list, then click \"Save\".\nThen, run \"rehash\" in MATLAB, and type \"which loadjson\", if you see an \noutput, that means JSONLab is installed for MATLAB/Octave.\n\n-------------------------------------------------------------------------------\n\nIII.Using JSONLab\n\nJSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder, \nand savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and \ntwo equivallent functions -- loadubjson and saveubjson for the binary \nJSON. The detailed help info for the four functions can be found below:\n\n=== loadjson.m ===\n<pre>\n  data=loadjson(fname,opt)\n     or\n  data=loadjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09, including previous works from \n \n          Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n             created on 2009/11/02\n          Franois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n             created on  2009/03/22\n          Joel Feenstra:\n          http://www.mathworks.com/matlabcentral/fileexchange/20565\n             created on 2008/07/03\n \n  $Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a JSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n                          speed-optimized array parser when loading an \n                          array object. The fast array parser may \n                          collapse block arrays into a single large\n                          array similar to rules defined in cell2mat; 0 to \n                          use a legacy parser; if set to a larger-than-1\n                          value, this option will specify the minimum\n                          dimension to enable the fast array parser. For\n                          example, if the input is a 3D array, setting\n                          FastArrayParser to 1 will return a 3D array;\n                          setting to 2 will return a cell array of 2D\n                          arrays; setting to 3 will return to a 2D cell\n                          array of 1D vectors; setting to 4 will return a\n                          3D cell array.\n            opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n       dat=loadjson(['examples' filesep 'example1.json'])\n       dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n</pre>\n\n=== savejson.m ===\n\n<pre>\n  json=savejson(rootname,obj,filename)\n     or\n  json=savejson(rootname,obj,opt)\n  json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n  Object Notation) string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09\n \n  $Id: savejson.m 458 2014-12-19 22:17:17Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array).\n       filename: a string for the file name to save the output JSON data.\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n                          of a 1D/2D array;\n         opt.ArrayIndent [1|0]: if 1, output explicit data array with\n                          precedent indentation; if 0, no indentation\n         opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n                          to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n                          and $1 represents the sign. For those who want to use\n                          1e999 to represent Inf, they can set opt.Inf to '$11e999'\n         opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n                          to represent NaN\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSONP='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n         opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n         opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a string in the JSON format (see http://json.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       savejson('jmesh',jsonmesh)\n       savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n </pre>\n\n=== loadubjson.m ===\n\n<pre>\n  data=loadubjson(fname,opt)\n     or\n  data=loadubjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/01\n \n  $Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a UBJSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.IntEndian [B|L]: specify the endianness of the integer fields\n                          in the UBJSON input data. B - Big-Endian format for \n                          integers (as required in the UBJSON specification); \n                          L - input integer fields are in Little-Endian order.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       obj=struct('string','value','array',[1 2 3]);\n       ubjdata=saveubjson('obj',obj);\n       dat=loadubjson(ubjdata)\n       dat=loadubjson(['examples' filesep 'example1.ubj'])\n       dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n</pre>\n\n=== saveubjson.m ===\n\n<pre>\n  json=saveubjson(rootname,obj,filename)\n     or\n  json=saveubjson(rootname,obj,opt)\n  json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a Universal \n  Binary JSON (UBJSON) binary string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/17\n \n  $Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array)\n       filename: a string for the file name to save the output UBJSON data\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSON='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a binary string in the UBJSON format (see http://ubjson.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       saveubjson('jsonmesh',jsonmesh)\n       saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n</pre>\n\n\n=== examples ===\n\nUnder the \"examples\" folder, you can find several scripts to demonstrate the\nbasic utilities of JSONLab. Running the \"demo_jsonlab_basic.m\" script, you \nwill see the conversions from MATLAB data structure to JSON text and backward.\nIn \"jsonlab_selftest.m\", we load complex JSON files downloaded from the Internet\nand validate the loadjson/savejson functions for regression testing purposes.\nSimilarly, a \"demo_ubjson_basic.m\" script is provided to test the saveubjson\nand loadubjson pairs for various matlab data structures.\n\nPlease run these examples and understand how JSONLab works before you use\nit to process your data.\n\n-------------------------------------------------------------------------------\n\nIV. Known Issues and TODOs\n\nJSONLab has several known limitations. We are striving to make it more general\nand robust. Hopefully in a few future releases, the limitations become less.\n\nHere are the known issues:\n\n# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;\n# When processing names containing multi-byte characters, Octave and MATLAB \\\ncan give different field-names; you can use feature('DefaultCharacterSet','latin1') \\\nin MATLAB to get consistant results\n# savejson can not handle class and dataset.\n# saveubjson converts a logical array into a uint8 ([U]) array\n# an unofficial N-D array count syntax is implemented in saveubjson. We are \\\nactively communicating with the UBJSON spec maintainer to investigate the \\\npossibility of making it upstream\n# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \\\nfiles, however, it can parse all UBJSON files produced by saveubjson.\n\n-------------------------------------------------------------------------------\n\nV. Contribution and feedback\n\nJSONLab is an open-source project. This means you can not only use it and modify\nit as you wish, but also you can contribute your changes back to JSONLab so\nthat everyone else can enjoy the improvement. For anyone who want to contribute,\nplease download JSONLab source code from it's subversion repository by using the\nfollowing command:\n\n svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab\n\nYou can make changes to the files as needed. Once you are satisfied with your\nchanges, and ready to share it with others, please cd the root directory of \nJSONLab, and type\n\n svn diff > yourname_featurename.patch\n\nYou then email the .patch file to JSONLab's maintainer, Qianqian Fang, at\nthe email address shown in the beginning of this file. Qianqian will review \nthe changes and commit it to the subversion if they are satisfactory.\n\nWe appreciate any suggestions and feedbacks from you. Please use iso2mesh's\nmailing list to report any questions you may have with JSONLab:\n\nhttp://groups.google.com/group/iso2mesh-users?hl=en&pli=1\n\n(Subscription to the mailing list is needed in order to post messages).\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/jsonopt.m",
    "content": "function val=jsonopt(key,default,varargin)\n%\n% val=jsonopt(key,default,optstruct)\n%\n% setting options based on a struct. The struct can be produced\n% by varargin2struct from a list of 'param','value' pairs\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n%\n% $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $\n%\n% input:\n%      key: a string with which one look up a value from a struct\n%      default: if the key does not exist, return default\n%      optstruct: a struct where each sub-field is a key \n%\n% output:\n%      val: if key exists, val=optstruct.key; otherwise val=default\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n% \n\nval=default;\nif(nargin<=2) return; end\nopt=varargin{1};\nif(isstruct(opt) && isfield(opt,key))\n    val=getfield(opt,key);\nend\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/loadjson.m",
    "content": "function data = loadjson(fname,varargin)\n%\n% data=loadjson(fname,opt)\n%    or\n% data=loadjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09, including previous works from \n%\n%         Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n%            created on 2009/11/02\n%         François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n%            created on  2009/03/22\n%         Joel Feenstra:\n%         http://www.mathworks.com/matlabcentral/fileexchange/20565\n%            created on 2008/07/03\n%\n% $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a JSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n%                         speed-optimized array parser when loading an \n%                         array object. The fast array parser may \n%                         collapse block arrays into a single large\n%                         array similar to rules defined in cell2mat; 0 to \n%                         use a legacy parser; if set to a larger-than-1\n%                         value, this option will specify the minimum\n%                         dimension to enable the fast array parser. For\n%                         example, if the input is a 3D array, setting\n%                         FastArrayParser to 1 will return a 3D array;\n%                         setting to 2 will return a cell array of 2D\n%                         arrays; setting to 3 will return to a 2D cell\n%                         array of 1D vectors; setting to 4 will return a\n%                         3D cell array.\n%           opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n%      dat=loadjson(['examples' filesep 'example1.json'])\n%      dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\n\nif(jsonopt('ShowProgress',0,opt)==1)\n    opt.progressbar_=waitbar(0,'loading ...');\nend\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\nif(isfield(opt,'progressbar_'))\n    close(opt.progressbar_);\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=data(j).x0x5F_ArraySize_;\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    if next_char ~= '}'\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            parse_char(':');\n            val = parse_value(varargin{:});\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}'\n                break;\n            end\n            parse_char(',');\n        end\n    end\n    parse_char('}');\n\n%%-------------------------------------------------------------------------\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim2=[];\n    arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:});\n    pbar=jsonopt('progressbar_',-1,varargin{:});\n\n    if next_char ~= ']'\n\tif(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))\n            [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos);\n            arraystr=['[' inStr(pos:endpos)];\n            arraystr=regexprep(arraystr,'\"_NaN_\"','NaN');\n            arraystr=regexprep(arraystr,'\"([-+]*)_Inf_\"','$1Inf');\n            arraystr(arraystr==sprintf('\\n'))=[];\n            arraystr(arraystr==sprintf('\\r'))=[];\n            %arraystr=regexprep(arraystr,'\\s*,',','); % this is slow,sometimes needed\n            if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D\n        \tastr=inStr((e1l+1):(e1r-1));\n        \tastr=regexprep(astr,'\"_NaN_\"','NaN');\n        \tastr=regexprep(astr,'\"([-+]*)_Inf_\"','$1Inf');\n        \tastr(astr==sprintf('\\n'))=[];\n        \tastr(astr==sprintf('\\r'))=[];\n        \tastr(astr==' ')='';\n        \tif(isempty(find(astr=='[', 1))) % array is 2D\n                    dim2=length(sscanf(astr,'%f,',[1 inf]));\n        \tend\n            else % array is 1D\n        \tastr=arraystr(2:end-1);\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]);\n        \tif(nextidx>=length(astr)-1)\n                    object=obj;\n                    pos=endpos;\n                    parse_char(']');\n                    return;\n        \tend\n            end\n            if(~isempty(dim2))\n        \tastr=arraystr;\n        \tastr(astr=='[')='';\n        \tastr(astr==']')='';\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf);\n        \tif(nextidx>=length(astr)-1)\n                    object=reshape(obj,dim2,numel(obj)/dim2)';\n                    pos=endpos;\n                    parse_char(']');\n                    if(pbar>0)\n                        waitbar(pos/length(inStr),pbar,'loading ...');\n                    end\n                    return;\n        \tend\n            end\n            arraystr=regexprep(arraystr,'\\]\\s*,','];');\n\telse\n            arraystr='[';\n\tend\n        try\n           if(isoct && regexp(arraystr,'\"','once'))\n                error('Octave eval can produce empty cells for JSON-like input');\n           end\n           object=eval(arraystr);\n           pos=endpos;\n        catch\n         while 1\n            newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1);\n            val = parse_value(newopt);\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            parse_char(',');\n         end\n        end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    parse_char(']');\n    \n    if(pbar>0)\n        waitbar(pos/length(inStr),pbar,'loading ...');\n    end\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr len  esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    if inStr(pos) ~= '\"'\n        error_pos('String starting with \" expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    str = '';\n    while pos <= len\n        while index_esc <= len_esc && esc(index_esc) < pos\n            index_esc = index_esc + 1;\n        end\n        if index_esc > len_esc\n            str = [str inStr(pos:len)];\n            pos = len + 1;\n            break;\n        else\n            str = [str inStr(pos:esc(index_esc)-1)];\n            pos = esc(index_esc);\n        end\n        nstr = length(str); switch inStr(pos)\n            case '\"'\n                pos = pos + 1;\n                if(~isempty(str))\n                    if(strcmp(str,'_Inf_'))\n                        str=Inf;\n                    elseif(strcmp(str,'-_Inf_'))\n                        str=-Inf;\n                    elseif(strcmp(str,'_NaN_'))\n                        str=NaN;\n                    end\n                end\n                return;\n            case '\\'\n                if pos+1 > len\n                    error_pos('End of file reached right after escape character');\n                end\n                pos = pos + 1;\n                switch inStr(pos)\n                    case {'\"' '\\' '/'}\n                        str(nstr+1) = inStr(pos);\n                        pos = pos + 1;\n                    case {'b' 'f' 'n' 'r' 't'}\n                        str(nstr+1) = sprintf(['\\' inStr(pos)]);\n                        pos = pos + 1;\n                    case 'u'\n                        if pos+4 > len\n                            error_pos('End of file reached in escaped unicode character');\n                        end\n                        str(nstr+(1:6)) = inStr(pos-1:pos+4);\n                        pos = pos + 5;\n                end\n            otherwise % should never happen\n                str(nstr+1) = inStr(pos), keyboard\n                pos = pos + 1;\n        end\n    end\n    error_pos('End of file while expecting end of inStr');\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct\n    currstr=inStr(pos:end);\n    numstr=0;\n    if(isoct~=0)\n        numstr=regexp(currstr,'^\\s*-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+\\-]?\\d+)?','end');\n        [num, one] = sscanf(currstr, '%f', 1);\n        delta=numstr+1;\n    else\n        [num, one, err, delta] = sscanf(currstr, '%f', 1);\n        if ~isempty(err)\n            error_pos('Error reading number at position %d');\n        end\n    end\n    pos = pos + delta-1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n    \n    pbar=jsonopt('progressbar_',-1,varargin{:});\n    if(pbar>0)\n        waitbar(pos/len,pbar,'loading ...');\n    end\n    \n    switch(inStr(pos))\n        case '\"'\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'-','0','1','2','3','4','5','6','7','8','9'}\n            val = parse_number(varargin{:});\n            return;\n        case 't'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true')\n                val = true;\n                pos = pos + 4;\n                return;\n            end\n        case 'f'\n            if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false')\n                val = false;\n                pos = pos + 5;\n                return;\n            end\n        case 'n'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null')\n                val = [];\n                pos = pos + 4;\n                return;\n            end\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/loadubjson.m",
    "content": "function data = loadubjson(fname,varargin)\n%\n% data=loadubjson(fname,opt)\n%    or\n% data=loadubjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/01\n%\n% $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a UBJSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.IntEndian [B|L]: specify the endianness of the integer fields\n%                         in the UBJSON input data. B - Big-Endian format for \n%                         integers (as required in the UBJSON specification); \n%                         L - input integer fields are in Little-Endian order.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      obj=struct('string','value','array',[1 2 3]);\n%      ubjdata=saveubjson('obj',obj);\n%      dat=loadubjson(ubjdata)\n%      dat=loadubjson(['examples' filesep 'example1.ubj'])\n%      dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken fileendian systemendian\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\nfileendian=upper(jsonopt('IntEndian','B',opt));\n[os,maxelem,systemendian]=computer;\n\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\n\n\n%%\nfunction newdata=parse_collection(id,data,obj)\n\nif(jsoncount>0 && exist('data','var')) \n    if(~iscell(data))\n       newdata=cell(1);\n       newdata{1}=data;\n       data=newdata;\n    end\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=double(data(j).x0x5F_ArraySize_);\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1); % TODO\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        count=double(parse_number());\n    end\n    if next_char ~= '}'\n        num=0;\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            %parse_char(':');\n            val = parse_value(varargin{:});\n            num=num+1;\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}' || (count>=0 && num>=count)\n                break;\n            end\n            %parse_char(',');\n        end\n    end\n    if(count==-1)\n        parse_char('}');\n    end\n\n%%-------------------------------------------------------------------------\nfunction [cid,len]=elem_info(type)\nid=strfind('iUIlLdD',type);\ndataclass={'int8','uint8','int16','int32','int64','single','double'};\nbytelen=[1,1,2,4,8,4,8];\nif(id>0)\n    cid=dataclass{id};\n    len=bytelen(id);\nelse\n    error_pos('unsupported type at position %d');\nend\n%%-------------------------------------------------------------------------\n\n\nfunction [data adv]=parse_block(type,count,varargin)\nglobal pos inStr isoct fileendian systemendian\n[cid,len]=elem_info(type);\ndatastr=inStr(pos:pos+len*count-1);\nif(isoct)\n    newdata=int8(datastr);\nelse\n    newdata=uint8(datastr);\nend\nid=strfind('iUIlLdD',type);\nif(id<=5 && fileendian~=systemendian)\n    newdata=swapbytes(typecast(newdata,cid));\nend\ndata=typecast(newdata,cid);\nadv=double(len*count);\n\n%%-------------------------------------------------------------------------\n\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim=[];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1);\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        if(next_char=='[')\n            dim=parse_array(varargin{:});\n            count=prod(double(dim));\n        else\n            count=double(parse_number());\n        end\n    end\n    if(~isempty(type))\n        if(count>=0)\n            [object adv]=parse_block(type,count,varargin{:});\n            if(~isempty(dim))\n                object=reshape(object,dim);\n            end\n            pos=pos+adv;\n            return;\n        else\n            endpos=matching_bracket(inStr,pos);\n            [cid,len]=elem_info(type);\n            count=(endpos-pos)/len;\n            [object adv]=parse_block(type,count,varargin{:});\n            pos=pos+adv;\n            parse_char(']');\n            return;\n        end\n    end\n    if next_char ~= ']'\n         while 1\n            val = parse_value(varargin{:});\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            %parse_char(',');\n         end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    if(count==-1)\n        parse_char(']');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    type=inStr(pos);\n    if type ~= 'S' && type ~= 'C' && type ~= 'H'\n        error_pos('String starting with S expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    if(type == 'C')\n        str=inStr(pos);\n        pos=pos+1;\n        return;\n    end\n    bytelen=double(parse_number());\n    if(length(inStr)>=pos+bytelen-1)\n        str=inStr(pos:pos+bytelen-1);\n        pos=pos+bytelen;\n    else\n        error_pos('End of file while expecting end of inStr');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct fileendian systemendian\n    id=strfind('iUIlLdD',inStr(pos));\n    if(isempty(id))\n        error_pos('expecting a number at position %d');\n    end\n    type={'int8','uint8','int16','int32','int64','single','double'};\n    bytelen=[1,1,2,4,8,4,8];\n    datastr=inStr(pos+1:pos+bytelen(id));\n    if(isoct)\n        newdata=int8(datastr);\n    else\n        newdata=uint8(datastr);\n    end\n    if(id<=5 && fileendian~=systemendian)\n        newdata=swapbytes(typecast(newdata,type{id}));\n    end\n    num=typecast(newdata,type{id});\n    pos = pos + bytelen(id)+1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n\n    switch(inStr(pos))\n        case {'S','C','H'}\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'i','U','I','l','L','d','D'}\n            val = parse_number(varargin{:});\n            return;\n        case 'T'\n            val = true;\n            pos = pos + 1;\n            return;\n        case 'F'\n            val = false;\n            pos = pos + 1;\n            return;\n        case {'Z','N'}\n            val = [];\n            pos = pos + 1;\n            return;\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos e1l e1r maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/mergestruct.m",
    "content": "function s=mergestruct(s1,s2)\n%\n% s=mergestruct(s1,s2)\n%\n% merge two struct objects into one\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      s1,s2: a struct object, s1 and s2 can not be arrays\n%\n% output:\n%      s: the merged struct object. fields in s1 and s2 will be combined in s.\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(~isstruct(s1) || ~isstruct(s2))\n    error('input parameters contain non-struct');\nend\nif(length(s1)>1 || length(s2)>1)\n    error('can not merge struct arrays');\nend\nfn=fieldnames(s2);\ns=s1;\nfor i=1:length(fn)              \n    s=setfield(s,fn{i},getfield(s2,fn{i}));\nend\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/savejson.m",
    "content": "function json=savejson(rootname,obj,varargin)\n%\n% json=savejson(rootname,obj,filename)\n%    or\n% json=savejson(rootname,obj,opt)\n% json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n% Object Notation) string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09\n%\n% $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array).\n%      filename: a string for the file name to save the output JSON data.\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n%                         of a 1D/2D array;\n%        opt.ArrayIndent [1|0]: if 1, output explicit data array with\n%                         precedent indentation; if 0, no indentation\n%        opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n%                         to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n%                         and $1 represents the sign. For those who want to use\n%                         1e999 to represent Inf, they can set opt.Inf to '$11e999'\n%        opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n%                         to represent NaN\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSONP='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%        opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n%        opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a string in the JSON format (see http://json.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      savejson('jmesh',jsonmesh)\n%      savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\n\nwhitespaces=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nif(jsonopt('Compact',0,opt)==1)\n    whitespaces=struct('tab','','newline','','sep',',');\nend\nif(~isfield(opt,'whitespaces_'))\n    opt.whitespaces_=whitespaces;\nend\n\nnl=whitespaces.newline;\n\njson=obj2json(rootname,obj,rootlevel,opt);\nif(rootisarray)\n    json=sprintf('%s%s',json,nl);\nelse\n    json=sprintf('{%s%s%s}\\n',nl,json,nl);\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=sprintf('%s(%s);%s',jsonp,json,nl);\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    if(jsonopt('SaveBinary',0,opt)==1)\n\t    fid = fopen(opt.FileName, 'wb');\n\t    fwrite(fid,json);\n    else\n\t    fid = fopen(opt.FileName, 'wt');\n\t    fwrite(fid,json,'char');\n    end\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2json(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2json(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2json(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2json(name,item,level,varargin{:});\nelse\n    txt=mat2json(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2json(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=jsonopt('whitespaces_',struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n')),varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nif(len>1)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": [%s',padding0, checkname(name,varargin{:}),nl); name=''; \n    else\n        txt=sprintf('%s[%s',padding0,nl); \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": []',padding0, checkname(name,varargin{:})); name=''; \n    else\n        txt=sprintf('%s[]',padding0); \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n    for i=1:dim(1)\n       txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:}));\n       if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    end\n    if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n    if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2json(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\npadding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1));\nnl=ws.newline;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding0,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding0,nl); end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n  for i=1:dim(1)\n    names = fieldnames(item(i,j));\n    if(~isempty(name) && len==1)\n        txt=sprintf('%s%s\"%s\": {%s',txt,padding1, checkname(name,varargin{:}),nl); \n    else\n        txt=sprintf('%s%s{%s',txt,padding1,nl); \n    end\n    if(~isempty(names))\n      for e=1:length(names)\n\t    txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:}));\n        if(e<length(names)) txt=sprintf('%s%s',txt,','); end\n        txt=sprintf('%s%s',txt,nl);\n      end\n    end\n    txt=sprintf('%s%s}',txt,padding1);\n    if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n  end\n  if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n  if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2json(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding1,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding1,nl); end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    if(isoct)\n        val=regexprep(item(e,:),'\\\\','\\\\');\n        val=regexprep(val,'\"','\\\"');\n        val=regexprep(val,'^\"','\\\"');\n    else\n        val=regexprep(item(e,:),'\\\\','\\\\\\\\');\n        val=regexprep(val,'\"','\\\\\"');\n        val=regexprep(val,'^\"','\\\\\"');\n    end\n    val=escapejsonstring(val);\n    if(len==1)\n        obj=['\"' checkname(name,varargin{:}) '\": ' '\"',val,'\"'];\n\tif(isempty(name)) obj=['\"',val,'\"']; end\n        txt=sprintf('%s%s%s%s',txt,padding1,obj);\n    else\n        txt=sprintf('%s%s%s%s',txt,padding0,['\"',val,'\"']);\n    end\n    if(e==len) sep=''; end\n    txt=sprintf('%s%s',txt,sep);\nend\nif(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2json(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:}))\n    if(isempty(name))\n    \ttxt=sprintf('%s{%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    else\n    \ttxt=sprintf('%s\"%s\": {%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    end\nelse\n    if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0)\n        numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\\[',''),']','');\n    else\n        numtxt=matdata2json(item,level+1,varargin{:});\n    end\n    if(isempty(name))\n    \ttxt=sprintf('%s%s',padding1,numtxt);\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n           \ttxt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        else\n    \t    txt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        end\n    end\n    return;\nend\ndataformat='%s%s%s%s%s';\n\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n    end\n    txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsSparse_\": ','1', sep);\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([iy(:),data'],level+2,varargin{:}), nl);\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,data],level+2,varargin{:}), nl);\n    else\n        % General case, store row and column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,iy,data],level+2,varargin{:}), nl);\n    end\nelse\n    if(isreal(item))\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json(item(:)',level+2,varargin{:}), nl);\n    else\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl);\n    end\nend\ntxt=sprintf('%s%s%s',txt,padding1,'}');\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2json(mat,level,varargin)\n\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\ntab=ws.tab;\nnl=ws.newline;\n\nif(size(mat,1)==1)\n    pre='';\n    post='';\n    level=level-1;\nelse\n    pre=sprintf('[%s',nl);\n    post=sprintf('%s%s]',nl,repmat(tab,1,level-1));\nend\n\nif(isempty(mat))\n    txt='null';\n    return;\nend\nfloatformat=jsonopt('FloatFormat','%.10g',varargin{:});\n%if(numel(mat)>1)\n    formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]];\n%else\n%    formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\\n')]];\n%end\n\nif(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1)\n    formatstr=[repmat(tab,1,level) formatstr];\nend\n\ntxt=sprintf(formatstr,mat');\ntxt(end-length(nl):end)=[];\nif(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1)\n   txt=regexprep(txt,'1','true');\n   txt=regexprep(txt,'0','false');\nend\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],\\n['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\ntxt=[pre txt post];\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n\n%%-------------------------------------------------------------------------\nfunction newstr=escapejsonstring(str)\nnewstr=str;\nisoct=exist('OCTAVE_VERSION','builtin');\nif(isoct)\n   vv=sscanf(OCTAVE_VERSION,'%f');\n   if(vv(1)>=3.8) isoct=0; end\nend\nif(isoct)\n  escapechars={'\\a','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},escapechars{i});\n  end\nelse\n  escapechars={'\\a','\\b','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\\\','\\\\\\\\'));\n  end\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/saveubjson.m",
    "content": "function json=saveubjson(rootname,obj,varargin)\n%\n% json=saveubjson(rootname,obj,filename)\n%    or\n% json=saveubjson(rootname,obj,opt)\n% json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a Universal \n% Binary JSON (UBJSON) binary string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/17\n%\n% $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array)\n%      filename: a string for the file name to save the output UBJSON data\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSON='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a binary string in the UBJSON format (see http://ubjson.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      saveubjson('jsonmesh',jsonmesh)\n%      saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\njson=obj2ubjson(rootname,obj,rootlevel,opt);\nif(~rootisarray)\n    json=['{' json '}'];\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=[jsonp '(' json ')'];\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    fid = fopen(opt.FileName, 'wb');\n    fwrite(fid,json);\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2ubjson(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2ubjson(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2ubjson(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2ubjson(name,item,level,varargin{:});\nelse\n    txt=mat2ubjson(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2ubjson(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item); % let's handle 1D cell first\nif(len>1) \n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) '[']; name=''; \n    else\n        txt='['; \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; \n    else\n        txt='Z'; \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=[txt '[']; end\n    for i=1:dim(1)\n       txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})];\n    end\n    if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2ubjson(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=[txt '[']; end\n  for i=1:dim(1)\n     names = fieldnames(item(i,j));\n     if(~isempty(name) && len==1)\n        txt=[txt S_(checkname(name,varargin{:})) '{']; \n     else\n        txt=[txt '{']; \n     end\n     if(~isempty(names))\n       for e=1:length(names)\n\t     txt=[txt obj2ubjson(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})];\n       end\n     end\n     txt=[txt '}'];\n  end\n  if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2ubjson(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    val=item(e,:);\n    if(len==1)\n        obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),''];\n\tif(isempty(name)) obj=['',S_(val),'']; end\n        txt=[txt,'',obj];\n    else\n        txt=[txt,'',['',S_(val),'']];\n    end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2ubjson(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) || jsonopt('ArrayToStruct',0,varargin{:}))\n      cid=I_(uint32(max(size(item))));\n      if(isempty(name))\n    \ttxt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ];\n      else\n          if(isempty(item))\n              txt=[S_(checkname(name,varargin{:})),'Z'];\n              return;\n          else\n    \t      txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))];\n          end\n      end\nelse\n    if(isempty(name))\n    \ttxt=matdata2ubjson(item,level+1,varargin{:});\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n            numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\\[',''),']','');\n           \ttxt=[S_(checkname(name,varargin{:})) numtxt];\n        else\n    \t    txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})];\n        end\n    end\n    return;\nend\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=[txt,S_('_ArrayIsComplex_'),'T'];\n    end\n    txt=[txt,S_('_ArrayIsSparse_'),'T'];\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([iy(:),data'],level+2,varargin{:})];\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,data],level+2,varargin{:})];\n    else\n        % General case, store row and column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,iy,data],level+2,varargin{:})];\n    end\nelse\n    if(isreal(item))\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson(item(:)',level+2,varargin{:})];\n    else\n        txt=[txt,S_('_ArrayIsComplex_'),'T'];\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})];\n    end\nend\ntxt=[txt,'}'];\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2ubjson(mat,level,varargin)\nif(isempty(mat))\n    txt='Z';\n    return;\nend\nif(size(mat,1)==1)\n    level=level-1;\nend\ntype='';\nhasnegtive=(mat<0);\nif(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0)))\n    if(isempty(hasnegtive))\n       if(max(mat(:))<=2^8)\n           type='U';\n       end\n    end\n    if(isempty(type))\n        % todo - need to consider negative ones separately\n        id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]);\n        if(isempty(find(id)))\n            error('high-precision data is not yet supported');\n        end\n        key='iIlL';\n\ttype=key(find(id));\n    end\n    txt=[I_a(mat(:),type,size(mat))];\nelseif(islogical(mat))\n    logicalval='FT';\n    if(numel(mat)==1)\n        txt=logicalval(mat+1);\n    else\n        txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')];\n    end\nelse\n    if(numel(mat)==1)\n        txt=['[' D_(mat) ']'];\n    else\n        txt=D_a(mat(:),'D',size(mat));\n    end\nend\n\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n%%-------------------------------------------------------------------------\nfunction val=S_(str)\nif(length(str)==1)\n  val=['C' str];\nelse\n  val=['S' I_(int32(length(str))) str];\nend\n%%-------------------------------------------------------------------------\nfunction val=I_(num)\nif(~isinteger(num))\n    error('input is not an integer');\nend\nif(num>=0 && num<255)\n   val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')];\n   return;\nend\nkey='iIlL';\ncid={'int8','int16','int32','int64'};\nfor i=1:4\n  if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1)))\n    val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')];\n    return;\n  end\nend\nerror('unsupported integer');\n\n%%-------------------------------------------------------------------------\nfunction val=D_(num)\nif(~isfloat(num))\n    error('input is not a float');\nend\n\nif(isa(num,'single'))\n  val=['d' data2byte(num,'uint8')];\nelse\n  val=['D' data2byte(num,'uint8')];\nend\n%%-------------------------------------------------------------------------\nfunction data=I_a(num,type,dim,format)\nid=find(ismember('iUIlL',type));\n\nif(id==0)\n  error('unsupported integer array');\nend\n\n% based on UBJSON specs, all integer types are stored in big endian format\n\nif(id==1)\n  data=data2byte(swapbytes(int8(num)),'uint8');\n  blen=1;\nelseif(id==2)\n  data=data2byte(swapbytes(uint8(num)),'uint8');\n  blen=1;\nelseif(id==3)\n  data=data2byte(swapbytes(int16(num)),'uint8');\n  blen=2;\nelseif(id==4)\n  data=data2byte(swapbytes(int32(num)),'uint8');\n  blen=4;\nelseif(id==5)\n  data=data2byte(swapbytes(int64(num)),'uint8');\n  blen=8;\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/blen)) data(:)'];\n  end\n  data=['[' data(:)'];\nelse\n  data=reshape(data,blen,numel(data)/blen);\n  data(2:blen+1,:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction data=D_a(num,type,dim,format)\nid=find(ismember('dD',type));\n\nif(id==0)\n  error('unsupported float array');\nend\n\nif(id==1)\n  data=data2byte(single(num),'uint8');\nelseif(id==2)\n  data=data2byte(double(num),'uint8');\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)'];\n  end\n  data=['[' data];\nelse\n  data=reshape(data,(id*4),length(data)/(id*4));\n  data(2:(id*4+1),:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction bytes=data2byte(varargin)\nbytes=typecast(varargin{:});\nbytes=bytes(:)';\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/jsonlab/varargin2struct.m",
    "content": "function opt=varargin2struct(varargin)\n%\n% opt=varargin2struct('param1',value1,'param2',value2,...)\n%   or\n% opt=varargin2struct(...,optstruct,...)\n%\n% convert a series of input parameters into a structure\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      'param', value: the input parameters should be pairs of a string and a value\n%       optstruct: if a parameter is a struct, the fields will be merged to the output struct\n%\n% output:\n%      opt: a struct where opt.param1=value1, opt.param2=value2 ...\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nlen=length(varargin);\nopt=struct;\nif(len==0) return; end\ni=1;\nwhile(i<=len)\n    if(isstruct(varargin{i}))\n        opt=mergestruct(opt,varargin{i});\n    elseif(ischar(varargin{i}) && i<len)\n        opt=setfield(opt,varargin{i},varargin{i+1});\n        i=i+1;\n    else\n        error('input must be in the form of ...,''name'',value,... pairs or structs');\n    end\n    i=i+1;\nend\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/makeValidFieldName.m",
    "content": "function str = makeValidFieldName(str)\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    isoct=exist('OCTAVE_VERSION','builtin');\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n"
  },
  {
    "path": "machine-learning-ex6/ex6/lib/submitWithConfiguration.m",
    "content": "function submitWithConfiguration(conf)\n  addpath('./lib/jsonlab');\n\n  parts = parts(conf);\n\n  fprintf('== Submitting solutions | %s...\\n', conf.itemName);\n\n  tokenFile = 'token.mat';\n  if exist(tokenFile, 'file')\n    load(tokenFile);\n    [email token] = promptToken(email, token, tokenFile);\n  else\n    [email token] = promptToken('', '', tokenFile);\n  end\n\n  if isempty(token)\n    fprintf('!! Submission Cancelled\\n');\n    return\n  end\n\n  try\n    response = submitParts(conf, email, token, parts);\n  catch\n    e = lasterror();\n    fprintf( ...\n      '!! Submission failed: unexpected error: %s\\n', ...\n      e.message);\n    fprintf('!! Please try again later.\\n');\n    return\n  end\n\n  if isfield(response, 'errorMessage')\n    fprintf('!! Submission failed: %s\\n', response.errorMessage);\n  else\n    showFeedback(parts, response);\n    save(tokenFile, 'email', 'token');\n  end\nend\n\nfunction [email token] = promptToken(email, existingToken, tokenFile)\n  if (~isempty(email) && ~isempty(existingToken))\n    prompt = sprintf( ...\n      'Use token from last successful submission (%s)? (Y/n): ', ...\n      email);\n    reenter = input(prompt, 's');\n\n    if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y')\n      token = existingToken;\n      return;\n    else\n      delete(tokenFile);\n    end\n  end\n  email = input('Login (email address): ', 's');\n  token = input('Token: ', 's');\nend\n\nfunction isValid = isValidPartOptionIndex(partOptions, i)\n  isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions));\nend\n\nfunction response = submitParts(conf, email, token, parts)\n  body = makePostBody(conf, email, token, parts);\n  submissionUrl = submissionUrl();\n  params = {'jsonBody', body};\n  responseBody = urlread(submissionUrl, 'post', params);\n  response = loadjson(responseBody);\nend\n\nfunction body = makePostBody(conf, email, token, parts)\n  bodyStruct.assignmentSlug = conf.assignmentSlug;\n  bodyStruct.submitterEmail = email;\n  bodyStruct.secret = token;\n  bodyStruct.parts = makePartsStruct(conf, parts);\n\n  opt.Compact = 1;\n  body = savejson('', bodyStruct, opt);\nend\n\nfunction partsStruct = makePartsStruct(conf, parts)\n  for part = parts\n    partId = part{:}.id;\n    fieldName = makeValidFieldName(partId);\n    outputStruct.output = conf.output(partId);\n    partsStruct.(fieldName) = outputStruct;\n  end\nend\n\nfunction [parts] = parts(conf)\n  parts = {};\n  for partArray = conf.partArrays\n    part.id = partArray{:}{1};\n    part.sourceFiles = partArray{:}{2};\n    part.name = partArray{:}{3};\n    parts{end + 1} = part;\n  end\nend\n\nfunction showFeedback(parts, response)\n  fprintf('== \\n');\n  fprintf('== %43s | %9s | %-s\\n', 'Part Name', 'Score', 'Feedback');\n  fprintf('== %43s | %9s | %-s\\n', '---------', '-----', '--------');\n  for part = parts\n    score = '';\n    partFeedback = '';\n    partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id));\n    partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id));\n    score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore);\n    fprintf('== %43s | %9s | %-s\\n', part{:}.name, score, partFeedback);\n  end\n  evaluation = response.evaluation;\n  totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore);\n  fprintf('==                                   --------------------------------\\n');\n  fprintf('== %43s | %9s | %-s\\n', '', totalScore, '');\n  fprintf('== \\n');\nend\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%\n% Service configuration\n%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\nfunction submissionUrl = submissionUrl()\n  submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1';\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/linearKernel.m",
    "content": "function sim = linearKernel(x1, x2)\n%LINEARKERNEL returns a linear kernel between x1 and x2\n%   sim = linearKernel(x1, x2) returns a linear kernel between x1 and x2\n%   and returns the value in sim\n\n% Ensure that x1 and x2 are column vectors\nx1 = x1(:); x2 = x2(:);\n\n% Compute the kernel\nsim = x1' * x2;  % dot product\n\nend"
  },
  {
    "path": "machine-learning-ex6/ex6/plotData.m",
    "content": "function plotData(X, y)\n%PLOTDATA Plots the data points X and y into a new figure \n%   PLOTDATA(x,y) plots the data points with + for the positive examples\n%   and o for the negative examples. X is assumed to be a Mx2 matrix.\n%\n% Note: This was slightly modified such that it expects y = 1 or y = 0\n\n% Find Indices of Positive and Negative Examples\npos = find(y == 1); neg = find(y == 0);\n\n% Plot Examples\nplot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 1, 'MarkerSize', 7)\nhold on;\nplot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7)\nhold off;\n\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/porterStemmer.m",
    "content": "function stem = porterStemmer(inString)\n% Applies the Porter Stemming algorithm as presented in the following\n% paper:\n% Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,\n%   no. 3, pp 130-137\n\n% Original code modeled after the C version provided at:\n% http://www.tartarus.org/~martin/PorterStemmer/c.txt\n\n% The main part of the stemming algorithm starts here. b is an array of\n% characters, holding the word to be stemmed. The letters are in b[k0],\n% b[k0+1] ending at b[k]. In fact k0 = 1 in this demo program (since\n% matlab begins indexing by 1 instead of 0). k is readjusted downwards as\n% the stemming progresses. Zero termination is not in fact used in the\n% algorithm.\n\n% To call this function, use the string to be stemmed as the input\n% argument.  This function returns the stemmed word as a string.\n\n% Lower-case string\ninString = lower(inString);\n\nglobal j;\nb = inString;\nk = length(b);\nk0 = 1;\nj = k;\n\n\n\n% With this if statement, strings of length 1 or 2 don't go through the\n% stemming process. Remove this conditional to match the published\n% algorithm.\nstem = b;\nif k > 2\n    % Output displays per step are commented out.\n    %disp(sprintf('Word to stem: %s', b));\n    x = step1ab(b, k, k0);\n    %disp(sprintf('Steps 1A and B yield: %s', x{1}));\n    x = step1c(x{1}, x{2}, k0);\n    %disp(sprintf('Step 1C yields: %s', x{1}));\n    x = step2(x{1}, x{2}, k0);\n    %disp(sprintf('Step 2 yields: %s', x{1}));\n    x = step3(x{1}, x{2}, k0);\n    %disp(sprintf('Step 3 yields: %s', x{1}));\n    x = step4(x{1}, x{2}, k0);\n    %disp(sprintf('Step 4 yields: %s', x{1}));\n    x = step5(x{1}, x{2}, k0);\n    %disp(sprintf('Step 5 yields: %s', x{1}));\n    stem = x{1};\nend\n\n% cons(j) is TRUE <=> b[j] is a consonant.\nfunction c = cons(i, b, k0)\nc = true;\nswitch(b(i))\n    case {'a', 'e', 'i', 'o', 'u'}\n        c = false;\n    case 'y'\n        if i == k0\n            c = true;\n        else\n            c = ~cons(i - 1, b, k0);\n        end\nend\n\n% mseq() measures the number of consonant sequences between k0 and j.  If\n% c is a consonant sequence and v a vowel sequence, and <..> indicates\n% arbitrary presence,\n\n%      <c><v>       gives 0\n%      <c>vc<v>     gives 1\n%      <c>vcvc<v>   gives 2\n%      <c>vcvcvc<v> gives 3\n%      ....\nfunction n = measure(b, k0)\nglobal j;\nn = 0;\ni = k0;\nwhile true\n    if i > j\n        return\n    end\n    if ~cons(i, b, k0)\n        break;\n    end\n    i = i + 1;\nend\ni = i + 1;\nwhile true\n    while true\n        if i > j\n            return\n        end\n        if cons(i, b, k0)\n            break;\n        end\n        i = i + 1;\n    end\n    i = i + 1;\n    n = n + 1;\n    while true\n        if i > j\n            return\n        end\n        if ~cons(i, b, k0)\n            break;\n        end\n        i = i + 1;\n    end\n    i = i + 1;\nend\n\n\n% vowelinstem() is TRUE <=> k0,...j contains a vowel\nfunction vis = vowelinstem(b, k0)\nglobal j;\nfor i = k0:j,\n    if ~cons(i, b, k0)\n        vis = true;\n        return\n    end\nend\nvis = false;\n\n%doublec(i) is TRUE <=> i,(i-1) contain a double consonant.\nfunction dc = doublec(i, b, k0)\nif i < k0+1\n    dc = false;\n    return\nend\nif b(i) ~= b(i-1)\n    dc = false;\n    return\nend\ndc = cons(i, b, k0);\n\n\n% cvc(j) is TRUE <=> j-2,j-1,j has the form consonant - vowel - consonant\n% and also if the second c is not w,x or y. this is used when trying to\n% restore an e at the end of a short word. e.g.\n%\n%      cav(e), lov(e), hop(e), crim(e), but\n%      snow, box, tray.\n\nfunction c1 = cvc(i, b, k0)\nif ((i < (k0+2)) || ~cons(i, b, k0) || cons(i-1, b, k0) || ~cons(i-2, b, k0))\n    c1 = false;\nelse\n    if (b(i) == 'w' || b(i) == 'x' || b(i) == 'y')\n        c1 = false;\n        return\n    end\n    c1 = true;\nend\n\n% ends(s) is TRUE <=> k0,...k ends with the string s.\nfunction s = ends(str, b, k)\nglobal j;\nif (str(length(str)) ~= b(k))\n    s = false;\n    return\nend % tiny speed-up\nif (length(str) > k)\n    s = false;\n    return\nend\nif strcmp(b(k-length(str)+1:k), str)\n    s = true;\n    j = k - length(str);\n    return\nelse\n    s = false;\nend\n\n% setto(s) sets (j+1),...k to the characters in the string s, readjusting\n% k accordingly.\n\nfunction so = setto(s, b, k)\nglobal j;\nfor i = j+1:(j+length(s))\n    b(i) = s(i-j);\nend\nif k > j+length(s)\n    b((j+length(s)+1):k) = '';\nend\nk = length(b);\nso = {b, k};\n\n% rs(s) is used further down.\n% [Note: possible null/value for r if rs is called]\nfunction r = rs(str, b, k, k0)\nr = {b, k};\nif measure(b, k0) > 0\n    r = setto(str, b, k);\nend\n\n% step1ab() gets rid of plurals and -ed or -ing. e.g.\n\n%       caresses  ->  caress\n%       ponies    ->  poni\n%       ties      ->  ti\n%       caress    ->  caress\n%       cats      ->  cat\n\n%       feed      ->  feed\n%       agreed    ->  agree\n%       disabled  ->  disable\n\n%       matting   ->  mat\n%       mating    ->  mate\n%       meeting   ->  meet\n%       milling   ->  mill\n%       messing   ->  mess\n\n%       meetings  ->  meet\n\nfunction s1ab = step1ab(b, k, k0)\nglobal j;\nif b(k) == 's'\n    if ends('sses', b, k)\n        k = k-2;\n    elseif ends('ies', b, k)\n        retVal = setto('i', b, k);\n        b = retVal{1};\n        k = retVal{2};\n    elseif (b(k-1) ~= 's')\n        k = k-1;\n    end\nend\nif ends('eed', b, k)\n    if measure(b, k0) > 0;\n        k = k-1;\n    end\nelseif (ends('ed', b, k) || ends('ing', b, k)) && vowelinstem(b, k0)\n    k = j;\n    retVal = {b, k};\n    if ends('at', b, k)\n        retVal = setto('ate', b(k0:k), k);\n    elseif ends('bl', b, k)\n        retVal = setto('ble', b(k0:k), k);\n    elseif ends('iz', b, k)\n        retVal = setto('ize', b(k0:k), k);\n    elseif doublec(k, b, k0)\n        retVal = {b, k-1};\n        if b(retVal{2}) == 'l' || b(retVal{2}) == 's' || ...\n                b(retVal{2}) == 'z'\n            retVal = {retVal{1}, retVal{2}+1};\n        end\n    elseif measure(b, k0) == 1 && cvc(k, b, k0)\n        retVal = setto('e', b(k0:k), k);\n    end\n    k = retVal{2};\n    b = retVal{1}(k0:k);\nend\nj = k;\ns1ab = {b(k0:k), k};\n\n%  step1c() turns terminal y to i when there is another vowel in the stem.\nfunction s1c = step1c(b, k, k0)\nglobal j;\nif ends('y', b, k) && vowelinstem(b, k0)\n    b(k) = 'i';\nend\nj = k;\ns1c = {b, k};\n\n% step2() maps double suffices to single ones. so -ization ( = -ize plus\n% -ation) maps to -ize etc. note that the string before the suffix must give\n% m() > 0.\nfunction s2 = step2(b, k, k0)\nglobal j;\ns2 = {b, k};\nswitch b(k-1)\n    case {'a'}\n        if ends('ational', b, k) s2 = rs('ate', b, k, k0);\n        elseif ends('tional', b, k) s2 = rs('tion', b, k, k0); end;\n    case {'c'}\n        if ends('enci', b, k) s2 = rs('ence', b, k, k0);\n        elseif ends('anci', b, k) s2 = rs('ance', b, k, k0); end;\n    case {'e'}\n        if ends('izer', b, k) s2 = rs('ize', b, k, k0); end;\n    case {'l'}\n        if ends('bli', b, k) s2 = rs('ble', b, k, k0);\n        elseif ends('alli', b, k) s2 = rs('al', b, k, k0);\n        elseif ends('entli', b, k) s2 = rs('ent', b, k, k0);\n        elseif ends('eli', b, k) s2 = rs('e', b, k, k0);\n        elseif ends('ousli', b, k) s2 = rs('ous', b, k, k0); end;\n    case {'o'}\n        if ends('ization', b, k) s2 = rs('ize', b, k, k0);\n        elseif ends('ation', b, k) s2 = rs('ate', b, k, k0);\n        elseif ends('ator', b, k) s2 = rs('ate', b, k, k0); end;\n    case {'s'}\n        if ends('alism', b, k) s2 = rs('al', b, k, k0);\n        elseif ends('iveness', b, k) s2 = rs('ive', b, k, k0);\n        elseif ends('fulness', b, k) s2 = rs('ful', b, k, k0);\n        elseif ends('ousness', b, k) s2 = rs('ous', b, k, k0); end;\n    case {'t'}\n        if ends('aliti', b, k) s2 = rs('al', b, k, k0);\n        elseif ends('iviti', b, k) s2 = rs('ive', b, k, k0);\n        elseif ends('biliti', b, k) s2 = rs('ble', b, k, k0); end;\n    case {'g'}\n        if ends('logi', b, k) s2 = rs('log', b, k, k0); end;\nend\nj = s2{2};\n\n% step3() deals with -ic-, -full, -ness etc. similar strategy to step2.\nfunction s3 = step3(b, k, k0)\nglobal j;\ns3 = {b, k};\nswitch b(k)\n    case {'e'}\n        if ends('icate', b, k) s3 = rs('ic', b, k, k0);\n        elseif ends('ative', b, k) s3 = rs('', b, k, k0);\n        elseif ends('alize', b, k) s3 = rs('al', b, k, k0); end;\n    case {'i'}\n        if ends('iciti', b, k) s3 = rs('ic', b, k, k0); end;\n    case {'l'}\n        if ends('ical', b, k) s3 = rs('ic', b, k, k0);\n        elseif ends('ful', b, k) s3 = rs('', b, k, k0); end;\n    case {'s'}\n        if ends('ness', b, k) s3 = rs('', b, k, k0); end;\nend\nj = s3{2};\n\n% step4() takes off -ant, -ence etc., in context <c>vcvc<v>.\nfunction s4 = step4(b, k, k0)\nglobal j;\nswitch b(k-1)\n    case {'a'}\n        if ends('al', b, k) end;\n    case {'c'}\n        if ends('ance', b, k)\n        elseif ends('ence', b, k) end;\n    case {'e'}\n        if ends('er', b, k) end;\n    case {'i'}\n        if ends('ic', b, k) end;\n    case {'l'}\n        if ends('able', b, k)\n        elseif ends('ible', b, k) end;\n    case {'n'}\n        if ends('ant', b, k)\n        elseif ends('ement', b, k)\n        elseif ends('ment', b, k)\n        elseif ends('ent', b, k) end;\n    case {'o'}\n        if ends('ion', b, k)\n            if j == 0\n            elseif ~(strcmp(b(j),'s') || strcmp(b(j),'t'))\n                j = k;\n            end\n        elseif ends('ou', b, k) end;\n    case {'s'}\n        if ends('ism', b, k) end;\n    case {'t'}\n        if ends('ate', b, k)\n        elseif ends('iti', b, k) end;\n    case {'u'}\n        if ends('ous', b, k) end;\n    case {'v'}\n        if ends('ive', b, k) end;\n    case {'z'}\n        if ends('ize', b, k) end;\nend\nif measure(b, k0) > 1\n    s4 = {b(k0:j), j};\nelse\n    s4 = {b(k0:k), k};\nend\n\n% step5() removes a final -e if m() > 1, and changes -ll to -l if m() > 1.\nfunction s5 = step5(b, k, k0)\nglobal j;\nj = k;\nif b(k) == 'e'\n    a = measure(b, k0);\n    if (a > 1) || ((a == 1) && ~cvc(k-1, b, k0))\n        k = k-1;\n    end\nend\nif (b(k) == 'l') && doublec(k, b, k0) && (measure(b, k0) > 1)\n    k = k-1;\nend\ns5 = {b(k0:k), k};\n"
  },
  {
    "path": "machine-learning-ex6/ex6/processEmail.m",
    "content": "function word_indices = processEmail(email_contents)\n%PROCESSEMAIL preprocesses a the body of an email and\n%returns a list of word_indices \n%   word_indices = PROCESSEMAIL(email_contents) preprocesses \n%   the body of an email and returns a list of indices of the \n%   words contained in the email. \n%\n\n% Load Vocabulary\nvocabList = getVocabList();\n\n% Init return value\nword_indices = [];\n\n% ========================== Preprocess Email ===========================\n\n% Find the Headers ( \\n\\n and remove )\n% Uncomment the following lines if you are working with raw emails with the\n% full headers\n\n% hdrstart = strfind(email_contents, ([char(10) char(10)]));\n% email_contents = email_contents(hdrstart(1):end);\n\n% Lower case\nemail_contents = lower(email_contents);\n\n% Strip all HTML\n% Looks for any expression that starts with < and ends with > and replace\n% and does not have any < or > in the tag it with a space\nemail_contents = regexprep(email_contents, '<[^<>]+>', ' ');\n\n% Handle Numbers\n% Look for one or more characters between 0-9\nemail_contents = regexprep(email_contents, '[0-9]+', 'number');\n\n% Handle URLS\n% Look for strings starting with http:// or https://\nemail_contents = regexprep(email_contents, ...\n                           '(http|https)://[^\\s]*', 'httpaddr');\n\n% Handle Email Addresses\n% Look for strings with @ in the middle\nemail_contents = regexprep(email_contents, '[^\\s]+@[^\\s]+', 'emailaddr');\n\n% Handle $ sign\nemail_contents = regexprep(email_contents, '[$]+', 'dollar');\n\n\n% ========================== Tokenize Email ===========================\n\n% Output the email to screen as well\nfprintf('\\n==== Processed Email ====\\n\\n');\n\n% Process file\nl = 0;\n\nwhile ~isempty(email_contents)\n\n    % Tokenize and also get rid of any punctuation\n    [str, email_contents] = ...\n       strtok(email_contents, ...\n              [' @$/#.-:&*+=[]?!(){},''\">_<;%' char(10) char(13)]);\n   \n    % Remove any non alphanumeric characters\n    str = regexprep(str, '[^a-zA-Z0-9]', '');\n\n    % Stem the word \n    % (the porterStemmer sometimes has issues, so we use a try catch block)\n    try str = porterStemmer(strtrim(str)); \n    catch str = ''; continue;\n    end;\n\n    % Skip the word if it is too short\n    if length(str) < 1\n       continue;\n    end\n\n    % Look up the word in the dictionary and add to word_indices if\n    % found\n    % ====================== YOUR CODE HERE ======================\n    % Instructions: Fill in this function to add the index of str to\n    %               word_indices if it is in the vocabulary. At this point\n    %               of the code, you have a stemmed word from the email in\n    %               the variable str. You should look up str in the\n    %               vocabulary list (vocabList). If a match exists, you\n    %               should add the index of the word to the word_indices\n    %               vector. Concretely, if str = 'action', then you should\n    %               look up the vocabulary list to find where in vocabList\n    %               'action' appears. For example, if vocabList{18} =\n    %               'action', then, you should add 18 to the word_indices \n    %               vector (e.g., word_indices = [word_indices ; 18]; ).\n    % \n    % Note: vocabList{idx} returns a the word with index idx in the\n    %       vocabulary list.\n    % \n    % Note: You can use strcmp(str1, str2) to compare two strings (str1 and\n    %       str2). It will return 1 only if the two strings are equivalent.\n    %\n\n    for i = 1:size(vocabList)\n        if strcmp(str,vocabList{i}) == 1\n            word_indices = [word_indices;i];\n        end\n    end\n\n\n\n\n\n\n\n\n    % =============================================================\n\n\n    % Print to screen, ensuring that the output lines are not too long\n    if (l + length(str) + 1) > 78\n        fprintf('\\n');\n        l = 0;\n    end\n    fprintf('%s ', str);\n    l = l + length(str) + 1;\n\nend\n\n% Print footer\nfprintf('\\n\\n=========================\\n');\n\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/readFile.m",
    "content": "function file_contents = readFile(filename)\n%READFILE reads a file and returns its entire contents \n%   file_contents = READFILE(filename) reads a file and returns its entire\n%   contents in file_contents\n%\n\n% Load File\nfid = fopen(filename);\nif fid\n    file_contents = fscanf(fid, '%c', inf);\n    fclose(fid);\nelse\n    file_contents = '';\n    fprintf('Unable to open %s\\n', filename);\nend\n\nend\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/spamSample1.txt",
    "content": "Do You Want To Make $1000 Or More Per Week?\n\n \n\nIf you are a motivated and qualified individual - I \nwill personally demonstrate to you a system that will \nmake you $1,000 per week or more! This is NOT mlm.\n\n \n\nCall our 24 hour pre-recorded number to get the \ndetails.  \n\n \n\n000-456-789\n\n \n\nI need people who want to make serious money.  Make \nthe call and get the facts. \n\nInvest 2 minutes in yourself now!\n\n \n\n000-456-789\n\n \n\nLooking forward to your call and I will introduce you \nto people like yourself who\nare currently making $10,000 plus per week!\n\n \n\n000-456-789\n\n\n\n3484lJGv6-241lEaN9080lRmS6-271WxHo7524qiyT5-438rjUv5615hQcf0-662eiDB9057dMtVl72\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/spamSample2.txt",
    "content": "Best Buy Viagra Generic Online\n\nViagra 100mg x 60 Pills $125, Free Pills & Reorder Discount, Top Selling 100% Quality & Satisfaction guaranteed!\n\nWe accept VISA, Master & E-Check Payments, 90000+ Satisfied Customers!\nhttp://medphysitcstech.ru\n\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/submit.m",
    "content": "function submit()\n  addpath('./lib');\n\n  conf.assignmentSlug = 'support-vector-machines';\n  conf.itemName = 'Support Vector Machines';\n  conf.partArrays = { ...\n    { ...\n      '1', ...\n      { 'gaussianKernel.m' }, ...\n      'Gaussian Kernel', ...\n    }, ...\n    { ...\n      '2', ...\n      { 'dataset3Params.m' }, ...\n      'Parameters (C, sigma) for Dataset 3', ...\n    }, ...\n    { ...\n      '3', ...\n      { 'processEmail.m' }, ...\n      'Email Preprocessing', ...\n    }, ...\n    { ...\n      '4', ...\n      { 'emailFeatures.m' }, ...\n      'Email Feature Extraction', ...\n    }, ...\n  };\n  conf.output = @output;\n\n  submitWithConfiguration(conf);\nend\n\nfunction out = output(partId, auxstring)\n  % Random Test Cases\n  x1 = sin(1:10)';\n  x2 = cos(1:10)';\n  ec = 'the quick brown fox jumped over the lazy dog';\n  wi = 1 + abs(round(x1 * 1863));\n  wi = [wi ; wi];\n  if partId == '1'\n    sim = gaussianKernel(x1, x2, 2);\n    out = sprintf('%0.5f ', sim);\n  elseif partId == '2'\n    load('ex6data3.mat');\n    [C, sigma] = dataset3Params(X, y, Xval, yval);\n    out = sprintf('%0.5f ', C);\n    out = [out sprintf('%0.5f ', sigma)];\n  elseif partId == '3'\n    word_indices = processEmail(ec);\n    out = sprintf('%d ', word_indices);\n  elseif partId == '4'\n    x = emailFeatures(wi);\n    out = sprintf('%d ', x);\n  end \nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/svmPredict.m",
    "content": "function pred = svmPredict(model, X)\n%SVMPREDICT returns a vector of predictions using a trained SVM model\n%(svmTrain). \n%   pred = SVMPREDICT(model, X) returns a vector of predictions using a \n%   trained SVM model (svmTrain). X is a mxn matrix where there each \n%   example is a row. model is a svm model returned from svmTrain.\n%   predictions pred is a m x 1 column of predictions of {0, 1} values.\n%\n\n% Check if we are getting a column vector, if so, then assume that we only\n% need to do prediction for a single example\nif (size(X, 2) == 1)\n    % Examples should be in rows\n    X = X';\nend\n\n% Dataset \nm = size(X, 1);\np = zeros(m, 1);\npred = zeros(m, 1);\n\nif strcmp(func2str(model.kernelFunction), 'linearKernel')\n    % We can use the weights and bias directly if working with the \n    % linear kernel\n    p = X * model.w + model.b;\nelseif strfind(func2str(model.kernelFunction), 'gaussianKernel')\n    % Vectorized RBF Kernel\n    % This is equivalent to computing the kernel on every pair of examples\n    X1 = sum(X.^2, 2);\n    X2 = sum(model.X.^2, 2)';\n    K = bsxfun(@plus, X1, bsxfun(@plus, X2, - 2 * X * model.X'));\n    K = model.kernelFunction(1, 0) .^ K;\n    K = bsxfun(@times, model.y', K);\n    K = bsxfun(@times, model.alphas', K);\n    p = sum(K, 2);\nelse\n    % Other Non-linear kernel\n    for i = 1:m\n        prediction = 0;\n        for j = 1:size(model.X, 1)\n            prediction = prediction + ...\n                model.alphas(j) * model.y(j) * ...\n                model.kernelFunction(X(i,:)', model.X(j,:)');\n        end\n        p(i) = prediction + model.b;\n    end\nend\n\n% Convert predictions into 0 / 1\npred(p >= 0) =  1;\npred(p <  0) =  0;\n\nend\n\n"
  },
  {
    "path": "machine-learning-ex6/ex6/svmTrain.m",
    "content": "function [model] = svmTrain(X, Y, C, kernelFunction, ...\n                            tol, max_passes)\n%SVMTRAIN Trains an SVM classifier using a simplified version of the SMO \n%algorithm. \n%   [model] = SVMTRAIN(X, Y, C, kernelFunction, tol, max_passes) trains an\n%   SVM classifier and returns trained model. X is the matrix of training \n%   examples.  Each row is a training example, and the jth column holds the \n%   jth feature.  Y is a column matrix containing 1 for positive examples \n%   and 0 for negative examples.  C is the standard SVM regularization \n%   parameter.  tol is a tolerance value used for determining equality of \n%   floating point numbers. max_passes controls the number of iterations\n%   over the dataset (without changes to alpha) before the algorithm quits.\n%\n% Note: This is a simplified version of the SMO algorithm for training\n%       SVMs. In practice, if you want to train an SVM classifier, we\n%       recommend using an optimized package such as:  \n%\n%           LIBSVM   (http://www.csie.ntu.edu.tw/~cjlin/libsvm/)\n%           SVMLight (http://svmlight.joachims.org/)\n%\n%\n\nif ~exist('tol', 'var') || isempty(tol)\n    tol = 1e-3;\nend\n\nif ~exist('max_passes', 'var') || isempty(max_passes)\n    max_passes = 5;\nend\n\n% Data parameters\nm = size(X, 1);\nn = size(X, 2);\n\n% Map 0 to -1\nY(Y==0) = -1;\n\n% Variables\nalphas = zeros(m, 1);\nb = 0;\nE = zeros(m, 1);\npasses = 0;\neta = 0;\nL = 0;\nH = 0;\n\n% Pre-compute the Kernel Matrix since our dataset is small\n% (in practice, optimized SVM packages that handle large datasets\n%  gracefully will _not_ do this)\n% \n% We have implemented optimized vectorized version of the Kernels here so\n% that the svm training will run faster.\nif strcmp(func2str(kernelFunction), 'linearKernel')\n    % Vectorized computation for the Linear Kernel\n    % This is equivalent to computing the kernel on every pair of examples\n    K = X*X';\nelseif strfind(func2str(kernelFunction), 'gaussianKernel')\n    % Vectorized RBF Kernel\n    % This is equivalent to computing the kernel on every pair of examples\n    X2 = sum(X.^2, 2);\n    K = bsxfun(@plus, X2, bsxfun(@plus, X2', - 2 * (X * X')));\n    K = kernelFunction(1, 0) .^ K;\nelse\n    % Pre-compute the Kernel Matrix\n    % The following can be slow due to the lack of vectorization\n    K = zeros(m);\n    for i = 1:m\n        for j = i:m\n             K(i,j) = kernelFunction(X(i,:)', X(j,:)');\n             K(j,i) = K(i,j); %the matrix is symmetric\n        end\n    end\nend\n\n% Train\nfprintf('\\nTraining ...');\ndots = 12;\nwhile passes < max_passes,\n            \n    num_changed_alphas = 0;\n    for i = 1:m,\n        \n        % Calculate Ei = f(x(i)) - y(i) using (2). \n        % E(i) = b + sum (X(i, :) * (repmat(alphas.*Y,1,n).*X)') - Y(i);\n        E(i) = b + sum (alphas.*Y.*K(:,i)) - Y(i);\n        \n        if ((Y(i)*E(i) < -tol && alphas(i) < C) || (Y(i)*E(i) > tol && alphas(i) > 0)),\n            \n            % In practice, there are many heuristics one can use to select\n            % the i and j. In this simplified code, we select them randomly.\n            j = ceil(m * rand());\n            while j == i,  % Make sure i \\neq j\n                j = ceil(m * rand());\n            end\n\n            % Calculate Ej = f(x(j)) - y(j) using (2).\n            E(j) = b + sum (alphas.*Y.*K(:,j)) - Y(j);\n\n            % Save old alphas\n            alpha_i_old = alphas(i);\n            alpha_j_old = alphas(j);\n            \n            % Compute L and H by (10) or (11). \n            if (Y(i) == Y(j)),\n                L = max(0, alphas(j) + alphas(i) - C);\n                H = min(C, alphas(j) + alphas(i));\n            else\n                L = max(0, alphas(j) - alphas(i));\n                H = min(C, C + alphas(j) - alphas(i));\n            end\n           \n            if (L == H),\n                % continue to next i. \n                continue;\n            end\n\n            % Compute eta by (14).\n            eta = 2 * K(i,j) - K(i,i) - K(j,j);\n            if (eta >= 0),\n                % continue to next i. \n                continue;\n            end\n            \n            % Compute and clip new value for alpha j using (12) and (15).\n            alphas(j) = alphas(j) - (Y(j) * (E(i) - E(j))) / eta;\n            \n            % Clip\n            alphas(j) = min (H, alphas(j));\n            alphas(j) = max (L, alphas(j));\n            \n            % Check if change in alpha is significant\n            if (abs(alphas(j) - alpha_j_old) < tol),\n                % continue to next i. \n                % replace anyway\n                alphas(j) = alpha_j_old;\n                continue;\n            end\n            \n            % Determine value for alpha i using (16). \n            alphas(i) = alphas(i) + Y(i)*Y(j)*(alpha_j_old - alphas(j));\n            \n            % Compute b1 and b2 using (17) and (18) respectively. \n            b1 = b - E(i) ...\n                 - Y(i) * (alphas(i) - alpha_i_old) *  K(i,j)' ...\n                 - Y(j) * (alphas(j) - alpha_j_old) *  K(i,j)';\n            b2 = b - E(j) ...\n                 - Y(i) * (alphas(i) - alpha_i_old) *  K(i,j)' ...\n                 - Y(j) * (alphas(j) - alpha_j_old) *  K(j,j)';\n\n            % Compute b by (19). \n            if (0 < alphas(i) && alphas(i) < C),\n                b = b1;\n            elseif (0 < alphas(j) && alphas(j) < C),\n                b = b2;\n            else\n                b = (b1+b2)/2;\n            end\n\n            num_changed_alphas = num_changed_alphas + 1;\n\n        end\n        \n    end\n    \n    if (num_changed_alphas == 0),\n        passes = passes + 1;\n    else\n        passes = 0;\n    end\n\n    fprintf('.');\n    dots = dots + 1;\n    if dots > 78\n        dots = 0;\n        fprintf('\\n');\n    end\n    if exist('OCTAVE_VERSION')\n        fflush(stdout);\n    end\nend\nfprintf(' Done! \\n\\n');\n\n% Save the model\nidx = alphas > 0;\nmodel.X= X(idx,:);\nmodel.y= Y(idx);\nmodel.kernelFunction = kernelFunction;\nmodel.b= b;\nmodel.alphas= alphas(idx);\nmodel.w = ((alphas.*Y)'*X)';\n\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/visualizeBoundary.m",
    "content": "function visualizeBoundary(X, y, model, varargin)\n%VISUALIZEBOUNDARY plots a non-linear decision boundary learned by the SVM\n%   VISUALIZEBOUNDARYLINEAR(X, y, model) plots a non-linear decision \n%   boundary learned by the SVM and overlays the data on it\n\n% Plot the training data on top of the boundary\nplotData(X, y)\n\n% Make classification predictions over a grid of values\nx1plot = linspace(min(X(:,1)), max(X(:,1)), 100)';\nx2plot = linspace(min(X(:,2)), max(X(:,2)), 100)';\n[X1, X2] = meshgrid(x1plot, x2plot);\nvals = zeros(size(X1));\nfor i = 1:size(X1, 2)\n   this_X = [X1(:, i), X2(:, i)];\n   vals(:, i) = svmPredict(model, this_X);\nend\n\n% Plot the SVM boundary\nhold on\ncontour(X1, X2, vals, [0 1], 'Color', 'b');     %ԭһ󣬷ΧӦ[0 1]\nhold off;\n\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/visualizeBoundaryLinear.m",
    "content": "function visualizeBoundaryLinear(X, y, model)\n%VISUALIZEBOUNDARYLINEAR plots a linear decision boundary learned by the\n%SVM\n%   VISUALIZEBOUNDARYLINEAR(X, y, model) plots a linear decision boundary \n%   learned by the SVM and overlays the data on it\n\nw = model.w;\nb = model.b;\nxp = linspace(min(X(:,1)), max(X(:,1)), 100);\nyp = - (w(1)*xp + b)/w(2);\nplotData(X, y);\nhold on;\nplot(xp, yp, '-b'); \nhold off\n\nend\n"
  },
  {
    "path": "machine-learning-ex6/ex6/vocab.txt",
    "content": "1\taa\n2\tab\n3\tabil\n4\tabl\n5\tabout\n6\tabov\n7\tabsolut\n8\tabus\n9\tac\n10\taccept\n11\taccess\n12\taccord\n13\taccount\n14\tachiev\n15\tacquir\n16\tacross\n17\tact\n18\taction\n19\tactiv\n20\tactual\n21\tad\n22\tadam\n23\tadd\n24\taddit\n25\taddress\n26\tadministr\n27\tadult\n28\tadvanc\n29\tadvantag\n30\tadvertis\n31\tadvic\n32\tadvis\n33\tae\n34\taf\n35\taffect\n36\taffili\n37\tafford\n38\tafrica\n39\tafter\n40\tag\n41\tagain\n42\tagainst\n43\tagenc\n44\tagent\n45\tago\n46\tagre\n47\tagreement\n48\taid\n49\tair\n50\tal\n51\talb\n52\talign\n53\tall\n54\tallow\n55\talmost\n56\talon\n57\talong\n58\talreadi\n59\talsa\n60\talso\n61\taltern\n62\talthough\n63\talwai\n64\tam\n65\tamaz\n66\tamerica\n67\tamerican\n68\tamong\n69\tamount\n70\tamp\n71\tan\n72\tanalysi\n73\tanalyst\n74\tand\n75\tani\n76\tanim\n77\tannounc\n78\tannual\n79\tannuiti\n80\tanoth\n81\tanswer\n82\tanti\n83\tanumb\n84\tanybodi\n85\tanymor\n86\tanyon\n87\tanyth\n88\tanywai\n89\tanywher\n90\taol\n91\tap\n92\tapolog\n93\tapp\n94\tappar\n95\tappear\n96\tappl\n97\tappli\n98\tapplic\n99\tappreci\n100\tapproach\n101\tapprov\n102\tapt\n103\tar\n104\tarchiv\n105\tarea\n106\taren\n107\targument\n108\tarial\n109\tarm\n110\taround\n111\tarrai\n112\tarriv\n113\tart\n114\tarticl\n115\tartist\n116\tas\n117\tascii\n118\task\n119\tasset\n120\tassist\n121\tassoci\n122\tassum\n123\tassur\n124\tat\n125\tatol\n126\tattach\n127\tattack\n128\tattempt\n129\tattent\n130\tattornei\n131\tattract\n132\taudio\n133\taug\n134\taugust\n135\tauthor\n136\tauto\n137\tautom\n138\tautomat\n139\tavail\n140\taverag\n141\tavoid\n142\tawai\n143\tawar\n144\taward\n145\tba\n146\tbabi\n147\tback\n148\tbackground\n149\tbackup\n150\tbad\n151\tbalanc\n152\tban\n153\tbank\n154\tbar\n155\tbase\n156\tbasenumb\n157\tbasi\n158\tbasic\n159\tbb\n160\tbc\n161\tbd\n162\tbe\n163\tbeat\n164\tbeberg\n165\tbecaus\n166\tbecom\n167\tbeen\n168\tbefor\n169\tbegin\n170\tbehalf\n171\tbehavior\n172\tbehind\n173\tbeliev\n174\tbelow\n175\tbenefit\n176\tbest\n177\tbeta\n178\tbetter\n179\tbetween\n180\tbf\n181\tbig\n182\tbill\n183\tbillion\n184\tbin\n185\tbinari\n186\tbit\n187\tblack\n188\tblank\n189\tblock\n190\tblog\n191\tblood\n192\tblue\n193\tbnumber\n194\tboard\n195\tbodi\n196\tboi\n197\tbonu\n198\tbook\n199\tboot\n200\tborder\n201\tboss\n202\tboston\n203\tbotan\n204\tboth\n205\tbottl\n206\tbottom\n207\tboundari\n208\tbox\n209\tbrain\n210\tbrand\n211\tbreak\n212\tbrian\n213\tbring\n214\tbroadcast\n215\tbroker\n216\tbrowser\n217\tbug\n218\tbui\n219\tbuild\n220\tbuilt\n221\tbulk\n222\tburn\n223\tbush\n224\tbusi\n225\tbut\n226\tbutton\n227\tby\n228\tbyte\n229\tca\n230\tcabl\n231\tcach\n232\tcalcul\n233\tcalifornia\n234\tcall\n235\tcame\n236\tcamera\n237\tcampaign\n238\tcan\n239\tcanada\n240\tcannot\n241\tcanon\n242\tcapabl\n243\tcapillari\n244\tcapit\n245\tcar\n246\tcard\n247\tcare\n248\tcareer\n249\tcarri\n250\tcartridg\n251\tcase\n252\tcash\n253\tcat\n254\tcatch\n255\tcategori\n256\tcaus\n257\tcb\n258\tcc\n259\tcd\n260\tce\n261\tcell\n262\tcent\n263\tcenter\n264\tcentral\n265\tcenturi\n266\tceo\n267\tcertain\n268\tcertainli\n269\tcf\n270\tchalleng\n271\tchanc\n272\tchang\n273\tchannel\n274\tchar\n275\tcharact\n276\tcharg\n277\tcharset\n278\tchat\n279\tcheap\n280\tcheck\n281\tcheer\n282\tchief\n283\tchildren\n284\tchina\n285\tchip\n286\tchoic\n287\tchoos\n288\tchri\n289\tciti\n290\tcitizen\n291\tcivil\n292\tclaim\n293\tclass\n294\tclassifi\n295\tclean\n296\tclear\n297\tclearli\n298\tclick\n299\tclient\n300\tclose\n301\tclue\n302\tcnet\n303\tcnumber\n304\tco\n305\tcode\n306\tcollect\n307\tcolleg\n308\tcolor\n309\tcom\n310\tcombin\n311\tcome\n312\tcomfort\n313\tcommand\n314\tcomment\n315\tcommentari\n316\tcommerci\n317\tcommiss\n318\tcommit\n319\tcommon\n320\tcommun\n321\tcompani\n322\tcompar\n323\tcomparison\n324\tcompat\n325\tcompet\n326\tcompetit\n327\tcompil\n328\tcomplet\n329\tcomprehens\n330\tcomput\n331\tconcentr\n332\tconcept\n333\tconcern\n334\tcondit\n335\tconf\n336\tconfer\n337\tconfid\n338\tconfidenti\n339\tconfig\n340\tconfigur\n341\tconfirm\n342\tconflict\n343\tconfus\n344\tcongress\n345\tconnect\n346\tconsid\n347\tconsolid\n348\tconstitut\n349\tconstruct\n350\tconsult\n351\tconsum\n352\tcontact\n353\tcontain\n354\tcontent\n355\tcontinu\n356\tcontract\n357\tcontribut\n358\tcontrol\n359\tconveni\n360\tconvers\n361\tconvert\n362\tcool\n363\tcooper\n364\tcopi\n365\tcopyright\n366\tcore\n367\tcorpor\n368\tcorrect\n369\tcorrespond\n370\tcost\n371\tcould\n372\tcouldn\n373\tcount\n374\tcountri\n375\tcoupl\n376\tcours\n377\tcourt\n378\tcover\n379\tcoverag\n380\tcrash\n381\tcreat\n382\tcreativ\n383\tcredit\n384\tcritic\n385\tcross\n386\tcultur\n387\tcurrent\n388\tcustom\n389\tcut\n390\tcv\n391\tda\n392\tdagga\n393\tdai\n394\tdaili\n395\tdan\n396\tdanger\n397\tdark\n398\tdata\n399\tdatabas\n400\tdatapow\n401\tdate\n402\tdave\n403\tdavid\n404\tdc\n405\tde\n406\tdead\n407\tdeal\n408\tdear\n409\tdeath\n410\tdebt\n411\tdecad\n412\tdecid\n413\tdecis\n414\tdeclar\n415\tdeclin\n416\tdecor\n417\tdefault\n418\tdefend\n419\tdefens\n420\tdefin\n421\tdefinit\n422\tdegre\n423\tdelai\n424\tdelet\n425\tdeliv\n426\tdeliveri\n427\tdell\n428\tdemand\n429\tdemocrat\n430\tdepart\n431\tdepend\n432\tdeposit\n433\tdescrib\n434\tdescript\n435\tdeserv\n436\tdesign\n437\tdesir\n438\tdesktop\n439\tdespit\n440\tdetail\n441\tdetect\n442\tdetermin\n443\tdev\n444\tdevel\n445\tdevelop\n446\tdevic\n447\tdi\n448\tdial\n449\tdid\n450\tdidn\n451\tdiet\n452\tdiffer\n453\tdifficult\n454\tdigit\n455\tdirect\n456\tdirectli\n457\tdirector\n458\tdirectori\n459\tdisabl\n460\tdiscount\n461\tdiscov\n462\tdiscoveri\n463\tdiscuss\n464\tdisk\n465\tdisplai\n466\tdisposit\n467\tdistanc\n468\tdistribut\n469\tdn\n470\tdnumber\n471\tdo\n472\tdoc\n473\tdocument\n474\tdoe\n475\tdoer\n476\tdoesn\n477\tdollar\n478\tdollarac\n479\tdollarnumb\n480\tdomain\n481\tdon\n482\tdone\n483\tdont\n484\tdoubl\n485\tdoubt\n486\tdown\n487\tdownload\n488\tdr\n489\tdraw\n490\tdream\n491\tdrive\n492\tdriver\n493\tdrop\n494\tdrug\n495\tdue\n496\tdure\n497\tdvd\n498\tdw\n499\tdynam\n500\tea\n501\teach\n502\tearli\n503\tearlier\n504\tearn\n505\tearth\n506\teasi\n507\teasier\n508\teasili\n509\teat\n510\teb\n511\tebai\n512\tec\n513\techo\n514\teconom\n515\teconomi\n516\ted\n517\tedg\n518\tedit\n519\teditor\n520\teduc\n521\teff\n522\teffect\n523\teffici\n524\teffort\n525\teither\n526\tel\n527\telectron\n528\telimin\n529\tels\n530\temail\n531\temailaddr\n532\temerg\n533\tempir\n534\temploy\n535\temploye\n536\ten\n537\tenabl\n538\tencod\n539\tencourag\n540\tend\n541\tenemi\n542\tenenkio\n543\tenergi\n544\tengin\n545\tenglish\n546\tenhanc\n547\tenjoi\n548\tenough\n549\tensur\n550\tenter\n551\tenterpris\n552\tentertain\n553\tentir\n554\tentri\n555\tenumb\n556\tenviron\n557\tequal\n558\tequip\n559\tequival\n560\terror\n561\tespeci\n562\tessenti\n563\testablish\n564\testat\n565\testim\n566\tet\n567\tetc\n568\teuro\n569\teurop\n570\teuropean\n571\teven\n572\tevent\n573\teventu\n574\tever\n575\teveri\n576\teveryon\n577\teveryth\n578\tevid\n579\tevil\n580\texactli\n581\texampl\n582\texcel\n583\texcept\n584\texchang\n585\texcit\n586\texclus\n587\texecut\n588\texercis\n589\texist\n590\texmh\n591\texpand\n592\texpect\n593\texpens\n594\texperi\n595\texpert\n596\texpir\n597\texplain\n598\texplor\n599\texpress\n600\textend\n601\textens\n602\textra\n603\textract\n604\textrem\n605\tey\n606\tfa\n607\tface\n608\tfact\n609\tfactor\n610\tfail\n611\tfair\n612\tfall\n613\tfals\n614\tfamili\n615\tfaq\n616\tfar\n617\tfast\n618\tfaster\n619\tfastest\n620\tfat\n621\tfather\n622\tfavorit\n623\tfax\n624\tfb\n625\tfd\n626\tfeatur\n627\tfeder\n628\tfee\n629\tfeed\n630\tfeedback\n631\tfeel\n632\tfemal\n633\tfew\n634\tffffff\n635\tffnumber\n636\tfield\n637\tfight\n638\tfigur\n639\tfile\n640\tfill\n641\tfilm\n642\tfilter\n643\tfinal\n644\tfinanc\n645\tfinanci\n646\tfind\n647\tfine\n648\tfinish\n649\tfire\n650\tfirewal\n651\tfirm\n652\tfirst\n653\tfit\n654\tfive\n655\tfix\n656\tflag\n657\tflash\n658\tflow\n659\tfnumber\n660\tfocu\n661\tfolder\n662\tfolk\n663\tfollow\n664\tfont\n665\tfood\n666\tfor\n667\tforc\n668\tforeign\n669\tforev\n670\tforget\n671\tfork\n672\tform\n673\tformat\n674\tformer\n675\tfortun\n676\tforward\n677\tfound\n678\tfoundat\n679\tfour\n680\tfranc\n681\tfree\n682\tfreedom\n683\tfrench\n684\tfreshrpm\n685\tfri\n686\tfridai\n687\tfriend\n688\tfrom\n689\tfront\n690\tftoc\n691\tftp\n692\tfull\n693\tfulli\n694\tfun\n695\tfunction\n696\tfund\n697\tfurther\n698\tfutur\n699\tga\n700\tgain\n701\tgame\n702\tgari\n703\tgarrigu\n704\tgave\n705\tgcc\n706\tgeek\n707\tgener\n708\tget\n709\tgif\n710\tgift\n711\tgirl\n712\tgive\n713\tgiven\n714\tglobal\n715\tgnome\n716\tgnu\n717\tgnupg\n718\tgo\n719\tgoal\n720\tgod\n721\tgoe\n722\tgold\n723\tgone\n724\tgood\n725\tgoogl\n726\tgot\n727\tgovern\n728\tgpl\n729\tgrand\n730\tgrant\n731\tgraphic\n732\tgreat\n733\tgreater\n734\tground\n735\tgroup\n736\tgrow\n737\tgrowth\n738\tgt\n739\tguarante\n740\tguess\n741\tgui\n742\tguid\n743\tha\n744\thack\n745\thad\n746\thalf\n747\tham\n748\thand\n749\thandl\n750\thappen\n751\thappi\n752\thard\n753\thardwar\n754\that\n755\thate\n756\thave\n757\thaven\n758\the\n759\thead\n760\theader\n761\theadlin\n762\thealth\n763\thear\n764\theard\n765\theart\n766\theaven\n767\thei\n768\theight\n769\theld\n770\thello\n771\thelp\n772\thelvetica\n773\ther\n774\therba\n775\there\n776\thermio\n777\thettinga\n778\thi\n779\thigh\n780\thigher\n781\thighli\n782\thighlight\n783\thim\n784\thistori\n785\thit\n786\thold\n787\thome\n788\thonor\n789\thope\n790\thost\n791\thot\n792\thour\n793\thous\n794\thow\n795\thowev\n796\thp\n797\thtml\n798\thttp\n799\thttpaddr\n800\thuge\n801\thuman\n802\thundr\n803\tibm\n804\tid\n805\tidea\n806\tident\n807\tidentifi\n808\tidnumb\n809\tie\n810\tif\n811\tignor\n812\tii\n813\tiii\n814\tiiiiiiihnumberjnumberhnumberjnumberhnumb\n815\tilleg\n816\tim\n817\timag\n818\timagin\n819\timmedi\n820\timpact\n821\timplement\n822\timport\n823\timpress\n824\timprov\n825\tin\n826\tinc\n827\tinclud\n828\tincom\n829\tincreas\n830\tincred\n831\tinde\n832\tindepend\n833\tindex\n834\tindia\n835\tindian\n836\tindic\n837\tindividu\n838\tindustri\n839\tinfo\n840\tinform\n841\tiniti\n842\tinlin\n843\tinnov\n844\tinput\n845\tinsert\n846\tinsid\n847\tinstal\n848\tinstanc\n849\tinstant\n850\tinstead\n851\tinstitut\n852\tinstruct\n853\tinsur\n854\tint\n855\tintegr\n856\tintel\n857\tintellig\n858\tintend\n859\tinteract\n860\tinterest\n861\tinterfac\n862\tintern\n863\tinternet\n864\tinterview\n865\tinto\n866\tintro\n867\tintroduc\n868\tinumb\n869\tinvest\n870\tinvestig\n871\tinvestor\n872\tinvok\n873\tinvolv\n874\tip\n875\tireland\n876\tirish\n877\tis\n878\tisland\n879\tisn\n880\tiso\n881\tisp\n882\tissu\n883\tit\n884\titem\n885\titself\n886\tjabber\n887\tjame\n888\tjava\n889\tjim\n890\tjnumberiiiiiiihepihepihf\n891\tjob\n892\tjoe\n893\tjohn\n894\tjoin\n895\tjournal\n896\tjudg\n897\tjudgment\n898\tjul\n899\tjuli\n900\tjump\n901\tjune\n902\tjust\n903\tjustin\n904\tkeep\n905\tkei\n906\tkept\n907\tkernel\n908\tkevin\n909\tkeyboard\n910\tkid\n911\tkill\n912\tkind\n913\tking\n914\tkingdom\n915\tknew\n916\tknow\n917\tknowledg\n918\tknown\n919\tla\n920\tlack\n921\tland\n922\tlanguag\n923\tlaptop\n924\tlarg\n925\tlarger\n926\tlargest\n927\tlaser\n928\tlast\n929\tlate\n930\tlater\n931\tlatest\n932\tlaunch\n933\tlaw\n934\tlawrenc\n935\tle\n936\tlead\n937\tleader\n938\tlearn\n939\tleast\n940\tleav\n941\tleft\n942\tlegal\n943\tlender\n944\tlength\n945\tless\n946\tlesson\n947\tlet\n948\tletter\n949\tlevel\n950\tlib\n951\tlibrari\n952\tlicens\n953\tlife\n954\tlifetim\n955\tlight\n956\tlike\n957\tlimit\n958\tline\n959\tlink\n960\tlinux\n961\tlist\n962\tlisten\n963\tlittl\n964\tlive\n965\tll\n966\tlo\n967\tload\n968\tloan\n969\tlocal\n970\tlocat\n971\tlock\n972\tlockergnom\n973\tlog\n974\tlong\n975\tlonger\n976\tlook\n977\tlose\n978\tloss\n979\tlost\n980\tlot\n981\tlove\n982\tlow\n983\tlower\n984\tlowest\n985\tlt\n986\tma\n987\tmac\n988\tmachin\n989\tmade\n990\tmagazin\n991\tmai\n992\tmail\n993\tmailer\n994\tmain\n995\tmaintain\n996\tmajor\n997\tmake\n998\tmaker\n999\tmale\n1000\tman\n1001\tmanag\n1002\tmani\n1003\tmanual\n1004\tmanufactur\n1005\tmap\n1006\tmarch\n1007\tmargin\n1008\tmark\n1009\tmarket\n1010\tmarshal\n1011\tmass\n1012\tmaster\n1013\tmatch\n1014\tmateri\n1015\tmatter\n1016\tmatthia\n1017\tmayb\n1018\tme\n1019\tmean\n1020\tmeasur\n1021\tmechan\n1022\tmedia\n1023\tmedic\n1024\tmeet\n1025\tmember\n1026\tmembership\n1027\tmemori\n1028\tmen\n1029\tmention\n1030\tmenu\n1031\tmerchant\n1032\tmessag\n1033\tmethod\n1034\tmh\n1035\tmichael\n1036\tmicrosoft\n1037\tmiddl\n1038\tmight\n1039\tmike\n1040\tmile\n1041\tmilitari\n1042\tmillion\n1043\tmime\n1044\tmind\n1045\tmine\n1046\tmini\n1047\tminimum\n1048\tminut\n1049\tmiss\n1050\tmistak\n1051\tmobil\n1052\tmode\n1053\tmodel\n1054\tmodem\n1055\tmodifi\n1056\tmodul\n1057\tmoment\n1058\tmon\n1059\tmondai\n1060\tmonei\n1061\tmonitor\n1062\tmonth\n1063\tmonthli\n1064\tmore\n1065\tmorn\n1066\tmortgag\n1067\tmost\n1068\tmostli\n1069\tmother\n1070\tmotiv\n1071\tmove\n1072\tmovi\n1073\tmpnumber\n1074\tmr\n1075\tms\n1076\tmsg\n1077\tmuch\n1078\tmulti\n1079\tmultipart\n1080\tmultipl\n1081\tmurphi\n1082\tmusic\n1083\tmust\n1084\tmy\n1085\tmyself\n1086\tname\n1087\tnation\n1088\tnatur\n1089\tnbsp\n1090\tnear\n1091\tnearli\n1092\tnecessari\n1093\tneed\n1094\tneg\n1095\tnet\n1096\tnetscap\n1097\tnetwork\n1098\tnever\n1099\tnew\n1100\tnewslett\n1101\tnext\n1102\tnextpart\n1103\tnice\n1104\tnigeria\n1105\tnight\n1106\tno\n1107\tnobodi\n1108\tnon\n1109\tnone\n1110\tnor\n1111\tnormal\n1112\tnorth\n1113\tnot\n1114\tnote\n1115\tnoth\n1116\tnotic\n1117\tnow\n1118\tnt\n1119\tnull\n1120\tnumber\n1121\tnumbera\n1122\tnumberam\n1123\tnumberanumb\n1124\tnumberb\n1125\tnumberbit\n1126\tnumberc\n1127\tnumbercb\n1128\tnumbercbr\n1129\tnumbercfont\n1130\tnumbercli\n1131\tnumbercnumb\n1132\tnumbercp\n1133\tnumberctd\n1134\tnumberd\n1135\tnumberdari\n1136\tnumberdnumb\n1137\tnumberenumb\n1138\tnumberf\n1139\tnumberfb\n1140\tnumberff\n1141\tnumberffont\n1142\tnumberfp\n1143\tnumberftd\n1144\tnumberk\n1145\tnumberm\n1146\tnumbermb\n1147\tnumberp\n1148\tnumberpd\n1149\tnumberpm\n1150\tnumberpx\n1151\tnumberst\n1152\tnumberth\n1153\tnumbertnumb\n1154\tnumberx\n1155\tobject\n1156\toblig\n1157\tobtain\n1158\tobvious\n1159\toccur\n1160\toct\n1161\toctob\n1162\tof\n1163\toff\n1164\toffer\n1165\toffic\n1166\toffici\n1167\toften\n1168\toh\n1169\tok\n1170\told\n1171\ton\n1172\tonc\n1173\tonli\n1174\tonlin\n1175\topen\n1176\toper\n1177\topinion\n1178\topportun\n1179\topt\n1180\toptim\n1181\toption\n1182\tor\n1183\torder\n1184\torg\n1185\torgan\n1186\torigin\n1187\tos\n1188\tosdn\n1189\tother\n1190\totherwis\n1191\tour\n1192\tout\n1193\toutlook\n1194\toutput\n1195\toutsid\n1196\tover\n1197\town\n1198\towner\n1199\toz\n1200\tpacif\n1201\tpack\n1202\tpackag\n1203\tpage\n1204\tpai\n1205\tpaid\n1206\tpain\n1207\tpalm\n1208\tpanel\n1209\tpaper\n1210\tparagraph\n1211\tparent\n1212\tpart\n1213\tparti\n1214\tparticip\n1215\tparticular\n1216\tparticularli\n1217\tpartit\n1218\tpartner\n1219\tpass\n1220\tpassword\n1221\tpast\n1222\tpatch\n1223\tpatent\n1224\tpath\n1225\tpattern\n1226\tpaul\n1227\tpayment\n1228\tpc\n1229\tpeac\n1230\tpeopl\n1231\tper\n1232\tpercent\n1233\tpercentag\n1234\tperfect\n1235\tperfectli\n1236\tperform\n1237\tperhap\n1238\tperiod\n1239\tperl\n1240\tperman\n1241\tpermiss\n1242\tperson\n1243\tpgp\n1244\tphone\n1245\tphoto\n1246\tphp\n1247\tphrase\n1248\tphysic\n1249\tpick\n1250\tpictur\n1251\tpiec\n1252\tpiiiiiiii\n1253\tpipe\n1254\tpjnumber\n1255\tplace\n1256\tplai\n1257\tplain\n1258\tplan\n1259\tplanet\n1260\tplant\n1261\tplanta\n1262\tplatform\n1263\tplayer\n1264\tpleas\n1265\tplu\n1266\tplug\n1267\tpm\n1268\tpocket\n1269\tpoint\n1270\tpolic\n1271\tpolici\n1272\tpolit\n1273\tpoor\n1274\tpop\n1275\tpopul\n1276\tpopular\n1277\tport\n1278\tposit\n1279\tpossibl\n1280\tpost\n1281\tpotenti\n1282\tpound\n1283\tpowel\n1284\tpower\n1285\tpowershot\n1286\tpractic\n1287\tpre\n1288\tpredict\n1289\tprefer\n1290\tpremium\n1291\tprepar\n1292\tpresent\n1293\tpresid\n1294\tpress\n1295\tpretti\n1296\tprevent\n1297\tpreviou\n1298\tprevious\n1299\tprice\n1300\tprincipl\n1301\tprint\n1302\tprintabl\n1303\tprinter\n1304\tprivaci\n1305\tprivat\n1306\tprize\n1307\tpro\n1308\tprobabl\n1309\tproblem\n1310\tprocedur\n1311\tprocess\n1312\tprocessor\n1313\tprocmail\n1314\tproduc\n1315\tproduct\n1316\tprofession\n1317\tprofil\n1318\tprofit\n1319\tprogram\n1320\tprogramm\n1321\tprogress\n1322\tproject\n1323\tpromis\n1324\tpromot\n1325\tprompt\n1326\tproperti\n1327\tpropos\n1328\tproprietari\n1329\tprospect\n1330\tprotect\n1331\tprotocol\n1332\tprove\n1333\tproven\n1334\tprovid\n1335\tproxi\n1336\tpub\n1337\tpublic\n1338\tpublish\n1339\tpudg\n1340\tpull\n1341\tpurchas\n1342\tpurpos\n1343\tput\n1344\tpython\n1345\tqnumber\n1346\tqualifi\n1347\tqualiti\n1348\tquarter\n1349\tquestion\n1350\tquick\n1351\tquickli\n1352\tquit\n1353\tquot\n1354\tradio\n1355\tragga\n1356\trais\n1357\trandom\n1358\trang\n1359\trate\n1360\trather\n1361\tratio\n1362\trazor\n1363\trazornumb\n1364\tre\n1365\treach\n1366\tread\n1367\treader\n1368\treadi\n1369\treal\n1370\trealiz\n1371\trealli\n1372\treason\n1373\treceiv\n1374\trecent\n1375\trecipi\n1376\trecommend\n1377\trecord\n1378\tred\n1379\tredhat\n1380\treduc\n1381\trefer\n1382\trefin\n1383\treg\n1384\tregard\n1385\tregion\n1386\tregist\n1387\tregul\n1388\tregular\n1389\trel\n1390\trelat\n1391\trelationship\n1392\treleas\n1393\trelev\n1394\treliabl\n1395\tremain\n1396\trememb\n1397\tremot\n1398\tremov\n1399\treplac\n1400\trepli\n1401\treport\n1402\trepositori\n1403\trepres\n1404\trepubl\n1405\trequest\n1406\trequir\n1407\tresearch\n1408\treserv\n1409\tresid\n1410\tresourc\n1411\trespect\n1412\trespond\n1413\trespons\n1414\trest\n1415\tresult\n1416\tretail\n1417\treturn\n1418\treveal\n1419\trevenu\n1420\trevers\n1421\treview\n1422\trevok\n1423\trh\n1424\trich\n1425\tright\n1426\trisk\n1427\troad\n1428\trobert\n1429\trock\n1430\trole\n1431\troll\n1432\trom\n1433\troman\n1434\troom\n1435\troot\n1436\tround\n1437\trpm\n1438\trss\n1439\trule\n1440\trun\n1441\tsa\n1442\tsafe\n1443\tsai\n1444\tsaid\n1445\tsale\n1446\tsame\n1447\tsampl\n1448\tsan\n1449\tsaou\n1450\tsat\n1451\tsatellit\n1452\tsave\n1453\tsaw\n1454\tscan\n1455\tschedul\n1456\tschool\n1457\tscienc\n1458\tscore\n1459\tscreen\n1460\tscript\n1461\tse\n1462\tsearch\n1463\tseason\n1464\tsecond\n1465\tsecret\n1466\tsection\n1467\tsecur\n1468\tsee\n1469\tseed\n1470\tseek\n1471\tseem\n1472\tseen\n1473\tselect\n1474\tself\n1475\tsell\n1476\tseminar\n1477\tsend\n1478\tsender\n1479\tsendmail\n1480\tsenior\n1481\tsens\n1482\tsensit\n1483\tsent\n1484\tsep\n1485\tsepar\n1486\tseptemb\n1487\tsequenc\n1488\tseri\n1489\tserif\n1490\tseriou\n1491\tserv\n1492\tserver\n1493\tservic\n1494\tset\n1495\tsetup\n1496\tseven\n1497\tseventh\n1498\tsever\n1499\tsex\n1500\tsexual\n1501\tsf\n1502\tshape\n1503\tshare\n1504\tshe\n1505\tshell\n1506\tship\n1507\tshop\n1508\tshort\n1509\tshot\n1510\tshould\n1511\tshow\n1512\tside\n1513\tsign\n1514\tsignatur\n1515\tsignific\n1516\tsimilar\n1517\tsimpl\n1518\tsimpli\n1519\tsinc\n1520\tsincer\n1521\tsingl\n1522\tsit\n1523\tsite\n1524\tsituat\n1525\tsix\n1526\tsize\n1527\tskeptic\n1528\tskill\n1529\tskin\n1530\tskip\n1531\tsleep\n1532\tslow\n1533\tsmall\n1534\tsmart\n1535\tsmoke\n1536\tsmtp\n1537\tsnumber\n1538\tso\n1539\tsocial\n1540\tsocieti\n1541\tsoftwar\n1542\tsold\n1543\tsolut\n1544\tsolv\n1545\tsome\n1546\tsomeon\n1547\tsometh\n1548\tsometim\n1549\tson\n1550\tsong\n1551\tsoni\n1552\tsoon\n1553\tsorri\n1554\tsort\n1555\tsound\n1556\tsourc\n1557\tsouth\n1558\tspace\n1559\tspain\n1560\tspam\n1561\tspamassassin\n1562\tspamd\n1563\tspammer\n1564\tspeak\n1565\tspec\n1566\tspecial\n1567\tspecif\n1568\tspecifi\n1569\tspeech\n1570\tspeed\n1571\tspend\n1572\tsponsor\n1573\tsport\n1574\tspot\n1575\tsrc\n1576\tssh\n1577\tst\n1578\tstabl\n1579\tstaff\n1580\tstai\n1581\tstand\n1582\tstandard\n1583\tstar\n1584\tstart\n1585\tstate\n1586\tstatement\n1587\tstatu\n1588\tstep\n1589\tsteve\n1590\tstill\n1591\tstock\n1592\tstop\n1593\tstorag\n1594\tstore\n1595\tstori\n1596\tstrategi\n1597\tstream\n1598\tstreet\n1599\tstring\n1600\tstrip\n1601\tstrong\n1602\tstructur\n1603\tstudi\n1604\tstuff\n1605\tstupid\n1606\tstyle\n1607\tsubject\n1608\tsubmit\n1609\tsubscrib\n1610\tsubscript\n1611\tsubstanti\n1612\tsuccess\n1613\tsuch\n1614\tsuffer\n1615\tsuggest\n1616\tsuit\n1617\tsum\n1618\tsummari\n1619\tsummer\n1620\tsun\n1621\tsuper\n1622\tsuppli\n1623\tsupport\n1624\tsuppos\n1625\tsure\n1626\tsurpris\n1627\tsuse\n1628\tsuspect\n1629\tsweet\n1630\tswitch\n1631\tsystem\n1632\ttab\n1633\ttabl\n1634\ttablet\n1635\ttag\n1636\ttake\n1637\ttaken\n1638\ttalk\n1639\ttape\n1640\ttarget\n1641\ttask\n1642\ttax\n1643\tteach\n1644\tteam\n1645\ttech\n1646\ttechnic\n1647\ttechniqu\n1648\ttechnolog\n1649\ttel\n1650\ttelecom\n1651\ttelephon\n1652\ttell\n1653\ttemperatur\n1654\ttempl\n1655\tten\n1656\tterm\n1657\ttermin\n1658\tterror\n1659\tterrorist\n1660\ttest\n1661\ttexa\n1662\ttext\n1663\tthan\n1664\tthank\n1665\tthat\n1666\tthe\n1667\tthei\n1668\ttheir\n1669\tthem\n1670\tthemselv\n1671\tthen\n1672\ttheori\n1673\tthere\n1674\ttherefor\n1675\tthese\n1676\tthi\n1677\tthing\n1678\tthink\n1679\tthinkgeek\n1680\tthird\n1681\tthose\n1682\tthough\n1683\tthought\n1684\tthousand\n1685\tthread\n1686\tthreat\n1687\tthree\n1688\tthrough\n1689\tthu\n1690\tthursdai\n1691\tti\n1692\tticket\n1693\ttim\n1694\ttime\n1695\ttip\n1696\ttire\n1697\ttitl\n1698\ttm\n1699\tto\n1700\ttodai\n1701\ttogeth\n1702\ttoken\n1703\ttold\n1704\ttoll\n1705\ttom\n1706\ttoner\n1707\ttoni\n1708\ttoo\n1709\ttook\n1710\ttool\n1711\ttop\n1712\ttopic\n1713\ttotal\n1714\ttouch\n1715\ttoward\n1716\ttrack\n1717\ttrade\n1718\ttradit\n1719\ttraffic\n1720\ttrain\n1721\ttransact\n1722\ttransfer\n1723\ttravel\n1724\ttreat\n1725\ttree\n1726\ttri\n1727\ttrial\n1728\ttrick\n1729\ttrip\n1730\ttroubl\n1731\ttrue\n1732\ttruli\n1733\ttrust\n1734\ttruth\n1735\ttry\n1736\ttue\n1737\ttuesdai\n1738\tturn\n1739\ttv\n1740\ttwo\n1741\ttype\n1742\tuk\n1743\tultim\n1744\tun\n1745\tunder\n1746\tunderstand\n1747\tunfortun\n1748\tuniqu\n1749\tunison\n1750\tunit\n1751\tunivers\n1752\tunix\n1753\tunless\n1754\tunlik\n1755\tunlimit\n1756\tunseen\n1757\tunsolicit\n1758\tunsubscrib\n1759\tuntil\n1760\tup\n1761\tupdat\n1762\tupgrad\n1763\tupon\n1764\turgent\n1765\turl\n1766\tus\n1767\tusa\n1768\tusag\n1769\tusb\n1770\tusd\n1771\tusdollarnumb\n1772\tuseless\n1773\tuser\n1774\tusr\n1775\tusual\n1776\tutil\n1777\tvacat\n1778\tvalid\n1779\tvalu\n1780\tvaluabl\n1781\tvar\n1782\tvariabl\n1783\tvarieti\n1784\tvariou\n1785\tve\n1786\tvendor\n1787\tventur\n1788\tveri\n1789\tverifi\n1790\tversion\n1791\tvia\n1792\tvideo\n1793\tview\n1794\tvirtual\n1795\tvisa\n1796\tvisit\n1797\tvisual\n1798\tvnumber\n1799\tvoic\n1800\tvote\n1801\tvs\n1802\tvulner\n1803\twa\n1804\twai\n1805\twait\n1806\twake\n1807\twalk\n1808\twall\n1809\twant\n1810\twar\n1811\twarm\n1812\twarn\n1813\twarranti\n1814\twashington\n1815\twasn\n1816\twast\n1817\twatch\n1818\twater\n1819\twe\n1820\twealth\n1821\tweapon\n1822\tweb\n1823\tweblog\n1824\twebsit\n1825\twed\n1826\twednesdai\n1827\tweek\n1828\tweekli\n1829\tweight\n1830\twelcom\n1831\twell\n1832\twent\n1833\twere\n1834\twest\n1835\twhat\n1836\twhatev\n1837\twhen\n1838\twhere\n1839\twhether\n1840\twhich\n1841\twhile\n1842\twhite\n1843\twhitelist\n1844\twho\n1845\twhole\n1846\twhose\n1847\twhy\n1848\twi\n1849\twide\n1850\twidth\n1851\twife\n1852\twill\n1853\twilliam\n1854\twin\n1855\twindow\n1856\twing\n1857\twinner\n1858\twireless\n1859\twish\n1860\twith\n1861\twithin\n1862\twithout\n1863\twnumberp\n1864\twoman\n1865\twomen\n1866\twon\n1867\twonder\n1868\tword\n1869\twork\n1870\tworker\n1871\tworld\n1872\tworldwid\n1873\tworri\n1874\tworst\n1875\tworth\n1876\twould\n1877\twouldn\n1878\twrite\n1879\twritten\n1880\twrong\n1881\twrote\n1882\twww\n1883\tximian\n1884\txml\n1885\txp\n1886\tyahoo\n1887\tye\n1888\tyeah\n1889\tyear\n1890\tyesterdai\n1891\tyet\n1892\tyork\n1893\tyou\n1894\tyoung\n1895\tyour\n1896\tyourself\n1897\tzdnet\n1898\tzero\n1899\tzip\n"
  },
  {
    "path": "machine-learning-ex7/ex7/computeCentroids.m",
    "content": "function centroids = computeCentroids(X, idx, K)\n%COMPUTECENTROIDS returs the new centroids by computing the means of the \n%data points assigned to each centroid.\n%   centroids = COMPUTECENTROIDS(X, idx, K) returns the new centroids by \n%   computing the means of the data points assigned to each centroid. It is\n%   given a dataset X where each row is a single data point, a vector\n%   idx of centroid assignments (i.e. each entry in range [1..K]) for each\n%   example, and K, the number of centroids. You should return a matrix\n%   centroids, where each row of centroids is the mean of the data points\n%   assigned to it.\n%\n\n% Useful variables\n[m n] = size(X);\n\n% You need to return the following variables correctly.\ncentroids = zeros(K, n);\n\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Go over every centroid and compute mean of all points that\n%               belong to it. Concretely, the row vector centroids(i, :)\n%               should contain the mean of the data points assigned to\n%               centroid i.\n%\n% Note: You can use a for-loop over the centroids to compute this.\n%\n\n\nfor i = 1:K\n    centroids(i,:) = mean(X(idx==i,:),1);\nend\n\n\n\n\n\n% =============================================================\n\n\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/displayData.m",
    "content": "function [h, display_array] = displayData(X, example_width)\n%DISPLAYDATA Display 2D data in a nice grid\n%   [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data\n%   stored in X in a nice grid. It returns the figure handle h and the \n%   displayed array if requested.\n\n% Set example_width automatically if not passed in\nif ~exist('example_width', 'var') || isempty(example_width) \n\texample_width = round(sqrt(size(X, 2)));\nend\n\n% Gray Image\ncolormap(gray);\n\n% Compute rows, cols\n[m n] = size(X);\nexample_height = (n / example_width);\n\n% Compute number of items to display\ndisplay_rows = floor(sqrt(m));\ndisplay_cols = ceil(m / display_rows);\n\n% Between images padding\npad = 1;\n\n% Setup blank display\ndisplay_array = - ones(pad + display_rows * (example_height + pad), ...\n                       pad + display_cols * (example_width + pad));\n\n% Copy each example into a patch on the display array\ncurr_ex = 1;\nfor j = 1:display_rows\n\tfor i = 1:display_cols\n\t\tif curr_ex > m, \n\t\t\tbreak; \n\t\tend\n\t\t% Copy the patch\n\t\t\n\t\t% Get the max value of the patch\n\t\tmax_val = max(abs(X(curr_ex, :)));\n\t\tdisplay_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...\n\t\t              pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...\n\t\t\t\t\t\treshape(X(curr_ex, :), example_height, example_width) / max_val;\n\t\tcurr_ex = curr_ex + 1;\n\tend\n\tif curr_ex > m, \n\t\tbreak; \n\tend\nend\n\n% Display Image\nh = imagesc(display_array, [-1 1]);\n\n% Do not show axis\naxis image off\n\ndrawnow;\n\nend\n"
  },
  {
    "path": "machine-learning-ex7/ex7/drawLine.m",
    "content": "function drawLine(p1, p2, varargin)\n%DRAWLINE Draws a line from point p1 to point p2\n%   DRAWLINE(p1, p2) Draws a line from point p1 to point p2 and holds the\n%   current figure\n\nplot([p1(1) p2(1)], [p1(2) p2(2)], varargin{:});\n\nend"
  },
  {
    "path": "machine-learning-ex7/ex7/ex7.m",
    "content": "%% Machine Learning Online Class\n%  Exercise 7 | Principle Component Analysis and K-Means Clustering\n%\n%  Instructions\n%  ------------\n%\n%  This file contains code that helps you get started on the\n%  exercise. You will need to complete the following functions:\n%\n%     pca.m\n%     projectData.m\n%     recoverData.m\n%     computeCentroids.m\n%     findClosestCentroids.m\n%     kMeansInitCentroids.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% ================= Part 1: Find Closest Centroids ====================\n%  To help you implement K-Means, we have divided the learning algorithm \n%  into two functions -- findClosestCentroids and computeCentroids. In this\n%  part, you shoudl complete the code in the findClosestCentroids function. \n%\nfprintf('Finding closest centroids.\\n\\n');\n\n% Load an example dataset that we will be using\nload('ex7data2.mat');\n\n% Select an initial set of centroids\nK = 3; % 3 Centroids\ninitial_centroids = [3 3; 6 2; 8 5];\n\n% Find the closest centroids for the examples using the\n% initial_centroids\nidx = findClosestCentroids(X, initial_centroids);\n\nfprintf('Closest centroids for the first 3 examples: \\n')\nfprintf(' %d', idx(1:3));\nfprintf('\\n(the closest centroids should be 1, 3, 2 respectively)\\n');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ===================== Part 2: Compute Means =========================\n%  After implementing the closest centroids function, you should now\n%  complete the computeCentroids function.\n%\nfprintf('\\nComputing centroids means.\\n\\n');\n\n%  Compute means based on the closest centroids found in the previous part.\ncentroids = computeCentroids(X, idx, K);\n\nfprintf('Centroids computed after initial finding of closest centroids: \\n')\nfprintf(' %f %f \\n' , centroids');\nfprintf('\\n(the centroids should be\\n');\nfprintf('   [ 2.428301 3.157924 ]\\n');\nfprintf('   [ 5.813503 2.633656 ]\\n');\nfprintf('   [ 7.119387 3.616684 ]\\n\\n');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% =================== Part 3: K-Means Clustering ======================\n%  After you have completed the two functions computeCentroids and\n%  findClosestCentroids, you have all the necessary pieces to run the\n%  kMeans algorithm. In this part, you will run the K-Means algorithm on\n%  the example dataset we have provided. \n%\nfprintf('\\nRunning K-Means clustering on example dataset.\\n\\n');\n\n% Load an example dataset\nload('ex7data2.mat');\n\n% Settings for running K-Means\nK = 3;\nmax_iters = 10;\n\n% For consistency, here we set centroids to specific values\n% but in practice you want to generate them automatically, such as by\n% settings them to be random examples (as can be seen in\n% kMeansInitCentroids).\ninitial_centroids = [3 3; 6 2; 8 5];\n\n% Run K-Means algorithm. The 'true' at the end tells our function to plot\n% the progress of K-Means\n[centroids, idx] = runkMeans(X, initial_centroids, max_iters, true);\nfprintf('\\nK-Means Done.\\n\\n');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ============= Part 4: K-Means Clustering on Pixels ===============\n%  In this exercise, you will use K-Means to compress an image. To do this,\n%  you will first run K-Means on the colors of the pixels in the image and\n%  then you will map each pixel on to it's closest centroid.\n%  \n%  You should now complete the code in kMeansInitCentroids.m\n%\n\nfprintf('\\nRunning K-Means clustering on pixels from an image.\\n\\n');\n\n%  Load an image of a bird\nA = double(imread('bird_small.png'));\n\n% If imread does not work for you, you can try instead\n%   load ('bird_small.mat');\n\nA = A / 255; % Divide by 255 so that all values are in the range 0 - 1\n\n% Size of the image\nimg_size = size(A);\n\n% Reshape the image into an Nx3 matrix where N = number of pixels.\n% Each row will contain the Red, Green and Blue pixel values\n% This gives us our dataset matrix X that we will use K-Means on.\nX = reshape(A, img_size(1) * img_size(2), 3);\n\n% Run your K-Means algorithm on this data\n% You should try different values of K and max_iters here\nK = 16; \nmax_iters = 10;\n\n% When using K-Means, it is important the initialize the centroids\n% randomly. \n% You should complete the code in kMeansInitCentroids.m before proceeding\ninitial_centroids = kMeansInitCentroids(X, K);\n\n% Run K-Means\n[centroids, idx] = runkMeans(X, initial_centroids, max_iters);\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% ================= Part 5: Image Compression ======================\n%  In this part of the exercise, you will use the clusters of K-Means to\n%  compress an image. To do this, we first find the closest clusters for\n%  each example. After that, we \n\nfprintf('\\nApplying K-Means to compress an image.\\n\\n');\n\n% Find closest cluster members\nidx = findClosestCentroids(X, centroids);\n\n% Essentially, now we have represented the image X as in terms of the\n% indices in idx. \n\n% We can now recover the image from the indices (idx) by mapping each pixel\n% (specified by it's index in idx) to the centroid value\nX_recovered = centroids(idx,:);\n\n% Reshape the recovered image into proper dimensions\nX_recovered = reshape(X_recovered, img_size(1), img_size(2), 3);\n\n% Display the original image \nsubplot(1, 2, 1);\nimagesc(A); \ntitle('Original');\n\n% Display compressed image side by side\nsubplot(1, 2, 2);\nimagesc(X_recovered)\ntitle(sprintf('Compressed, with %d colors.', K));\n\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/ex7_pca.m",
    "content": "%% Machine Learning Online Class\n%  Exercise 7 | Principle Component Analysis and K-Means Clustering\n%\n%  Instructions\n%  ------------\n%\n%  This file contains code that helps you get started on the\n%  exercise. You will need to complete the following functions:\n%\n%     pca.m\n%     projectData.m\n%     recoverData.m\n%     computeCentroids.m\n%     findClosestCentroids.m\n%     kMeansInitCentroids.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% ================== Part 1: Load Example Dataset  ===================\n%  We start this exercise by using a small dataset that is easily to\n%  visualize\n%\nfprintf('Visualizing example dataset for PCA.\\n\\n');\n\n%  The following command loads the dataset. You should now have the \n%  variable X in your environment\nload ('ex7data1.mat');\n\n%  Visualize the example dataset\nplot(X(:, 1), X(:, 2), 'bo');\naxis([0.5 6.5 2 8]); axis square;\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% =============== Part 2: Principal Component Analysis ===============\n%  You should now implement PCA, a dimension reduction technique. You\n%  should complete the code in pca.m\n%\nfprintf('\\nRunning PCA on example dataset.\\n\\n');\n\n%  Before running PCA, it is important to first normalize X\n[X_norm, mu, sigma] = featureNormalize(X);\n\n%  Run PCA\n[U, S] = pca(X_norm);\n\n%  Compute mu, the mean of the each feature\n\n%  Draw the eigenvectors centered at mean of data. These lines show the\n%  directions of maximum variations in the dataset.\nhold on;\ndrawLine(mu, mu + 1.5 * S(1,1) * U(:,1)', '-k', 'LineWidth', 2);\ndrawLine(mu, mu + 1.5 * S(2,2) * U(:,2)', '-k', 'LineWidth', 2);\nhold off;\n\nfprintf('Top eigenvector: \\n');\nfprintf(' U(:,1) = %f %f \\n', U(1,1), U(2,1));\nfprintf('\\n(you should expect to see -0.707107 -0.707107)\\n');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% =================== Part 3: Dimension Reduction ===================\n%  You should now implement the projection step to map the data onto the \n%  first k eigenvectors. The code will then plot the data in this reduced \n%  dimensional space.  This will show you what the data looks like when \n%  using only the corresponding eigenvectors to reconstruct it.\n%\n%  You should complete the code in projectData.m\n%\nfprintf('\\nDimension reduction on example dataset.\\n\\n');\n\n%  Plot the normalized dataset (returned from pca)\nplot(X_norm(:, 1), X_norm(:, 2), 'bo');\naxis([-4 3 -4 3]); axis square\n\n%  Project the data onto K = 1 dimension\nK = 1;\nZ = projectData(X_norm, U, K);\nfprintf('Projection of the first example: %f\\n', Z(1));\nfprintf('\\n(this value should be about 1.481274)\\n\\n');\n\nX_rec  = recoverData(Z, U, K);\nfprintf('Approximation of the first example: %f %f\\n', X_rec(1, 1), X_rec(1, 2));\nfprintf('\\n(this value should be about  -1.047419 -1.047419)\\n\\n');\n\n%  Draw lines connecting the projected points to the original points\nhold on;\nplot(X_rec(:, 1), X_rec(:, 2), 'ro');\nfor i = 1:size(X_norm, 1)\n    drawLine(X_norm(i,:), X_rec(i,:), '--k', 'LineWidth', 1);\nend\nhold off\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =============== Part 4: Loading and Visualizing Face Data =============\n%  We start the exercise by first loading and visualizing the dataset.\n%  The following code will load the dataset into your environment\n%\nfprintf('\\nLoading face dataset.\\n\\n');\n\n%  Load Face dataset\nload ('ex7faces.mat')\n\n%  Display the first 100 faces in the dataset\ndisplayData(X(1:100, :));\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% =========== Part 5: PCA on Face Data: Eigenfaces  ===================\n%  Run PCA and visualize the eigenvectors which are in this case eigenfaces\n%  We display the first 36 eigenfaces.\n%\nfprintf(['\\nRunning PCA on face dataset.\\n' ...\n         '(this mght take a minute or two ...)\\n\\n']);\n\n%  Before running PCA, it is important to first normalize X by subtracting \n%  the mean value from each feature\n[X_norm, mu, sigma] = featureNormalize(X);\n\n%  Run PCA\n[U, S] = pca(X_norm);\n\n%  Visualize the top 36 eigenvectors found\ndisplayData(U(:, 1:36)');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% ============= Part 6: Dimension Reduction for Faces =================\n%  Project images to the eigen space using the top k eigenvectors \n%  If you are applying a machine learning algorithm \nfprintf('\\nDimension reduction for face dataset.\\n\\n');\n\nK = 100;\nZ = projectData(X_norm, U, K);\n\nfprintf('The projected data Z has a size of: ')\nfprintf('%d ', size(Z));\n\nfprintf('\\n\\nProgram paused. Press enter to continue.\\n');\npause;\n\n%% ==== Part 7: Visualization of Faces after PCA Dimension Reduction ====\n%  Project images to the eigen space using the top K eigen vectors and \n%  visualize only using those K dimensions\n%  Compare to the original input, which is also displayed\n\nfprintf('\\nVisualizing the projected (reduced dimension) faces.\\n\\n');\n\nK = 100;\nX_rec  = recoverData(Z, U, K);\n\n% Display normalized data\nsubplot(1, 2, 1);\ndisplayData(X_norm(1:100,:));\ntitle('Original faces');\naxis square;\n\n% Display reconstructed data from only k eigenfaces\nsubplot(1, 2, 2);\ndisplayData(X_rec(1:100,:));\ntitle('Recovered faces');\naxis square;\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n\n%% === Part 8(a): Optional (ungraded) Exercise: PCA for Visualization ===\n%  One useful application of PCA is to use it to visualize high-dimensional\n%  data. In the last K-Means exercise you ran K-Means on 3-dimensional \n%  pixel colors of an image. We first visualize this output in 3D, and then\n%  apply PCA to obtain a visualization in 2D.\n\nclose all; close all; clc\n\n% Re-load the image from the previous exercise and run K-Means on it\n% For this to work, you need to complete the K-Means assignment first\nA = double(imread('bird_small.png'));\n\n% If imread does not work for you, you can try instead\n%   load ('bird_small.mat');\n\nA = A / 255;\nimg_size = size(A);\nX = reshape(A, img_size(1) * img_size(2), 3);\nK = 16; \nmax_iters = 10;\ninitial_centroids = kMeansInitCentroids(X, K);\n[centroids, idx] = runkMeans(X, initial_centroids, max_iters);\n\n%  Sample 1000 random indexes (since working with all the data is\n%  too expensive. If you have a fast computer, you may increase this.\nsel = floor(rand(1000, 1) * size(X, 1)) + 1;\n\n%  Setup Color Palette\npalette = hsv(K);\ncolors = palette(idx(sel), :);\n\n%  Visualize the data and centroid memberships in 3D\nfigure;\nscatter3(X(sel, 1), X(sel, 2), X(sel, 3), 10, colors);\ntitle('Pixel dataset plotted in 3D. Color shows centroid memberships');\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% === Part 8(b): Optional (ungraded) Exercise: PCA for Visualization ===\n% Use PCA to project this cloud to 2D for visualization\n\n% Subtract the mean to use PCA\n[X_norm, mu, sigma] = featureNormalize(X);\n\n% PCA and project the data to 2D\n[U, S] = pca(X_norm);\nZ = projectData(X_norm, U, 2);\n\n% Plot in 2D\nfigure;\nplotDataPoints(Z(sel, :), idx(sel), K);\ntitle('Pixel dataset plotted in 2D, using PCA for dimensionality reduction');\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n"
  },
  {
    "path": "machine-learning-ex7/ex7/featureNormalize.m",
    "content": "function [X_norm, mu, sigma] = featureNormalize(X)\n%FEATURENORMALIZE Normalizes the features in X \n%   FEATURENORMALIZE(X) returns a normalized version of X where\n%   the mean value of each feature is 0 and the standard deviation\n%   is 1. This is often a good preprocessing step to do when\n%   working with learning algorithms.\n\nmu = mean(X);\nX_norm = bsxfun(@minus, X, mu);\n\nsigma = std(X_norm);\nX_norm = bsxfun(@rdivide, X_norm, sigma);\n\n\n% ============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex7/ex7/findClosestCentroids.m",
    "content": "function idx = findClosestCentroids(X, centroids)\n%FINDCLOSESTCENTROIDS computes the centroid memberships for every example\n%   idx = FINDCLOSESTCENTROIDS (X, centroids) returns the closest centroids\n%   in idx for a dataset X where each row is a single example. idx = m x 1 \n%   vector of centroid assignments (i.e. each entry in range [1..K])\n%\n\n% Set K\nK = size(centroids, 1);\n\n% You need to return the following variables correctly.\nidx = zeros(size(X,1), 1);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Go over every example, find its closest centroid, and store\n%               the index inside idx at the appropriate location.\n%               Concretely, idx(i) should contain the index of the centroid\n%               closest to example i. Hence, it should be a value in the \n%               range 1..K\n%\n% Note: You can use a for-loop over the examples to compute this.\n%\n\nm = size(X,1);\n\nfor i = 1:m \n    for j = 1:K\n        idx(i,j) = (X(i,:)-centroids(j,:))*(X(i,:)-centroids(j,:))';\n    end\nend\n[dummy idx] = min(idx,[],2);\n\n\n\n\n\n% =============================================================\n\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/kMeansInitCentroids.m",
    "content": "function centroids = kMeansInitCentroids(X, K)\n%KMEANSINITCENTROIDS This function initializes K centroids that are to be \n%used in K-Means on the dataset X\n%   centroids = KMEANSINITCENTROIDS(X, K) returns K initial centroids to be\n%   used with the K-Means on the dataset X\n%\n\n% You should return this values correctly\ncentroids = zeros(K, size(X, 2));\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: You should set centroids to randomly chosen examples from\n%               the dataset X\n%\n\n\nrandidx = randperm(size(X, 1));\n\ncentroids = X(randidx(1:K), :);\n\n\n\n\n\n% =============================================================\n\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/AUTHORS.txt",
    "content": "The author of \"jsonlab\" toolbox is Qianqian Fang. Qianqian\nis currently an Assistant Professor at Massachusetts General Hospital, \nHarvard Medical School.\n\nAddress: Martinos Center for Biomedical Imaging, \n         Massachusetts General Hospital, \n         Harvard Medical School\n         Bldg 149, 13th St, Charlestown, MA 02129, USA\nURL: http://nmr.mgh.harvard.edu/~fangq/\nEmail: <fangq at nmr.mgh.harvard.edu> or <fangqq at gmail.com>\n\n\nThe script loadjson.m was built upon previous works by\n\n- Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n       date: 2009/11/02\n- François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n       date: 2009/03/22\n- Joel Feenstra: http://www.mathworks.com/matlabcentral/fileexchange/20565\n       date: 2008/07/03\n\n\nThis toolbox contains patches submitted by the following contributors:\n\n- Blake Johnson <bjohnso at bbn.com>\n  part of revision 341\n\n- Niclas Borlin <Niclas.Borlin at cs.umu.se>\n  various fixes in revision 394, including\n  - loadjson crashes for all-zero sparse matrix.\n  - loadjson crashes for empty sparse matrix.\n  - Non-zero size of 0-by-N and N-by-0 empty matrices is lost after savejson/loadjson.\n  - loadjson crashes for sparse real column vector.\n  - loadjson crashes for sparse complex column vector.\n  - Data is corrupted by savejson for sparse real row vector.\n  - savejson crashes for sparse complex row vector. \n\n- Yul Kang <yul.kang.on at gmail.com>\n  patches for svn revision 415.\n  - savejson saves an empty cell array as [] instead of null\n  - loadjson differentiates an empty struct from an empty array\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/ChangeLog.txt",
    "content": "============================================================================\n\n   JSONlab - a toolbox to encode/decode JSON/UBJSON files in MATLAB/Octave\n\n----------------------------------------------------------------------------\n\nJSONlab ChangeLog (key features marked by *):\n\n== JSONlab 1.0 (codename: Optimus - Final), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2015/01/02 polish help info for all major functions, update examples, finalize 1.0\n 2014/12/19 fix a bug to strictly respect NoRowBracket in savejson\n\n== JSONlab 1.0.0-RC2 (codename: Optimus - RC2), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/11/22 show progress bar in loadjson ('ShowProgress') \n 2014/11/17 add Compact option in savejson to output compact JSON format ('Compact')\n 2014/11/17 add FastArrayParser in loadjson to specify fast parser applicable levels\n 2014/09/18 start official github mirror: https://github.com/fangq/jsonlab\n\n== JSONlab 1.0.0-RC1 (codename: Optimus - RC1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/09/17  fix several compatibility issues when running on octave versions 3.2-3.8\n 2014/09/17  support 2D cell and struct arrays in both savejson and saveubjson\n 2014/08/04  escape special characters in a JSON string\n 2014/02/16  fix a bug when saving ubjson files\n\n== JSONlab 0.9.9 (codename: Optimus - beta), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/01/22  use binary read and write in saveubjson and loadubjson\n\n== JSONlab 0.9.8-1 (codename: Optimus - alpha update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2013/10/07 better round-trip conservation for empty arrays and structs (patch submitted by Yul Kang)\n\n== JSONlab 0.9.8 (codename: Optimus - alpha), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2013/08/23 *universal Binary JSON (UBJSON) support, including both saveubjson and loadubjson\n\n== JSONlab 0.9.1 (codename: Rodimus, update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2012/12/18 *handling of various empty and sparse matrices (fixes submitted by Niclas Borlin)\n\n== JSONlab 0.9.0 (codename: Rodimus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/06/17 *new format for an invalid leading char, unpacking hex code in savejson\n 2012/06/01  support JSONP in savejson\n 2012/05/25  fix the empty cell bug (reported by Cyril Davin)\n 2012/04/05  savejson can save to a file (suggested by Patrick Rapin)\n\n== JSONlab 0.8.1 (codename: Sentiel, Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/02/28  loadjson quotation mark escape bug, see http://bit.ly/yyk1nS\n 2012/01/25  patch to handle root-less objects, contributed by Blake Johnson\n\n== JSONlab 0.8.0 (codename: Sentiel), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/01/13 *speed up loadjson by 20 fold when parsing large data arrays in matlab\n 2012/01/11  remove row bracket if an array has 1 element, suggested by Mykel Kochenderfer\n 2011/12/22 *accept sequence of 'param',value input in savejson and loadjson\n 2011/11/18  fix struct array bug reported by Mykel Kochenderfer\n\n== JSONlab 0.5.1 (codename: Nexus Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/21  fix a bug in loadjson, previous code does not use any of the acceleration\n 2011/10/20  loadjson supports JSON collections - concatenated JSON objects\n\n== JSONlab 0.5.0 (codename: Nexus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/16  package and release jsonlab 0.5.0\n 2011/10/15 *add json demo and regression test, support cpx numbers, fix double quote bug\n 2011/10/11 *speed up readjson dramatically, interpret _Array* tags, show data in root level\n 2011/10/10  create jsonlab project, start jsonlab website, add online documentation\n 2011/10/07 *speed up savejson by 25x using sprintf instead of mat2str, add options support\n 2011/10/06 *savejson works for structs, cells and arrays\n 2011/09/09  derive loadjson from JSON parser from MATLAB Central, draft savejson.m\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/LICENSE_BSD.txt",
    "content": "Copyright 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are\npermitted provided that the following conditions are met:\n\n   1. Redistributions of source code must retain the above copyright notice, this list of\n      conditions and the following disclaimer.\n\n   2. Redistributions in binary form must reproduce the above copyright notice, this list\n      of conditions and the following disclaimer in the documentation and/or other materials\n      provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS \nOR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nThe views and conclusions contained in the software and documentation are those of the\nauthors and should not be interpreted as representing official policies, either expressed\nor implied, of the copyright holders.\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/README.txt",
    "content": "===============================================================================\n=                                 JSONLab                                     =\n=           An open-source MATLAB/Octave JSON encoder and decoder             =\n===============================================================================\n\n*Copyright (C) 2011-2015  Qianqian Fang <fangq at nmr.mgh.harvard.edu>\n*License: BSD License, see License_BSD.txt for details\n*Version: 1.0 (Optimus - Final)\n\n-------------------------------------------------------------------------------\n\nTable of Content:\n\nI.  Introduction\nII. Installation\nIII.Using JSONLab\nIV. Known Issues and TODOs\nV.  Contribution and feedback\n\n-------------------------------------------------------------------------------\n\nI.  Introduction\n\nJSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable, \nhuman-readable and \"[http://en.wikipedia.org/wiki/JSON fat-free]\" text format \nto represent complex and hierarchical data. It is as powerful as \n[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely \nused for data-exchange in applications, and is essential for the wild success \nof [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and \n[http://en.wikipedia.org/wiki/Web_2.0 Web2.0]. \n\nUBJSON (Universal Binary JSON) is a binary JSON format, specifically \noptimized for compact file size and better performance while keeping\nthe semantics as simple as the text-based JSON format. Using the UBJSON\nformat allows to wrap complex binary data in a flexible and extensible\nstructure, making it possible to process complex and large dataset \nwithout accuracy loss due to text conversions.\n\nWe envision that both JSON and its binary version will serve as part of \nthe mainstream data-exchange formats for scientific research in the future. \nIt will provide the flexibility and generality achieved by other popular \ngeneral-purpose file specifications, such as\n[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly \nreduced complexity and enhanced performance.\n\nJSONLab is a free and open-source implementation of a JSON/UBJSON encoder \nand a decoder in the native MATLAB language. It can be used to convert a MATLAB \ndata structure (array, struct, cell, struct array and cell array) into \nJSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB \ndata structure. JSONLab supports both MATLAB and  \n[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).\n\n-------------------------------------------------------------------------------\n\nII. Installation\n\nThe installation of JSONLab is no different than any other simple\nMATLAB toolbox. You only need to download/unzip the JSONLab package\nto a folder, and add the folder's path to MATLAB/Octave's path list\nby using the following command:\n\n    addpath('/path/to/jsonlab');\n\nIf you want to add this path permanently, you need to type \"pathtool\", \nbrowse to the jsonlab root folder and add to the list, then click \"Save\".\nThen, run \"rehash\" in MATLAB, and type \"which loadjson\", if you see an \noutput, that means JSONLab is installed for MATLAB/Octave.\n\n-------------------------------------------------------------------------------\n\nIII.Using JSONLab\n\nJSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder, \nand savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and \ntwo equivallent functions -- loadubjson and saveubjson for the binary \nJSON. The detailed help info for the four functions can be found below:\n\n=== loadjson.m ===\n<pre>\n  data=loadjson(fname,opt)\n     or\n  data=loadjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09, including previous works from \n \n          Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n             created on 2009/11/02\n          Franois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n             created on  2009/03/22\n          Joel Feenstra:\n          http://www.mathworks.com/matlabcentral/fileexchange/20565\n             created on 2008/07/03\n \n  $Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a JSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n                          speed-optimized array parser when loading an \n                          array object. The fast array parser may \n                          collapse block arrays into a single large\n                          array similar to rules defined in cell2mat; 0 to \n                          use a legacy parser; if set to a larger-than-1\n                          value, this option will specify the minimum\n                          dimension to enable the fast array parser. For\n                          example, if the input is a 3D array, setting\n                          FastArrayParser to 1 will return a 3D array;\n                          setting to 2 will return a cell array of 2D\n                          arrays; setting to 3 will return to a 2D cell\n                          array of 1D vectors; setting to 4 will return a\n                          3D cell array.\n            opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n       dat=loadjson(['examples' filesep 'example1.json'])\n       dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n</pre>\n\n=== savejson.m ===\n\n<pre>\n  json=savejson(rootname,obj,filename)\n     or\n  json=savejson(rootname,obj,opt)\n  json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n  Object Notation) string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09\n \n  $Id: savejson.m 458 2014-12-19 22:17:17Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array).\n       filename: a string for the file name to save the output JSON data.\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n                          of a 1D/2D array;\n         opt.ArrayIndent [1|0]: if 1, output explicit data array with\n                          precedent indentation; if 0, no indentation\n         opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n                          to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n                          and $1 represents the sign. For those who want to use\n                          1e999 to represent Inf, they can set opt.Inf to '$11e999'\n         opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n                          to represent NaN\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSONP='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n         opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n         opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a string in the JSON format (see http://json.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       savejson('jmesh',jsonmesh)\n       savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n </pre>\n\n=== loadubjson.m ===\n\n<pre>\n  data=loadubjson(fname,opt)\n     or\n  data=loadubjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/01\n \n  $Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a UBJSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.IntEndian [B|L]: specify the endianness of the integer fields\n                          in the UBJSON input data. B - Big-Endian format for \n                          integers (as required in the UBJSON specification); \n                          L - input integer fields are in Little-Endian order.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       obj=struct('string','value','array',[1 2 3]);\n       ubjdata=saveubjson('obj',obj);\n       dat=loadubjson(ubjdata)\n       dat=loadubjson(['examples' filesep 'example1.ubj'])\n       dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n</pre>\n\n=== saveubjson.m ===\n\n<pre>\n  json=saveubjson(rootname,obj,filename)\n     or\n  json=saveubjson(rootname,obj,opt)\n  json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a Universal \n  Binary JSON (UBJSON) binary string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/17\n \n  $Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array)\n       filename: a string for the file name to save the output UBJSON data\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSON='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a binary string in the UBJSON format (see http://ubjson.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       saveubjson('jsonmesh',jsonmesh)\n       saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n</pre>\n\n\n=== examples ===\n\nUnder the \"examples\" folder, you can find several scripts to demonstrate the\nbasic utilities of JSONLab. Running the \"demo_jsonlab_basic.m\" script, you \nwill see the conversions from MATLAB data structure to JSON text and backward.\nIn \"jsonlab_selftest.m\", we load complex JSON files downloaded from the Internet\nand validate the loadjson/savejson functions for regression testing purposes.\nSimilarly, a \"demo_ubjson_basic.m\" script is provided to test the saveubjson\nand loadubjson pairs for various matlab data structures.\n\nPlease run these examples and understand how JSONLab works before you use\nit to process your data.\n\n-------------------------------------------------------------------------------\n\nIV. Known Issues and TODOs\n\nJSONLab has several known limitations. We are striving to make it more general\nand robust. Hopefully in a few future releases, the limitations become less.\n\nHere are the known issues:\n\n# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;\n# When processing names containing multi-byte characters, Octave and MATLAB \\\ncan give different field-names; you can use feature('DefaultCharacterSet','latin1') \\\nin MATLAB to get consistant results\n# savejson can not handle class and dataset.\n# saveubjson converts a logical array into a uint8 ([U]) array\n# an unofficial N-D array count syntax is implemented in saveubjson. We are \\\nactively communicating with the UBJSON spec maintainer to investigate the \\\npossibility of making it upstream\n# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \\\nfiles, however, it can parse all UBJSON files produced by saveubjson.\n\n-------------------------------------------------------------------------------\n\nV. Contribution and feedback\n\nJSONLab is an open-source project. This means you can not only use it and modify\nit as you wish, but also you can contribute your changes back to JSONLab so\nthat everyone else can enjoy the improvement. For anyone who want to contribute,\nplease download JSONLab source code from it's subversion repository by using the\nfollowing command:\n\n svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab\n\nYou can make changes to the files as needed. Once you are satisfied with your\nchanges, and ready to share it with others, please cd the root directory of \nJSONLab, and type\n\n svn diff > yourname_featurename.patch\n\nYou then email the .patch file to JSONLab's maintainer, Qianqian Fang, at\nthe email address shown in the beginning of this file. Qianqian will review \nthe changes and commit it to the subversion if they are satisfactory.\n\nWe appreciate any suggestions and feedbacks from you. Please use iso2mesh's\nmailing list to report any questions you may have with JSONLab:\n\nhttp://groups.google.com/group/iso2mesh-users?hl=en&pli=1\n\n(Subscription to the mailing list is needed in order to post messages).\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/jsonopt.m",
    "content": "function val=jsonopt(key,default,varargin)\n%\n% val=jsonopt(key,default,optstruct)\n%\n% setting options based on a struct. The struct can be produced\n% by varargin2struct from a list of 'param','value' pairs\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n%\n% $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $\n%\n% input:\n%      key: a string with which one look up a value from a struct\n%      default: if the key does not exist, return default\n%      optstruct: a struct where each sub-field is a key \n%\n% output:\n%      val: if key exists, val=optstruct.key; otherwise val=default\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n% \n\nval=default;\nif(nargin<=2) return; end\nopt=varargin{1};\nif(isstruct(opt) && isfield(opt,key))\n    val=getfield(opt,key);\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/loadjson.m",
    "content": "function data = loadjson(fname,varargin)\n%\n% data=loadjson(fname,opt)\n%    or\n% data=loadjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09, including previous works from \n%\n%         Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n%            created on 2009/11/02\n%         François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n%            created on  2009/03/22\n%         Joel Feenstra:\n%         http://www.mathworks.com/matlabcentral/fileexchange/20565\n%            created on 2008/07/03\n%\n% $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a JSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n%                         speed-optimized array parser when loading an \n%                         array object. The fast array parser may \n%                         collapse block arrays into a single large\n%                         array similar to rules defined in cell2mat; 0 to \n%                         use a legacy parser; if set to a larger-than-1\n%                         value, this option will specify the minimum\n%                         dimension to enable the fast array parser. For\n%                         example, if the input is a 3D array, setting\n%                         FastArrayParser to 1 will return a 3D array;\n%                         setting to 2 will return a cell array of 2D\n%                         arrays; setting to 3 will return to a 2D cell\n%                         array of 1D vectors; setting to 4 will return a\n%                         3D cell array.\n%           opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n%      dat=loadjson(['examples' filesep 'example1.json'])\n%      dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\n\nif(jsonopt('ShowProgress',0,opt)==1)\n    opt.progressbar_=waitbar(0,'loading ...');\nend\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\nif(isfield(opt,'progressbar_'))\n    close(opt.progressbar_);\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=data(j).x0x5F_ArraySize_;\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    if next_char ~= '}'\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            parse_char(':');\n            val = parse_value(varargin{:});\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}'\n                break;\n            end\n            parse_char(',');\n        end\n    end\n    parse_char('}');\n\n%%-------------------------------------------------------------------------\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim2=[];\n    arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:});\n    pbar=jsonopt('progressbar_',-1,varargin{:});\n\n    if next_char ~= ']'\n\tif(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))\n            [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos);\n            arraystr=['[' inStr(pos:endpos)];\n            arraystr=regexprep(arraystr,'\"_NaN_\"','NaN');\n            arraystr=regexprep(arraystr,'\"([-+]*)_Inf_\"','$1Inf');\n            arraystr(arraystr==sprintf('\\n'))=[];\n            arraystr(arraystr==sprintf('\\r'))=[];\n            %arraystr=regexprep(arraystr,'\\s*,',','); % this is slow,sometimes needed\n            if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D\n        \tastr=inStr((e1l+1):(e1r-1));\n        \tastr=regexprep(astr,'\"_NaN_\"','NaN');\n        \tastr=regexprep(astr,'\"([-+]*)_Inf_\"','$1Inf');\n        \tastr(astr==sprintf('\\n'))=[];\n        \tastr(astr==sprintf('\\r'))=[];\n        \tastr(astr==' ')='';\n        \tif(isempty(find(astr=='[', 1))) % array is 2D\n                    dim2=length(sscanf(astr,'%f,',[1 inf]));\n        \tend\n            else % array is 1D\n        \tastr=arraystr(2:end-1);\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]);\n        \tif(nextidx>=length(astr)-1)\n                    object=obj;\n                    pos=endpos;\n                    parse_char(']');\n                    return;\n        \tend\n            end\n            if(~isempty(dim2))\n        \tastr=arraystr;\n        \tastr(astr=='[')='';\n        \tastr(astr==']')='';\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf);\n        \tif(nextidx>=length(astr)-1)\n                    object=reshape(obj,dim2,numel(obj)/dim2)';\n                    pos=endpos;\n                    parse_char(']');\n                    if(pbar>0)\n                        waitbar(pos/length(inStr),pbar,'loading ...');\n                    end\n                    return;\n        \tend\n            end\n            arraystr=regexprep(arraystr,'\\]\\s*,','];');\n\telse\n            arraystr='[';\n\tend\n        try\n           if(isoct && regexp(arraystr,'\"','once'))\n                error('Octave eval can produce empty cells for JSON-like input');\n           end\n           object=eval(arraystr);\n           pos=endpos;\n        catch\n         while 1\n            newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1);\n            val = parse_value(newopt);\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            parse_char(',');\n         end\n        end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    parse_char(']');\n    \n    if(pbar>0)\n        waitbar(pos/length(inStr),pbar,'loading ...');\n    end\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr len  esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    if inStr(pos) ~= '\"'\n        error_pos('String starting with \" expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    str = '';\n    while pos <= len\n        while index_esc <= len_esc && esc(index_esc) < pos\n            index_esc = index_esc + 1;\n        end\n        if index_esc > len_esc\n            str = [str inStr(pos:len)];\n            pos = len + 1;\n            break;\n        else\n            str = [str inStr(pos:esc(index_esc)-1)];\n            pos = esc(index_esc);\n        end\n        nstr = length(str); switch inStr(pos)\n            case '\"'\n                pos = pos + 1;\n                if(~isempty(str))\n                    if(strcmp(str,'_Inf_'))\n                        str=Inf;\n                    elseif(strcmp(str,'-_Inf_'))\n                        str=-Inf;\n                    elseif(strcmp(str,'_NaN_'))\n                        str=NaN;\n                    end\n                end\n                return;\n            case '\\'\n                if pos+1 > len\n                    error_pos('End of file reached right after escape character');\n                end\n                pos = pos + 1;\n                switch inStr(pos)\n                    case {'\"' '\\' '/'}\n                        str(nstr+1) = inStr(pos);\n                        pos = pos + 1;\n                    case {'b' 'f' 'n' 'r' 't'}\n                        str(nstr+1) = sprintf(['\\' inStr(pos)]);\n                        pos = pos + 1;\n                    case 'u'\n                        if pos+4 > len\n                            error_pos('End of file reached in escaped unicode character');\n                        end\n                        str(nstr+(1:6)) = inStr(pos-1:pos+4);\n                        pos = pos + 5;\n                end\n            otherwise % should never happen\n                str(nstr+1) = inStr(pos), keyboard\n                pos = pos + 1;\n        end\n    end\n    error_pos('End of file while expecting end of inStr');\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct\n    currstr=inStr(pos:end);\n    numstr=0;\n    if(isoct~=0)\n        numstr=regexp(currstr,'^\\s*-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+\\-]?\\d+)?','end');\n        [num, one] = sscanf(currstr, '%f', 1);\n        delta=numstr+1;\n    else\n        [num, one, err, delta] = sscanf(currstr, '%f', 1);\n        if ~isempty(err)\n            error_pos('Error reading number at position %d');\n        end\n    end\n    pos = pos + delta-1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n    \n    pbar=jsonopt('progressbar_',-1,varargin{:});\n    if(pbar>0)\n        waitbar(pos/len,pbar,'loading ...');\n    end\n    \n    switch(inStr(pos))\n        case '\"'\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'-','0','1','2','3','4','5','6','7','8','9'}\n            val = parse_number(varargin{:});\n            return;\n        case 't'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true')\n                val = true;\n                pos = pos + 4;\n                return;\n            end\n        case 'f'\n            if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false')\n                val = false;\n                pos = pos + 5;\n                return;\n            end\n        case 'n'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null')\n                val = [];\n                pos = pos + 4;\n                return;\n            end\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/loadubjson.m",
    "content": "function data = loadubjson(fname,varargin)\n%\n% data=loadubjson(fname,opt)\n%    or\n% data=loadubjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/01\n%\n% $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a UBJSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.IntEndian [B|L]: specify the endianness of the integer fields\n%                         in the UBJSON input data. B - Big-Endian format for \n%                         integers (as required in the UBJSON specification); \n%                         L - input integer fields are in Little-Endian order.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      obj=struct('string','value','array',[1 2 3]);\n%      ubjdata=saveubjson('obj',obj);\n%      dat=loadubjson(ubjdata)\n%      dat=loadubjson(['examples' filesep 'example1.ubj'])\n%      dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken fileendian systemendian\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\nfileendian=upper(jsonopt('IntEndian','B',opt));\n[os,maxelem,systemendian]=computer;\n\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\n\n\n%%\nfunction newdata=parse_collection(id,data,obj)\n\nif(jsoncount>0 && exist('data','var')) \n    if(~iscell(data))\n       newdata=cell(1);\n       newdata{1}=data;\n       data=newdata;\n    end\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=double(data(j).x0x5F_ArraySize_);\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1); % TODO\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        count=double(parse_number());\n    end\n    if next_char ~= '}'\n        num=0;\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            %parse_char(':');\n            val = parse_value(varargin{:});\n            num=num+1;\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}' || (count>=0 && num>=count)\n                break;\n            end\n            %parse_char(',');\n        end\n    end\n    if(count==-1)\n        parse_char('}');\n    end\n\n%%-------------------------------------------------------------------------\nfunction [cid,len]=elem_info(type)\nid=strfind('iUIlLdD',type);\ndataclass={'int8','uint8','int16','int32','int64','single','double'};\nbytelen=[1,1,2,4,8,4,8];\nif(id>0)\n    cid=dataclass{id};\n    len=bytelen(id);\nelse\n    error_pos('unsupported type at position %d');\nend\n%%-------------------------------------------------------------------------\n\n\nfunction [data adv]=parse_block(type,count,varargin)\nglobal pos inStr isoct fileendian systemendian\n[cid,len]=elem_info(type);\ndatastr=inStr(pos:pos+len*count-1);\nif(isoct)\n    newdata=int8(datastr);\nelse\n    newdata=uint8(datastr);\nend\nid=strfind('iUIlLdD',type);\nif(id<=5 && fileendian~=systemendian)\n    newdata=swapbytes(typecast(newdata,cid));\nend\ndata=typecast(newdata,cid);\nadv=double(len*count);\n\n%%-------------------------------------------------------------------------\n\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim=[];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1);\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        if(next_char=='[')\n            dim=parse_array(varargin{:});\n            count=prod(double(dim));\n        else\n            count=double(parse_number());\n        end\n    end\n    if(~isempty(type))\n        if(count>=0)\n            [object adv]=parse_block(type,count,varargin{:});\n            if(~isempty(dim))\n                object=reshape(object,dim);\n            end\n            pos=pos+adv;\n            return;\n        else\n            endpos=matching_bracket(inStr,pos);\n            [cid,len]=elem_info(type);\n            count=(endpos-pos)/len;\n            [object adv]=parse_block(type,count,varargin{:});\n            pos=pos+adv;\n            parse_char(']');\n            return;\n        end\n    end\n    if next_char ~= ']'\n         while 1\n            val = parse_value(varargin{:});\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            %parse_char(',');\n         end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    if(count==-1)\n        parse_char(']');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    type=inStr(pos);\n    if type ~= 'S' && type ~= 'C' && type ~= 'H'\n        error_pos('String starting with S expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    if(type == 'C')\n        str=inStr(pos);\n        pos=pos+1;\n        return;\n    end\n    bytelen=double(parse_number());\n    if(length(inStr)>=pos+bytelen-1)\n        str=inStr(pos:pos+bytelen-1);\n        pos=pos+bytelen;\n    else\n        error_pos('End of file while expecting end of inStr');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct fileendian systemendian\n    id=strfind('iUIlLdD',inStr(pos));\n    if(isempty(id))\n        error_pos('expecting a number at position %d');\n    end\n    type={'int8','uint8','int16','int32','int64','single','double'};\n    bytelen=[1,1,2,4,8,4,8];\n    datastr=inStr(pos+1:pos+bytelen(id));\n    if(isoct)\n        newdata=int8(datastr);\n    else\n        newdata=uint8(datastr);\n    end\n    if(id<=5 && fileendian~=systemendian)\n        newdata=swapbytes(typecast(newdata,type{id}));\n    end\n    num=typecast(newdata,type{id});\n    pos = pos + bytelen(id)+1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n\n    switch(inStr(pos))\n        case {'S','C','H'}\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'i','U','I','l','L','d','D'}\n            val = parse_number(varargin{:});\n            return;\n        case 'T'\n            val = true;\n            pos = pos + 1;\n            return;\n        case 'F'\n            val = false;\n            pos = pos + 1;\n            return;\n        case {'Z','N'}\n            val = [];\n            pos = pos + 1;\n            return;\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos e1l e1r maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/mergestruct.m",
    "content": "function s=mergestruct(s1,s2)\n%\n% s=mergestruct(s1,s2)\n%\n% merge two struct objects into one\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      s1,s2: a struct object, s1 and s2 can not be arrays\n%\n% output:\n%      s: the merged struct object. fields in s1 and s2 will be combined in s.\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(~isstruct(s1) || ~isstruct(s2))\n    error('input parameters contain non-struct');\nend\nif(length(s1)>1 || length(s2)>1)\n    error('can not merge struct arrays');\nend\nfn=fieldnames(s2);\ns=s1;\nfor i=1:length(fn)              \n    s=setfield(s,fn{i},getfield(s2,fn{i}));\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/savejson.m",
    "content": "function json=savejson(rootname,obj,varargin)\n%\n% json=savejson(rootname,obj,filename)\n%    or\n% json=savejson(rootname,obj,opt)\n% json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n% Object Notation) string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09\n%\n% $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array).\n%      filename: a string for the file name to save the output JSON data.\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n%                         of a 1D/2D array;\n%        opt.ArrayIndent [1|0]: if 1, output explicit data array with\n%                         precedent indentation; if 0, no indentation\n%        opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n%                         to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n%                         and $1 represents the sign. For those who want to use\n%                         1e999 to represent Inf, they can set opt.Inf to '$11e999'\n%        opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n%                         to represent NaN\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSONP='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%        opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n%        opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a string in the JSON format (see http://json.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      savejson('jmesh',jsonmesh)\n%      savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\n\nwhitespaces=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nif(jsonopt('Compact',0,opt)==1)\n    whitespaces=struct('tab','','newline','','sep',',');\nend\nif(~isfield(opt,'whitespaces_'))\n    opt.whitespaces_=whitespaces;\nend\n\nnl=whitespaces.newline;\n\njson=obj2json(rootname,obj,rootlevel,opt);\nif(rootisarray)\n    json=sprintf('%s%s',json,nl);\nelse\n    json=sprintf('{%s%s%s}\\n',nl,json,nl);\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=sprintf('%s(%s);%s',jsonp,json,nl);\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    if(jsonopt('SaveBinary',0,opt)==1)\n\t    fid = fopen(opt.FileName, 'wb');\n\t    fwrite(fid,json);\n    else\n\t    fid = fopen(opt.FileName, 'wt');\n\t    fwrite(fid,json,'char');\n    end\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2json(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2json(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2json(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2json(name,item,level,varargin{:});\nelse\n    txt=mat2json(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2json(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=jsonopt('whitespaces_',struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n')),varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nif(len>1)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": [%s',padding0, checkname(name,varargin{:}),nl); name=''; \n    else\n        txt=sprintf('%s[%s',padding0,nl); \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": []',padding0, checkname(name,varargin{:})); name=''; \n    else\n        txt=sprintf('%s[]',padding0); \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n    for i=1:dim(1)\n       txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:}));\n       if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    end\n    if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n    if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2json(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\npadding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1));\nnl=ws.newline;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding0,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding0,nl); end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n  for i=1:dim(1)\n    names = fieldnames(item(i,j));\n    if(~isempty(name) && len==1)\n        txt=sprintf('%s%s\"%s\": {%s',txt,padding1, checkname(name,varargin{:}),nl); \n    else\n        txt=sprintf('%s%s{%s',txt,padding1,nl); \n    end\n    if(~isempty(names))\n      for e=1:length(names)\n\t    txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:}));\n        if(e<length(names)) txt=sprintf('%s%s',txt,','); end\n        txt=sprintf('%s%s',txt,nl);\n      end\n    end\n    txt=sprintf('%s%s}',txt,padding1);\n    if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n  end\n  if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n  if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2json(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding1,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding1,nl); end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    if(isoct)\n        val=regexprep(item(e,:),'\\\\','\\\\');\n        val=regexprep(val,'\"','\\\"');\n        val=regexprep(val,'^\"','\\\"');\n    else\n        val=regexprep(item(e,:),'\\\\','\\\\\\\\');\n        val=regexprep(val,'\"','\\\\\"');\n        val=regexprep(val,'^\"','\\\\\"');\n    end\n    val=escapejsonstring(val);\n    if(len==1)\n        obj=['\"' checkname(name,varargin{:}) '\": ' '\"',val,'\"'];\n\tif(isempty(name)) obj=['\"',val,'\"']; end\n        txt=sprintf('%s%s%s%s',txt,padding1,obj);\n    else\n        txt=sprintf('%s%s%s%s',txt,padding0,['\"',val,'\"']);\n    end\n    if(e==len) sep=''; end\n    txt=sprintf('%s%s',txt,sep);\nend\nif(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2json(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:}))\n    if(isempty(name))\n    \ttxt=sprintf('%s{%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    else\n    \ttxt=sprintf('%s\"%s\": {%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    end\nelse\n    if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0)\n        numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\\[',''),']','');\n    else\n        numtxt=matdata2json(item,level+1,varargin{:});\n    end\n    if(isempty(name))\n    \ttxt=sprintf('%s%s',padding1,numtxt);\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n           \ttxt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        else\n    \t    txt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        end\n    end\n    return;\nend\ndataformat='%s%s%s%s%s';\n\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n    end\n    txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsSparse_\": ','1', sep);\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([iy(:),data'],level+2,varargin{:}), nl);\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,data],level+2,varargin{:}), nl);\n    else\n        % General case, store row and column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,iy,data],level+2,varargin{:}), nl);\n    end\nelse\n    if(isreal(item))\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json(item(:)',level+2,varargin{:}), nl);\n    else\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl);\n    end\nend\ntxt=sprintf('%s%s%s',txt,padding1,'}');\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2json(mat,level,varargin)\n\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\ntab=ws.tab;\nnl=ws.newline;\n\nif(size(mat,1)==1)\n    pre='';\n    post='';\n    level=level-1;\nelse\n    pre=sprintf('[%s',nl);\n    post=sprintf('%s%s]',nl,repmat(tab,1,level-1));\nend\n\nif(isempty(mat))\n    txt='null';\n    return;\nend\nfloatformat=jsonopt('FloatFormat','%.10g',varargin{:});\n%if(numel(mat)>1)\n    formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]];\n%else\n%    formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\\n')]];\n%end\n\nif(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1)\n    formatstr=[repmat(tab,1,level) formatstr];\nend\n\ntxt=sprintf(formatstr,mat');\ntxt(end-length(nl):end)=[];\nif(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1)\n   txt=regexprep(txt,'1','true');\n   txt=regexprep(txt,'0','false');\nend\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],\\n['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\ntxt=[pre txt post];\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n\n%%-------------------------------------------------------------------------\nfunction newstr=escapejsonstring(str)\nnewstr=str;\nisoct=exist('OCTAVE_VERSION','builtin');\nif(isoct)\n   vv=sscanf(OCTAVE_VERSION,'%f');\n   if(vv(1)>=3.8) isoct=0; end\nend\nif(isoct)\n  escapechars={'\\a','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},escapechars{i});\n  end\nelse\n  escapechars={'\\a','\\b','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\\\','\\\\\\\\'));\n  end\nend\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/saveubjson.m",
    "content": "function json=saveubjson(rootname,obj,varargin)\n%\n% json=saveubjson(rootname,obj,filename)\n%    or\n% json=saveubjson(rootname,obj,opt)\n% json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a Universal \n% Binary JSON (UBJSON) binary string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/17\n%\n% $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array)\n%      filename: a string for the file name to save the output UBJSON data\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSON='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a binary string in the UBJSON format (see http://ubjson.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      saveubjson('jsonmesh',jsonmesh)\n%      saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\njson=obj2ubjson(rootname,obj,rootlevel,opt);\nif(~rootisarray)\n    json=['{' json '}'];\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=[jsonp '(' json ')'];\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    fid = fopen(opt.FileName, 'wb');\n    fwrite(fid,json);\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2ubjson(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2ubjson(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2ubjson(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2ubjson(name,item,level,varargin{:});\nelse\n    txt=mat2ubjson(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2ubjson(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item); % let's handle 1D cell first\nif(len>1) \n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) '[']; name=''; \n    else\n        txt='['; \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; \n    else\n        txt='Z'; \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=[txt '[']; end\n    for i=1:dim(1)\n       txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})];\n    end\n    if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2ubjson(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=[txt '[']; end\n  for i=1:dim(1)\n     names = fieldnames(item(i,j));\n     if(~isempty(name) && len==1)\n        txt=[txt S_(checkname(name,varargin{:})) '{']; \n     else\n        txt=[txt '{']; \n     end\n     if(~isempty(names))\n       for e=1:length(names)\n\t     txt=[txt obj2ubjson(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})];\n       end\n     end\n     txt=[txt '}'];\n  end\n  if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2ubjson(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    val=item(e,:);\n    if(len==1)\n        obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),''];\n\tif(isempty(name)) obj=['',S_(val),'']; end\n        txt=[txt,'',obj];\n    else\n        txt=[txt,'',['',S_(val),'']];\n    end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2ubjson(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) || jsonopt('ArrayToStruct',0,varargin{:}))\n      cid=I_(uint32(max(size(item))));\n      if(isempty(name))\n    \ttxt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ];\n      else\n          if(isempty(item))\n              txt=[S_(checkname(name,varargin{:})),'Z'];\n              return;\n          else\n    \t      txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))];\n          end\n      end\nelse\n    if(isempty(name))\n    \ttxt=matdata2ubjson(item,level+1,varargin{:});\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n            numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\\[',''),']','');\n           \ttxt=[S_(checkname(name,varargin{:})) numtxt];\n        else\n    \t    txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})];\n        end\n    end\n    return;\nend\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=[txt,S_('_ArrayIsComplex_'),'T'];\n    end\n    txt=[txt,S_('_ArrayIsSparse_'),'T'];\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([iy(:),data'],level+2,varargin{:})];\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,data],level+2,varargin{:})];\n    else\n        % General case, store row and column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,iy,data],level+2,varargin{:})];\n    end\nelse\n    if(isreal(item))\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson(item(:)',level+2,varargin{:})];\n    else\n        txt=[txt,S_('_ArrayIsComplex_'),'T'];\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})];\n    end\nend\ntxt=[txt,'}'];\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2ubjson(mat,level,varargin)\nif(isempty(mat))\n    txt='Z';\n    return;\nend\nif(size(mat,1)==1)\n    level=level-1;\nend\ntype='';\nhasnegtive=(mat<0);\nif(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0)))\n    if(isempty(hasnegtive))\n       if(max(mat(:))<=2^8)\n           type='U';\n       end\n    end\n    if(isempty(type))\n        % todo - need to consider negative ones separately\n        id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]);\n        if(isempty(find(id)))\n            error('high-precision data is not yet supported');\n        end\n        key='iIlL';\n\ttype=key(find(id));\n    end\n    txt=[I_a(mat(:),type,size(mat))];\nelseif(islogical(mat))\n    logicalval='FT';\n    if(numel(mat)==1)\n        txt=logicalval(mat+1);\n    else\n        txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')];\n    end\nelse\n    if(numel(mat)==1)\n        txt=['[' D_(mat) ']'];\n    else\n        txt=D_a(mat(:),'D',size(mat));\n    end\nend\n\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n%%-------------------------------------------------------------------------\nfunction val=S_(str)\nif(length(str)==1)\n  val=['C' str];\nelse\n  val=['S' I_(int32(length(str))) str];\nend\n%%-------------------------------------------------------------------------\nfunction val=I_(num)\nif(~isinteger(num))\n    error('input is not an integer');\nend\nif(num>=0 && num<255)\n   val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')];\n   return;\nend\nkey='iIlL';\ncid={'int8','int16','int32','int64'};\nfor i=1:4\n  if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1)))\n    val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')];\n    return;\n  end\nend\nerror('unsupported integer');\n\n%%-------------------------------------------------------------------------\nfunction val=D_(num)\nif(~isfloat(num))\n    error('input is not a float');\nend\n\nif(isa(num,'single'))\n  val=['d' data2byte(num,'uint8')];\nelse\n  val=['D' data2byte(num,'uint8')];\nend\n%%-------------------------------------------------------------------------\nfunction data=I_a(num,type,dim,format)\nid=find(ismember('iUIlL',type));\n\nif(id==0)\n  error('unsupported integer array');\nend\n\n% based on UBJSON specs, all integer types are stored in big endian format\n\nif(id==1)\n  data=data2byte(swapbytes(int8(num)),'uint8');\n  blen=1;\nelseif(id==2)\n  data=data2byte(swapbytes(uint8(num)),'uint8');\n  blen=1;\nelseif(id==3)\n  data=data2byte(swapbytes(int16(num)),'uint8');\n  blen=2;\nelseif(id==4)\n  data=data2byte(swapbytes(int32(num)),'uint8');\n  blen=4;\nelseif(id==5)\n  data=data2byte(swapbytes(int64(num)),'uint8');\n  blen=8;\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/blen)) data(:)'];\n  end\n  data=['[' data(:)'];\nelse\n  data=reshape(data,blen,numel(data)/blen);\n  data(2:blen+1,:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction data=D_a(num,type,dim,format)\nid=find(ismember('dD',type));\n\nif(id==0)\n  error('unsupported float array');\nend\n\nif(id==1)\n  data=data2byte(single(num),'uint8');\nelseif(id==2)\n  data=data2byte(double(num),'uint8');\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)'];\n  end\n  data=['[' data];\nelse\n  data=reshape(data,(id*4),length(data)/(id*4));\n  data(2:(id*4+1),:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction bytes=data2byte(varargin)\nbytes=typecast(varargin{:});\nbytes=bytes(:)';\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/jsonlab/varargin2struct.m",
    "content": "function opt=varargin2struct(varargin)\n%\n% opt=varargin2struct('param1',value1,'param2',value2,...)\n%   or\n% opt=varargin2struct(...,optstruct,...)\n%\n% convert a series of input parameters into a structure\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      'param', value: the input parameters should be pairs of a string and a value\n%       optstruct: if a parameter is a struct, the fields will be merged to the output struct\n%\n% output:\n%      opt: a struct where opt.param1=value1, opt.param2=value2 ...\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nlen=length(varargin);\nopt=struct;\nif(len==0) return; end\ni=1;\nwhile(i<=len)\n    if(isstruct(varargin{i}))\n        opt=mergestruct(opt,varargin{i});\n    elseif(ischar(varargin{i}) && i<len)\n        opt=setfield(opt,varargin{i},varargin{i+1});\n        i=i+1;\n    else\n        error('input must be in the form of ...,''name'',value,... pairs or structs');\n    end\n    i=i+1;\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/makeValidFieldName.m",
    "content": "function str = makeValidFieldName(str)\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    isoct=exist('OCTAVE_VERSION','builtin');\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n"
  },
  {
    "path": "machine-learning-ex7/ex7/lib/submitWithConfiguration.m",
    "content": "function submitWithConfiguration(conf)\n  addpath('./lib/jsonlab');\n\n  parts = parts(conf);\n\n  fprintf('== Submitting solutions | %s...\\n', conf.itemName);\n\n  tokenFile = 'token.mat';\n  if exist(tokenFile, 'file')\n    load(tokenFile);\n    [email token] = promptToken(email, token, tokenFile);\n  else\n    [email token] = promptToken('', '', tokenFile);\n  end\n\n  if isempty(token)\n    fprintf('!! Submission Cancelled\\n');\n    return\n  end\n\n  try\n    response = submitParts(conf, email, token, parts);\n  catch\n    e = lasterror();\n    fprintf( ...\n      '!! Submission failed: unexpected error: %s\\n', ...\n      e.message);\n    fprintf('!! Please try again later.\\n');\n    return\n  end\n\n  if isfield(response, 'errorMessage')\n    fprintf('!! Submission failed: %s\\n', response.errorMessage);\n  else\n    showFeedback(parts, response);\n    save(tokenFile, 'email', 'token');\n  end\nend\n\nfunction [email token] = promptToken(email, existingToken, tokenFile)\n  if (~isempty(email) && ~isempty(existingToken))\n    prompt = sprintf( ...\n      'Use token from last successful submission (%s)? (Y/n): ', ...\n      email);\n    reenter = input(prompt, 's');\n\n    if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y')\n      token = existingToken;\n      return;\n    else\n      delete(tokenFile);\n    end\n  end\n  email = input('Login (email address): ', 's');\n  token = input('Token: ', 's');\nend\n\nfunction isValid = isValidPartOptionIndex(partOptions, i)\n  isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions));\nend\n\nfunction response = submitParts(conf, email, token, parts)\n  body = makePostBody(conf, email, token, parts);\n  submissionUrl = submissionUrl();\n  params = {'jsonBody', body};\n  responseBody = urlread(submissionUrl, 'post', params);\n  response = loadjson(responseBody);\nend\n\nfunction body = makePostBody(conf, email, token, parts)\n  bodyStruct.assignmentSlug = conf.assignmentSlug;\n  bodyStruct.submitterEmail = email;\n  bodyStruct.secret = token;\n  bodyStruct.parts = makePartsStruct(conf, parts);\n\n  opt.Compact = 1;\n  body = savejson('', bodyStruct, opt);\nend\n\nfunction partsStruct = makePartsStruct(conf, parts)\n  for part = parts\n    partId = part{:}.id;\n    fieldName = makeValidFieldName(partId);\n    outputStruct.output = conf.output(partId);\n    partsStruct.(fieldName) = outputStruct;\n  end\nend\n\nfunction [parts] = parts(conf)\n  parts = {};\n  for partArray = conf.partArrays\n    part.id = partArray{:}{1};\n    part.sourceFiles = partArray{:}{2};\n    part.name = partArray{:}{3};\n    parts{end + 1} = part;\n  end\nend\n\nfunction showFeedback(parts, response)\n  fprintf('== \\n');\n  fprintf('== %43s | %9s | %-s\\n', 'Part Name', 'Score', 'Feedback');\n  fprintf('== %43s | %9s | %-s\\n', '---------', '-----', '--------');\n  for part = parts\n    score = '';\n    partFeedback = '';\n    partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id));\n    partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id));\n    score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore);\n    fprintf('== %43s | %9s | %-s\\n', part{:}.name, score, partFeedback);\n  end\n  evaluation = response.evaluation;\n  totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore);\n  fprintf('==                                   --------------------------------\\n');\n  fprintf('== %43s | %9s | %-s\\n', '', totalScore, '');\n  fprintf('== \\n');\nend\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%\n% Service configuration\n%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\nfunction submissionUrl = submissionUrl()\n  submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1';\nend\n"
  },
  {
    "path": "machine-learning-ex7/ex7/pca.m",
    "content": "function [U, S] = pca(X)\n%PCA Run principal component analysis on the dataset X\n%   [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X\n%   Returns the eigenvectors U, the eigenvalues (on diagonal) in S\n%\n\n% Useful values\n[m, n] = size(X);\n\n% You need to return the following variables correctly.\nU = zeros(n);\nS = zeros(n);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: You should first compute the covariance matrix. Then, you\n%               should use the \"svd\" function to compute the eigenvectors\n%               and eigenvalues of the covariance matrix. \n%\n% Note: When computing the covariance matrix, remember to divide by m (the\n%       number of examples).\n%\n\nSigma = X'*X/m;\n\n[U,S] = svd(Sigma);\n\n\n\n\n\n% =========================================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex7/ex7/plotDataPoints.m",
    "content": "function plotDataPoints(X, idx, K)\n%PLOTDATAPOINTS plots data points in X, coloring them so that those with the same\n%index assignments in idx have the same color\n%   PLOTDATAPOINTS(X, idx, K) plots data points in X, coloring them so that those \n%   with the same index assignments in idx have the same color\n\n% Create palette\npalette = hsv(K + 1);\ncolors = palette(idx, :);\n\n% Plot the data\nscatter(X(:,1), X(:,2), 15, colors);\n\nend\n"
  },
  {
    "path": "machine-learning-ex7/ex7/plotProgresskMeans.m",
    "content": "function plotProgresskMeans(X, centroids, previous, idx, K, i)\n%PLOTPROGRESSKMEANS is a helper function that displays the progress of \n%k-Means as it is running. It is intended for use only with 2D data.\n%   PLOTPROGRESSKMEANS(X, centroids, previous, idx, K, i) plots the data\n%   points with colors assigned to each centroid. With the previous\n%   centroids, it also plots a line between the previous locations and\n%   current locations of the centroids.\n%\n\n% Plot the examples\nplotDataPoints(X, idx, K);\n\n% Plot the centroids as black x's\nplot(centroids(:,1), centroids(:,2), 'x', ...\n     'MarkerEdgeColor','k', ...\n     'MarkerSize', 10, 'LineWidth', 3);\n\n% Plot the history of the centroids with lines\nfor j=1:size(centroids,1)\n    drawLine(centroids(j, :), previous(j, :));\nend\n\n% Title\ntitle(sprintf('Iteration number %d', i))\n\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/projectData.m",
    "content": "function Z = projectData(X, U, K)\n%PROJECTDATA Computes the reduced data representation when projecting only \n%on to the top k eigenvectors\n%   Z = projectData(X, U, K) computes the projection of \n%   the normalized inputs X into the reduced dimensional space spanned by\n%   the first K columns of U. It returns the projected examples in Z.\n%\n\n% You need to return the following variables correctly.\nZ = zeros(size(X, 1), K);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the projection of the data using only the top K \n%               eigenvectors in U (first K columns). \n%               For the i-th example X(i,:), the projection on to the k-th \n%               eigenvector is given as follows:\n%                    x = X(i, :)';\n%                    projection_k = x' * U(:, k);\n%\n\nU_reduce = U(:,1:K);\nZ = X*U_reduce;\n\n\n% =============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex7/ex7/recoverData.m",
    "content": "function X_rec = recoverData(Z, U, K)\n%RECOVERDATA Recovers an approximation of the original data when using the \n%projected data\n%   X_rec = RECOVERDATA(Z, U, K) recovers an approximation the \n%   original data that has been reduced to K dimensions. It returns the\n%   approximate reconstruction in X_rec.\n%\n\n% You need to return the following variables correctly.\nX_rec = zeros(size(Z, 1), size(U, 1));\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the approximation of the data by projecting back\n%               onto the original space using the top K eigenvectors in U.\n%\n%               For the i-th example Z(i,:), the (approximate)\n%               recovered data for dimension j is given as follows:\n%                    v = Z(i, :)';\n%                    recovered_j = v' * U(j, 1:K)';\n%\n%               Notice that U(j, 1:K) is a row vector.\n%          \nU_reduce = U(:,1:K);\nX_rec = Z*U_reduce';\n\n\n% =============================================================\n\nend\n"
  },
  {
    "path": "machine-learning-ex7/ex7/runkMeans.m",
    "content": "function [centroids, idx] = runkMeans(X, initial_centroids, ...\n                                      max_iters, plot_progress)\n%RUNKMEANS runs the K-Means algorithm on data matrix X, where each row of X\n%is a single example\n%   [centroids, idx] = RUNKMEANS(X, initial_centroids, max_iters, ...\n%   plot_progress) runs the K-Means algorithm on data matrix X, where each \n%   row of X is a single example. It uses initial_centroids used as the\n%   initial centroids. max_iters specifies the total number of interactions \n%   of K-Means to execute. plot_progress is a true/false flag that \n%   indicates if the function should also plot its progress as the \n%   learning happens. This is set to false by default. runkMeans returns \n%   centroids, a Kxn matrix of the computed centroids and idx, a m x 1 \n%   vector of centroid assignments (i.e. each entry in range [1..K])\n%\n\n% Set default value for plot progress\nif ~exist('plot_progress', 'var') || isempty(plot_progress)\n    plot_progress = false;\nend\n\n% Plot the data if we are plotting progress\nif plot_progress\n    figure;\n    hold on;\nend\n\n% Initialize values\n[m n] = size(X);\nK = size(initial_centroids, 1);\ncentroids = initial_centroids;\nprevious_centroids = centroids;\nidx = zeros(m, 1);\n\n% Run K-Means\nfor i=1:max_iters\n    \n    % Output progress\n    fprintf('K-Means iteration %d/%d...\\n', i, max_iters);\n    if exist('OCTAVE_VERSION')\n        fflush(stdout);\n    end\n    \n    % For each example in X, assign it to the closest centroid\n    idx = findClosestCentroids(X, centroids);\n    \n    % Optionally, plot progress here\n    if plot_progress\n        plotProgresskMeans(X, centroids, previous_centroids, idx, K, i);\n        previous_centroids = centroids;\n        fprintf('Press enter to continue.\\n');\n        pause;\n    end\n    \n    % Given the memberships, compute new centroids\n    centroids = computeCentroids(X, idx, K);\nend\n\n% Hold off if we are plotting progress\nif plot_progress\n    hold off;\nend\n\nend\n\n"
  },
  {
    "path": "machine-learning-ex7/ex7/submit.m",
    "content": "function submit()\n  addpath('./lib');\n\n  conf.assignmentSlug = 'k-means-clustering-and-pca';\n  conf.itemName = 'K-Means Clustering and PCA';\n  conf.partArrays = { ...\n    { ...\n      '1', ...\n      { 'findClosestCentroids.m' }, ...\n      'Find Closest Centroids (k-Means)', ...\n    }, ...\n    { ...\n      '2', ...\n      { 'computeCentroids.m' }, ...\n      'Compute Centroid Means (k-Means)', ...\n    }, ...\n    { ...\n      '3', ...\n      { 'pca.m' }, ...\n      'PCA', ...\n    }, ...\n    { ...\n      '4', ...\n      { 'projectData.m' }, ...\n      'Project Data (PCA)', ...\n    }, ...\n    { ...\n      '5', ...\n      { 'recoverData.m' }, ...\n      'Recover Data (PCA)', ...\n    }, ...\n  };\n  conf.output = @output;\n\n  submitWithConfiguration(conf);\nend\n\nfunction out = output(partId, auxstring)\n  % Random Test Cases\n  X = reshape(sin(1:165), 15, 11);\n  Z = reshape(cos(1:121), 11, 11);\n  C = Z(1:5, :);\n  idx = (1 + mod(1:15, 3))';\n  if partId == '1'\n    idx = findClosestCentroids(X, C);\n    out = sprintf('%0.5f ', idx(:));\n  elseif partId == '2'\n    centroids = computeCentroids(X, idx, 3);\n    out = sprintf('%0.5f ', centroids(:));\n  elseif partId == '3'\n    [U, S] = pca(X);\n    out = sprintf('%0.5f ', abs([U(:); S(:)]));\n  elseif partId == '4'\n    X_proj = projectData(X, Z, 5);\n    out = sprintf('%0.5f ', X_proj(:));\n  elseif partId == '5'\n    X_rec = recoverData(X(:,1:5), Z, 5);\n    out = sprintf('%0.5f ', X_rec(:));\n  end \nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/checkCostFunction.m",
    "content": "function checkCostFunction(lambda)\n%CHECKCOSTFUNCTION Creates a collaborative filering problem \n%to check your cost function and gradients\n%   CHECKCOSTFUNCTION(lambda) Creates a collaborative filering problem \n%   to check your cost function and gradients, it will output the \n%   analytical gradients produced by your code and the numerical gradients \n%   (computed using computeNumericalGradient). These two gradient \n%   computations should result in very similar values.\n\n% Set lambda\nif ~exist('lambda', 'var') || isempty(lambda)\n    lambda = 0;\nend\n\n%% Create small problem\nX_t = rand(4, 3);\nTheta_t = rand(5, 3);\n\n% Zap out most entries\nY = X_t * Theta_t';\nY(rand(size(Y)) > 0.5) = 0;\nR = zeros(size(Y));\nR(Y ~= 0) = 1;\n\n%% Run Gradient Checking\nX = randn(size(X_t));\nTheta = randn(size(Theta_t));\nnum_users = size(Y, 2);\nnum_movies = size(Y, 1);\nnum_features = size(Theta_t, 2);\n\nnumgrad = computeNumericalGradient( ...\n                @(t) cofiCostFunc(t, Y, R, num_users, num_movies, ...\n                                num_features, lambda), [X(:); Theta(:)]);\n\n[cost, grad] = cofiCostFunc([X(:); Theta(:)],  Y, R, num_users, ...\n                          num_movies, num_features, lambda);\n\ndisp([numgrad grad]);\nfprintf(['The above two columns you get should be very similar.\\n' ...\n         '(Left-Your Numerical Gradient, Right-Analytical Gradient)\\n\\n']);\n\ndiff = norm(numgrad-grad)/norm(numgrad+grad);\nfprintf(['If your backpropagation implementation is correct, then \\n' ...\n         'the relative difference will be small (less than 1e-9). \\n' ...\n         '\\nRelative Difference: %g\\n'], diff);\n\nend"
  },
  {
    "path": "machine-learning-ex8/ex8/cofiCostFunc.m",
    "content": "function [J, grad] = cofiCostFunc(params, Y, R, num_users, num_movies, ...\n                                  num_features, lambda)\n%COFICOSTFUNC Collaborative filtering cost function\n%   [J, grad] = COFICOSTFUNC(params, Y, R, num_users, num_movies, ...\n%   num_features, lambda) returns the cost and gradient for the\n%   collaborative filtering problem.\n%\n\n% Unfold the U and W matrices from params\nX = reshape(params(1:num_movies*num_features), num_movies, num_features);\nTheta = reshape(params(num_movies*num_features+1:end), ...\n                num_users, num_features);\n\n            \n% You need to return the following values correctly\nJ = 0;\nX_grad = zeros(size(X));\nTheta_grad = zeros(size(Theta));\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the cost function and gradient for collaborative\n%               filtering. Concretely, you should first implement the cost\n%               function (without regularization) and make sure it is\n%               matches our costs. After that, you should implement the \n%               gradient and use the checkCostFunction routine to check\n%               that the gradient is correct. Finally, you should implement\n%               regularization.\n%\n% Notes: X - num_movies  x num_features matrix of movie features\n%        Theta - num_users  x num_features matrix of user features\n%        Y - num_movies x num_users matrix of user ratings of movies\n%        R - num_movies x num_users matrix, where R(i, j) = 1 if the \n%            i-th movie was rated by the j-th user\n%\n% You should set the following variables correctly:\n%\n%        X_grad - num_movies x num_features matrix, containing the \n%                 partial derivatives w.r.t. to each element of X\n%        Theta_grad - num_users x num_features matrix, containing the \n%                     partial derivatives w.r.t. to each element of Theta\n%\n\nterm = X*Theta'.*R-Y.*R;\nJ = (term(:)'*term(:)+lambda*Theta(:)'*Theta(:)+lambda*X(:)'*X(:))/2;\n\n\n\nX_grad = term*Theta+lambda*X;\nTheta_grad = term'*X+lambda*Theta;\n\n\n\n\n\n\n\n\n\n% =============================================================\n\ngrad = [X_grad(:); Theta_grad(:)];\n\nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/computeNumericalGradient.m",
    "content": "function numgrad = computeNumericalGradient(J, theta)\n%COMPUTENUMERICALGRADIENT Computes the gradient using \"finite differences\"\n%and gives us a numerical estimate of the gradient.\n%   numgrad = COMPUTENUMERICALGRADIENT(J, theta) computes the numerical\n%   gradient of the function J around theta. Calling y = J(theta) should\n%   return the function value at theta.\n\n% Notes: The following code implements numerical gradient checking, and \n%        returns the numerical gradient.It sets numgrad(i) to (a numerical \n%        approximation of) the partial derivative of J with respect to the \n%        i-th input argument, evaluated at theta. (i.e., numgrad(i) should \n%        be the (approximately) the partial derivative of J with respect \n%        to theta(i).)\n%                \n\nnumgrad = zeros(size(theta));\nperturb = zeros(size(theta));\ne = 1e-4;\nfor p = 1:numel(theta)\n    % Set perturbation vector\n    perturb(p) = e;\n    loss1 = J(theta - perturb);\n    loss2 = J(theta + perturb);\n    % Compute Numerical Gradient\n    numgrad(p) = (loss2 - loss1) / (2*e);\n    perturb(p) = 0;\nend\n\nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/estimateGaussian.m",
    "content": "function [mu sigma2] = estimateGaussian(X)\n%ESTIMATEGAUSSIAN This function estimates the parameters of a \n%Gaussian distribution using the data in X\n%   [mu sigma2] = estimateGaussian(X), \n%   The input X is the dataset with each n-dimensional data point in one row\n%   The output is an n-dimensional vector mu, the mean of the data set\n%   and the variances sigma^2, an n x 1 vector\n% \n\n% Useful variables\n[m, n] = size(X);\n\n% You should return these values correctly\nmu = zeros(n, 1);\nsigma2 = zeros(n, 1);\n\n% ====================== YOUR CODE HERE ======================\n% Instructions: Compute the mean of the data and the variances\n%               In particular, mu(i) should contain the mean of\n%               the data for the i-th feature and sigma2(i)\n%               should contain variance of the i-th feature.\n%\n\nmu = mean(X,1)';            %ÿеƽֵתΪ\nsigma2 = ((m-1)*var(X)/m)'; %var(X)ÿеķ,ҪעmĹϵ\n\n\n\n\n\n\n\n\n% =============================================================\n\n\nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/ex8.m",
    "content": "%% Machine Learning Online Class\n%  Exercise 8 | Anomaly Detection and Collaborative Filtering\n%\n%  Instructions\n%  ------------\n%\n%  This file contains code that helps you get started on the\n%  exercise. You will need to complete the following functions:\n%\n%     estimateGaussian.m\n%     selectThreshold.m\n%     cofiCostFunc.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% Initialization\nclear ; close all; clc\n\n%% ================== Part 1: Load Example Dataset  ===================\n%  We start this exercise by using a small dataset that is easy to\n%  visualize.\n%\n%  Our example case consists of 2 network server statistics across\n%  several machines: the latency and throughput of each machine.\n%  This exercise will help us find possibly faulty (or very fast) machines.\n%\n\nfprintf('Visualizing example dataset for outlier detection.\\n\\n');\n\n%  The following command loads the dataset. You should now have the\n%  variables X, Xval, yval in your environment\nload('ex8data1.mat');\n\n%  Visualize the example dataset\nplot(X(:, 1), X(:, 2), 'bx');\naxis([0 30 0 30]);\nxlabel('Latency (ms)');\nylabel('Throughput (mb/s)');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause\n\n\n%% ================== Part 2: Estimate the dataset statistics ===================\n%  For this exercise, we assume a Gaussian distribution for the dataset.\n%\n%  We first estimate the parameters of our assumed Gaussian distribution, \n%  then compute the probabilities for each of the points and then visualize \n%  both the overall distribution and where each of the points falls in \n%  terms of that distribution.\n%\nfprintf('Visualizing Gaussian fit.\\n\\n');\n\n%  Estimate my and sigma2\n[mu sigma2] = estimateGaussian(X);\n\n%  Returns the density of the multivariate normal at each data point (row) \n%  of X\np = multivariateGaussian(X, mu, sigma2);\n\n%  Visualize the fit\nvisualizeFit(X,  mu, sigma2);\nxlabel('Latency (ms)');\nylabel('Throughput (mb/s)');\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ================== Part 3: Find Outliers ===================\n%  Now you will find a good epsilon threshold using a cross-validation set\n%  probabilities given the estimated Gaussian distribution\n% \n\npval = multivariateGaussian(Xval, mu, sigma2);\n\n[epsilon F1] = selectThreshold(yval, pval);\nfprintf('Best epsilon found using cross-validation: %e\\n', epsilon);\nfprintf('Best F1 on Cross Validation Set:  %f\\n', F1);\nfprintf('   (you should see a value epsilon of about 8.99e-05)\\n\\n');\n\n%  Find the outliers in the training set and plot the\noutliers = find(p < epsilon);\n\n%  Draw a red circle around those outliers\nhold on\nplot(X(outliers, 1), X(outliers, 2), 'ro', 'LineWidth', 2, 'MarkerSize', 10);\nhold off\n\nfprintf('Program paused. Press enter to continue.\\n');\npause;\n\n%% ================== Part 4: Multidimensional Outliers ===================\n%  We will now use the code from the previous part and apply it to a \n%  harder problem in which more features describe each datapoint and only \n%  some features indicate whether a point is an outlier.\n%\n\n%  Loads the second dataset. You should now have the\n%  variables X, Xval, yval in your environment\nload('ex8data2.mat');\n\n%  Apply the same steps to the larger dataset\n[mu sigma2] = estimateGaussian(X);\n\n%  Training set \np = multivariateGaussian(X, mu, sigma2);\n\n%  Cross-validation set\npval = multivariateGaussian(Xval, mu, sigma2);\n\n%  Find the best threshold\n[epsilon F1] = selectThreshold(yval, pval);\n\nfprintf('Best epsilon found using cross-validation: %e\\n', epsilon);\nfprintf('Best F1 on Cross Validation Set:  %f\\n', F1);\nfprintf('# Outliers found: %d\\n', sum(p < epsilon));\nfprintf('   (you should see a value epsilon of about 1.38e-18)\\n\\n');\npause\n\n\n\n"
  },
  {
    "path": "machine-learning-ex8/ex8/ex8_cofi.m",
    "content": "%% Machine Learning Online Class\n%  Exercise 8 | Anomaly Detection and Collaborative Filtering\n%\n%  Instructions\n%  ------------\n%\n%  This file contains code that helps you get started on the\n%  exercise. You will need to complete the following functions:\n%\n%     estimateGaussian.m\n%     selectThreshold.m\n%     cofiCostFunc.m\n%\n%  For this exercise, you will not need to change any code in this file,\n%  or any other files other than those mentioned above.\n%\n\n%% =============== Part 1: Loading movie ratings dataset ================\n%  You will start by loading the movie ratings dataset to understand the\n%  structure of the data.\n%  \nfprintf('Loading movie ratings dataset.\\n\\n');\n\n%  Load data\nload ('ex8_movies.mat');\n\n%  Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies on \n%  943 users\n%\n%  R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a\n%  rating to movie i\n\n%  From the matrix, we can compute statistics like average rating.\nfprintf('Average rating for movie 1 (Toy Story): %f / 5\\n\\n', ...\n        mean(Y(1, R(1, :))));\n\n%  We can \"visualize\" the ratings matrix by plotting it with imagesc\nimagesc(Y);\nylabel('Movies');\nxlabel('Users');\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n%% ============ Part 2: Collaborative Filtering Cost Function ===========\n%  You will now implement the cost function for collaborative filtering.\n%  To help you debug your cost function, we have included set of weights\n%  that we trained on that. Specifically, you should complete the code in \n%  cofiCostFunc.m to return J.\n\n%  Load pre-trained weights (X, Theta, num_users, num_movies, num_features)\nload ('ex8_movieParams.mat');\n\n%  Reduce the data set size so that this runs faster\nnum_users = 4; num_movies = 5; num_features = 3;\nX = X(1:num_movies, 1:num_features);\nTheta = Theta(1:num_users, 1:num_features);\nY = Y(1:num_movies, 1:num_users);\nR = R(1:num_movies, 1:num_users);\n\n%  Evaluate cost function\nJ = cofiCostFunc([X(:) ; Theta(:)], Y, R, num_users, num_movies, ...\n               num_features, 0);\n           \nfprintf(['Cost at loaded parameters: %f '...\n         '\\n(this value should be about 22.22)\\n'], J);\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n\n%% ============== Part 3: Collaborative Filtering Gradient ==============\n%  Once your cost function matches up with ours, you should now implement \n%  the collaborative filtering gradient function. Specifically, you should \n%  complete the code in cofiCostFunc.m to return the grad argument.\n%  \nfprintf('\\nChecking Gradients (without regularization) ... \\n');\n\n%  Check gradients by running checkNNGradients\ncheckCostFunction;\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n\n%% ========= Part 4: Collaborative Filtering Cost Regularization ========\n%  Now, you should implement regularization for the cost function for \n%  collaborative filtering. You can implement it by adding the cost of\n%  regularization to the original cost computation.\n%  \n\n%  Evaluate cost function\nJ = cofiCostFunc([X(:) ; Theta(:)], Y, R, num_users, num_movies, ...\n               num_features, 1.5);\n           \nfprintf(['Cost at loaded parameters (lambda = 1.5): %f '...\n         '\\n(this value should be about 31.34)\\n'], J);\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n\n%% ======= Part 5: Collaborative Filtering Gradient Regularization ======\n%  Once your cost matches up with ours, you should proceed to implement \n%  regularization for the gradient. \n%\n\n%  \nfprintf('\\nChecking Gradients (with regularization) ... \\n');\n\n%  Check gradients by running checkNNGradients\ncheckCostFunction(1.5);\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n\n%% ============== Part 6: Entering ratings for a new user ===============\n%  Before we will train the collaborative filtering model, we will first\n%  add ratings that correspond to a new user that we just observed. This\n%  part of the code will also allow you to put in your own ratings for the\n%  movies in our dataset!\n%\nmovieList = loadMovieList();\n\n%  Initialize my ratings\nmy_ratings = zeros(1682, 1);\n\n% Check the file movie_idx.txt for id of each movie in our dataset\n% For example, Toy Story (1995) has ID 1, so to rate it \"4\", you can set\nmy_ratings(1) = 4;\n\n% Or suppose did not enjoy Silence of the Lambs (1991), you can set\nmy_ratings(98) = 2;\n\n% We have selected a few movies we liked / did not like and the ratings we\n% gave are as follows:\nmy_ratings(7) = 3;\nmy_ratings(12)= 5;\nmy_ratings(54) = 4;\nmy_ratings(64)= 5;\nmy_ratings(66)= 3;\nmy_ratings(69) = 5;\nmy_ratings(183) = 4;\nmy_ratings(226) = 5;\nmy_ratings(355)= 5;\n\nfprintf('\\n\\nNew user ratings:\\n');\nfor i = 1:length(my_ratings)\n    if my_ratings(i) > 0 \n        fprintf('Rated %d for %s\\n', my_ratings(i), ...\n                 movieList{i});\n    end\nend\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n\n%% ================== Part 7: Learning Movie Ratings ====================\n%  Now, you will train the collaborative filtering model on a movie rating \n%  dataset of 1682 movies and 943 users\n%\n\nfprintf('\\nTraining collaborative filtering...\\n');\n\n%  Load data\nload('ex8_movies.mat');\n\n%  Y is a 1682x943 matrix, containing ratings (1-5) of 1682 movies by \n%  943 users\n%\n%  R is a 1682x943 matrix, where R(i,j) = 1 if and only if user j gave a\n%  rating to movie i\n\n%  Add our own ratings to the data matrix\nY = [my_ratings Y];\nR = [(my_ratings ~= 0) R];\n\n%  Normalize Ratings\n[Ynorm, Ymean] = normalizeRatings(Y, R);\n\n%  Useful Values\nnum_users = size(Y, 2);\nnum_movies = size(Y, 1);\nnum_features = 10;\n\n% Set Initial Parameters (Theta, X)\nX = randn(num_movies, num_features);\nTheta = randn(num_users, num_features);\n\ninitial_parameters = [X(:); Theta(:)];\n\n% Set options for fmincg\noptions = optimset('GradObj', 'on', 'MaxIter', 100);\n\n% Set Regularization\nlambda = 10;\ntheta = fmincg (@(t)(cofiCostFunc(t, Y, R, num_users, num_movies, ...\n                                num_features, lambda)), ...\n                initial_parameters, options);\n\n% Unfold the returned theta back into U and W\nX = reshape(theta(1:num_movies*num_features), num_movies, num_features);\nTheta = reshape(theta(num_movies*num_features+1:end), ...\n                num_users, num_features);\n\nfprintf('Recommender system learning completed.\\n');\n\nfprintf('\\nProgram paused. Press enter to continue.\\n');\npause;\n\n%% ================== Part 8: Recommendation for you ====================\n%  After training the model, you can now make recommendations by computing\n%  the predictions matrix.\n%\n\np = X * Theta';\nmy_predictions = p(:,1) + Ymean;\n\nmovieList = loadMovieList();\n\n[r, ix] = sort(my_predictions, 'descend');\nfprintf('\\nTop recommendations for you:\\n');\nfor i=1:10\n    j = ix(i);\n    fprintf('Predicting rating %.1f for movie %s\\n', my_predictions(j), ...\n            movieList{j});\nend\n\nfprintf('\\n\\nOriginal ratings provided:\\n');\nfor i = 1:length(my_ratings)\n    if my_ratings(i) > 0 \n        fprintf('Rated %d for %s\\n', my_ratings(i), ...\n                 movieList{i});\n    end\nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/fmincg.m",
    "content": "function [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)\n% Minimize a continuous differentialble multivariate function. Starting point\n% is given by \"X\" (D by 1), and the function named in the string \"f\", must\n% return a function value and a vector of partial derivatives. The Polack-\n% Ribiere flavour of conjugate gradients is used to compute search directions,\n% and a line search using quadratic and cubic polynomial approximations and the\n% Wolfe-Powell stopping criteria is used together with the slope ratio method\n% for guessing initial step sizes. Additionally a bunch of checks are made to\n% make sure that exploration is taking place and that extrapolation will not\n% be unboundedly large. The \"length\" gives the length of the run: if it is\n% positive, it gives the maximum number of line searches, if negative its\n% absolute gives the maximum allowed number of function evaluations. You can\n% (optionally) give \"length\" a second component, which will indicate the\n% reduction in function value to be expected in the first line-search (defaults\n% to 1.0). The function returns when either its length is up, or if no further\n% progress can be made (ie, we are at a minimum, or so close that due to\n% numerical problems, we cannot get any closer). If the function terminates\n% within a few iterations, it could be an indication that the function value\n% and derivatives are not consistent (ie, there may be a bug in the\n% implementation of your \"f\" function). The function returns the found\n% solution \"X\", a vector of function values \"fX\" indicating the progress made\n% and \"i\" the number of iterations (line searches or function evaluations,\n% depending on the sign of \"length\") used.\n%\n% Usage: [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)\n%\n% See also: checkgrad \n%\n% Copyright (C) 2001 and 2002 by Carl Edward Rasmussen. Date 2002-02-13\n%\n%\n% (C) Copyright 1999, 2000 & 2001, Carl Edward Rasmussen\n% \n% Permission is granted for anyone to copy, use, or modify these\n% programs and accompanying documents for purposes of research or\n% education, provided this copyright notice is retained, and note is\n% made of any changes that have been made.\n% \n% These programs and documents are distributed without any warranty,\n% express or implied.  As the programs were written for research\n% purposes only, they have not been tested to the degree that would be\n% advisable in any important application.  All use of these programs is\n% entirely at the user's own risk.\n%\n% [ml-class] Changes Made:\n% 1) Function name and argument specifications\n% 2) Output display\n%\n\n% Read options\nif exist('options', 'var') && ~isempty(options) && isfield(options, 'MaxIter')\n    length = options.MaxIter;\nelse\n    length = 100;\nend\n\n\nRHO = 0.01;                            % a bunch of constants for line searches\nSIG = 0.5;       % RHO and SIG are the constants in the Wolfe-Powell conditions\nINT = 0.1;    % don't reevaluate within 0.1 of the limit of the current bracket\nEXT = 3.0;                    % extrapolate maximum 3 times the current bracket\nMAX = 20;                         % max 20 function evaluations per line search\nRATIO = 100;                                      % maximum allowed slope ratio\n\nargstr = ['feval(f, X'];                      % compose string used to call function\nfor i = 1:(nargin - 3)\n  argstr = [argstr, ',P', int2str(i)];\nend\nargstr = [argstr, ')'];\n\nif max(size(length)) == 2, red=length(2); length=length(1); else red=1; end\nS=['Iteration '];\n\ni = 0;                                            % zero the run length counter\nls_failed = 0;                             % no previous line search has failed\nfX = [];\n[f1 df1] = eval(argstr);                      % get function value and gradient\ni = i + (length<0);                                            % count epochs?!\ns = -df1;                                        % search direction is steepest\nd1 = -s'*s;                                                 % this is the slope\nz1 = red/(1-d1);                                  % initial step is red/(|s|+1)\n\nwhile i < abs(length)                                      % while not finished\n  i = i + (length>0);                                      % count iterations?!\n\n  X0 = X; f0 = f1; df0 = df1;                   % make a copy of current values\n  X = X + z1*s;                                             % begin line search\n  [f2 df2] = eval(argstr);\n  i = i + (length<0);                                          % count epochs?!\n  d2 = df2'*s;\n  f3 = f1; d3 = d1; z3 = -z1;             % initialize point 3 equal to point 1\n  if length>0, M = MAX; else M = min(MAX, -length-i); end\n  success = 0; limit = -1;                     % initialize quanteties\n  while 1\n    while ((f2 > f1+z1*RHO*d1) || (d2 > -SIG*d1)) && (M > 0) \n      limit = z1;                                         % tighten the bracket\n      if f2 > f1\n        z2 = z3 - (0.5*d3*z3*z3)/(d3*z3+f2-f3);                 % quadratic fit\n      else\n        A = 6*(f2-f3)/z3+3*(d2+d3);                                 % cubic fit\n        B = 3*(f3-f2)-z3*(d3+2*d2);\n        z2 = (sqrt(B*B-A*d2*z3*z3)-B)/A;       % numerical error possible - ok!\n      end\n      if isnan(z2) || isinf(z2)\n        z2 = z3/2;                  % if we had a numerical problem then bisect\n      end\n      z2 = max(min(z2, INT*z3),(1-INT)*z3);  % don't accept too close to limits\n      z1 = z1 + z2;                                           % update the step\n      X = X + z2*s;\n      [f2 df2] = eval(argstr);\n      M = M - 1; i = i + (length<0);                           % count epochs?!\n      d2 = df2'*s;\n      z3 = z3-z2;                    % z3 is now relative to the location of z2\n    end\n    if f2 > f1+z1*RHO*d1 || d2 > -SIG*d1\n      break;                                                % this is a failure\n    elseif d2 > SIG*d1\n      success = 1; break;                                             % success\n    elseif M == 0\n      break;                                                          % failure\n    end\n    A = 6*(f2-f3)/z3+3*(d2+d3);                      % make cubic extrapolation\n    B = 3*(f3-f2)-z3*(d3+2*d2);\n    z2 = -d2*z3*z3/(B+sqrt(B*B-A*d2*z3*z3));        % num. error possible - ok!\n    if ~isreal(z2) || isnan(z2) || isinf(z2) || z2 < 0 % num prob or wrong sign?\n      if limit < -0.5                               % if we have no upper limit\n        z2 = z1 * (EXT-1);                 % the extrapolate the maximum amount\n      else\n        z2 = (limit-z1)/2;                                   % otherwise bisect\n      end\n    elseif (limit > -0.5) && (z2+z1 > limit)         % extraplation beyond max?\n      z2 = (limit-z1)/2;                                               % bisect\n    elseif (limit < -0.5) && (z2+z1 > z1*EXT)       % extrapolation beyond limit\n      z2 = z1*(EXT-1.0);                           % set to extrapolation limit\n    elseif z2 < -z3*INT\n      z2 = -z3*INT;\n    elseif (limit > -0.5) && (z2 < (limit-z1)*(1.0-INT))  % too close to limit?\n      z2 = (limit-z1)*(1.0-INT);\n    end\n    f3 = f2; d3 = d2; z3 = -z2;                  % set point 3 equal to point 2\n    z1 = z1 + z2; X = X + z2*s;                      % update current estimates\n    [f2 df2] = eval(argstr);\n    M = M - 1; i = i + (length<0);                             % count epochs?!\n    d2 = df2'*s;\n  end                                                      % end of line search\n\n  if success                                         % if line search succeeded\n    f1 = f2; fX = [fX' f1]';\n    fprintf('%s %4i | Cost: %4.6e\\r', S, i, f1);\n    s = (df2'*df2-df1'*df2)/(df1'*df1)*s - df2;      % Polack-Ribiere direction\n    tmp = df1; df1 = df2; df2 = tmp;                         % swap derivatives\n    d2 = df1'*s;\n    if d2 > 0                                      % new slope must be negative\n      s = -df1;                              % otherwise use steepest direction\n      d2 = -s'*s;    \n    end\n    z1 = z1 * min(RATIO, d1/(d2-realmin));          % slope ratio but max RATIO\n    d1 = d2;\n    ls_failed = 0;                              % this line search did not fail\n  else\n    X = X0; f1 = f0; df1 = df0;  % restore point from before failed line search\n    if ls_failed || i > abs(length)          % line search failed twice in a row\n      break;                             % or we ran out of time, so we give up\n    end\n    tmp = df1; df1 = df2; df2 = tmp;                         % swap derivatives\n    s = -df1;                                                    % try steepest\n    d1 = -s'*s;\n    z1 = 1/(1-d1);                     \n    ls_failed = 1;                                    % this line search failed\n  end\n  if exist('OCTAVE_VERSION')\n    fflush(stdout);\n  end\nend\nfprintf('\\n');\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/AUTHORS.txt",
    "content": "The author of \"jsonlab\" toolbox is Qianqian Fang. Qianqian\nis currently an Assistant Professor at Massachusetts General Hospital, \nHarvard Medical School.\n\nAddress: Martinos Center for Biomedical Imaging, \n         Massachusetts General Hospital, \n         Harvard Medical School\n         Bldg 149, 13th St, Charlestown, MA 02129, USA\nURL: http://nmr.mgh.harvard.edu/~fangq/\nEmail: <fangq at nmr.mgh.harvard.edu> or <fangqq at gmail.com>\n\n\nThe script loadjson.m was built upon previous works by\n\n- Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n       date: 2009/11/02\n- François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n       date: 2009/03/22\n- Joel Feenstra: http://www.mathworks.com/matlabcentral/fileexchange/20565\n       date: 2008/07/03\n\n\nThis toolbox contains patches submitted by the following contributors:\n\n- Blake Johnson <bjohnso at bbn.com>\n  part of revision 341\n\n- Niclas Borlin <Niclas.Borlin at cs.umu.se>\n  various fixes in revision 394, including\n  - loadjson crashes for all-zero sparse matrix.\n  - loadjson crashes for empty sparse matrix.\n  - Non-zero size of 0-by-N and N-by-0 empty matrices is lost after savejson/loadjson.\n  - loadjson crashes for sparse real column vector.\n  - loadjson crashes for sparse complex column vector.\n  - Data is corrupted by savejson for sparse real row vector.\n  - savejson crashes for sparse complex row vector. \n\n- Yul Kang <yul.kang.on at gmail.com>\n  patches for svn revision 415.\n  - savejson saves an empty cell array as [] instead of null\n  - loadjson differentiates an empty struct from an empty array\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/ChangeLog.txt",
    "content": "============================================================================\n\n   JSONlab - a toolbox to encode/decode JSON/UBJSON files in MATLAB/Octave\n\n----------------------------------------------------------------------------\n\nJSONlab ChangeLog (key features marked by *):\n\n== JSONlab 1.0 (codename: Optimus - Final), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2015/01/02 polish help info for all major functions, update examples, finalize 1.0\n 2014/12/19 fix a bug to strictly respect NoRowBracket in savejson\n\n== JSONlab 1.0.0-RC2 (codename: Optimus - RC2), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/11/22 show progress bar in loadjson ('ShowProgress') \n 2014/11/17 add Compact option in savejson to output compact JSON format ('Compact')\n 2014/11/17 add FastArrayParser in loadjson to specify fast parser applicable levels\n 2014/09/18 start official github mirror: https://github.com/fangq/jsonlab\n\n== JSONlab 1.0.0-RC1 (codename: Optimus - RC1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/09/17  fix several compatibility issues when running on octave versions 3.2-3.8\n 2014/09/17  support 2D cell and struct arrays in both savejson and saveubjson\n 2014/08/04  escape special characters in a JSON string\n 2014/02/16  fix a bug when saving ubjson files\n\n== JSONlab 0.9.9 (codename: Optimus - beta), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2014/01/22  use binary read and write in saveubjson and loadubjson\n\n== JSONlab 0.9.8-1 (codename: Optimus - alpha update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2013/10/07 better round-trip conservation for empty arrays and structs (patch submitted by Yul Kang)\n\n== JSONlab 0.9.8 (codename: Optimus - alpha), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2013/08/23 *universal Binary JSON (UBJSON) support, including both saveubjson and loadubjson\n\n== JSONlab 0.9.1 (codename: Rodimus, update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n 2012/12/18 *handling of various empty and sparse matrices (fixes submitted by Niclas Borlin)\n\n== JSONlab 0.9.0 (codename: Rodimus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/06/17 *new format for an invalid leading char, unpacking hex code in savejson\n 2012/06/01  support JSONP in savejson\n 2012/05/25  fix the empty cell bug (reported by Cyril Davin)\n 2012/04/05  savejson can save to a file (suggested by Patrick Rapin)\n\n== JSONlab 0.8.1 (codename: Sentiel, Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/02/28  loadjson quotation mark escape bug, see http://bit.ly/yyk1nS\n 2012/01/25  patch to handle root-less objects, contributed by Blake Johnson\n\n== JSONlab 0.8.0 (codename: Sentiel), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2012/01/13 *speed up loadjson by 20 fold when parsing large data arrays in matlab\n 2012/01/11  remove row bracket if an array has 1 element, suggested by Mykel Kochenderfer\n 2011/12/22 *accept sequence of 'param',value input in savejson and loadjson\n 2011/11/18  fix struct array bug reported by Mykel Kochenderfer\n\n== JSONlab 0.5.1 (codename: Nexus Update 1), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/21  fix a bug in loadjson, previous code does not use any of the acceleration\n 2011/10/20  loadjson supports JSON collections - concatenated JSON objects\n\n== JSONlab 0.5.0 (codename: Nexus), FangQ <fangq (at) nmr.mgh.harvard.edu> ==\n\n 2011/10/16  package and release jsonlab 0.5.0\n 2011/10/15 *add json demo and regression test, support cpx numbers, fix double quote bug\n 2011/10/11 *speed up readjson dramatically, interpret _Array* tags, show data in root level\n 2011/10/10  create jsonlab project, start jsonlab website, add online documentation\n 2011/10/07 *speed up savejson by 25x using sprintf instead of mat2str, add options support\n 2011/10/06 *savejson works for structs, cells and arrays\n 2011/09/09  derive loadjson from JSON parser from MATLAB Central, draft savejson.m\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/LICENSE_BSD.txt",
    "content": "Copyright 2011-2015 Qianqian Fang <fangq at nmr.mgh.harvard.edu>. All rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are\npermitted provided that the following conditions are met:\n\n   1. Redistributions of source code must retain the above copyright notice, this list of\n      conditions and the following disclaimer.\n\n   2. Redistributions in binary form must reproduce the above copyright notice, this list\n      of conditions and the following disclaimer in the documentation and/or other materials\n      provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY EXPRESS OR IMPLIED\nWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\nFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS \nOR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\nCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\nNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nThe views and conclusions contained in the software and documentation are those of the\nauthors and should not be interpreted as representing official policies, either expressed\nor implied, of the copyright holders.\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/README.txt",
    "content": "===============================================================================\n=                                 JSONLab                                     =\n=           An open-source MATLAB/Octave JSON encoder and decoder             =\n===============================================================================\n\n*Copyright (C) 2011-2015  Qianqian Fang <fangq at nmr.mgh.harvard.edu>\n*License: BSD License, see License_BSD.txt for details\n*Version: 1.0 (Optimus - Final)\n\n-------------------------------------------------------------------------------\n\nTable of Content:\n\nI.  Introduction\nII. Installation\nIII.Using JSONLab\nIV. Known Issues and TODOs\nV.  Contribution and feedback\n\n-------------------------------------------------------------------------------\n\nI.  Introduction\n\nJSON ([http://www.json.org/ JavaScript Object Notation]) is a highly portable, \nhuman-readable and \"[http://en.wikipedia.org/wiki/JSON fat-free]\" text format \nto represent complex and hierarchical data. It is as powerful as \n[http://en.wikipedia.org/wiki/XML XML], but less verbose. JSON format is widely \nused for data-exchange in applications, and is essential for the wild success \nof [http://en.wikipedia.org/wiki/Ajax_(programming) Ajax] and \n[http://en.wikipedia.org/wiki/Web_2.0 Web2.0]. \n\nUBJSON (Universal Binary JSON) is a binary JSON format, specifically \noptimized for compact file size and better performance while keeping\nthe semantics as simple as the text-based JSON format. Using the UBJSON\nformat allows to wrap complex binary data in a flexible and extensible\nstructure, making it possible to process complex and large dataset \nwithout accuracy loss due to text conversions.\n\nWe envision that both JSON and its binary version will serve as part of \nthe mainstream data-exchange formats for scientific research in the future. \nIt will provide the flexibility and generality achieved by other popular \ngeneral-purpose file specifications, such as\n[http://www.hdfgroup.org/HDF5/whatishdf5.html HDF5], with significantly \nreduced complexity and enhanced performance.\n\nJSONLab is a free and open-source implementation of a JSON/UBJSON encoder \nand a decoder in the native MATLAB language. It can be used to convert a MATLAB \ndata structure (array, struct, cell, struct array and cell array) into \nJSON/UBJSON formatted strings, or to decode a JSON/UBJSON file into MATLAB \ndata structure. JSONLab supports both MATLAB and  \n[http://www.gnu.org/software/octave/ GNU Octave] (a free MATLAB clone).\n\n-------------------------------------------------------------------------------\n\nII. Installation\n\nThe installation of JSONLab is no different than any other simple\nMATLAB toolbox. You only need to download/unzip the JSONLab package\nto a folder, and add the folder's path to MATLAB/Octave's path list\nby using the following command:\n\n    addpath('/path/to/jsonlab');\n\nIf you want to add this path permanently, you need to type \"pathtool\", \nbrowse to the jsonlab root folder and add to the list, then click \"Save\".\nThen, run \"rehash\" in MATLAB, and type \"which loadjson\", if you see an \noutput, that means JSONLab is installed for MATLAB/Octave.\n\n-------------------------------------------------------------------------------\n\nIII.Using JSONLab\n\nJSONLab provides two functions, loadjson.m -- a MATLAB->JSON decoder, \nand savejson.m -- a MATLAB->JSON encoder, for the text-based JSON, and \ntwo equivallent functions -- loadubjson and saveubjson for the binary \nJSON. The detailed help info for the four functions can be found below:\n\n=== loadjson.m ===\n<pre>\n  data=loadjson(fname,opt)\n     or\n  data=loadjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09, including previous works from \n \n          Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n             created on 2009/11/02\n          Franois Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n             created on  2009/03/22\n          Joel Feenstra:\n          http://www.mathworks.com/matlabcentral/fileexchange/20565\n             created on 2008/07/03\n \n  $Id: loadjson.m 452 2014-11-22 16:43:33Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a JSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n                          speed-optimized array parser when loading an \n                          array object. The fast array parser may \n                          collapse block arrays into a single large\n                          array similar to rules defined in cell2mat; 0 to \n                          use a legacy parser; if set to a larger-than-1\n                          value, this option will specify the minimum\n                          dimension to enable the fast array parser. For\n                          example, if the input is a 3D array, setting\n                          FastArrayParser to 1 will return a 3D array;\n                          setting to 2 will return a cell array of 2D\n                          arrays; setting to 3 will return to a 2D cell\n                          array of 1D vectors; setting to 4 will return a\n                          3D cell array.\n            opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n       dat=loadjson(['examples' filesep 'example1.json'])\n       dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n</pre>\n\n=== savejson.m ===\n\n<pre>\n  json=savejson(rootname,obj,filename)\n     or\n  json=savejson(rootname,obj,opt)\n  json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n  Object Notation) string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2011/09/09\n \n  $Id: savejson.m 458 2014-12-19 22:17:17Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array).\n       filename: a string for the file name to save the output JSON data.\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n                          of a 1D/2D array;\n         opt.ArrayIndent [1|0]: if 1, output explicit data array with\n                          precedent indentation; if 0, no indentation\n         opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n                          to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n                          and $1 represents the sign. For those who want to use\n                          1e999 to represent Inf, they can set opt.Inf to '$11e999'\n         opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n                          to represent NaN\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSONP='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n         opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n         opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a string in the JSON format (see http://json.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       savejson('jmesh',jsonmesh)\n       savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n </pre>\n\n=== loadubjson.m ===\n\n<pre>\n  data=loadubjson(fname,opt)\n     or\n  data=loadubjson(fname,'param1',value1,'param2',value2,...)\n \n  parse a JSON (JavaScript Object Notation) file or string\n \n  authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/01\n \n  $Id: loadubjson.m 436 2014-08-05 20:51:40Z fangq $\n \n  input:\n       fname: input file name, if fname contains \"{}\" or \"[]\", fname\n              will be interpreted as a UBJSON string\n       opt: a struct to store parsing options, opt can be replaced by \n            a list of ('param',value) pairs - the param string is equivallent\n            to a field in opt. opt can have the following \n            fields (first in [.|.] is the default)\n \n            opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n                          for each element of the JSON data, and group \n                          arrays based on the cell2mat rules.\n            opt.IntEndian [B|L]: specify the endianness of the integer fields\n                          in the UBJSON input data. B - Big-Endian format for \n                          integers (as required in the UBJSON specification); \n                          L - input integer fields are in Little-Endian order.\n \n  output:\n       dat: a cell array, where {...} blocks are converted into cell arrays,\n            and [...] are converted to arrays\n \n  examples:\n       obj=struct('string','value','array',[1 2 3]);\n       ubjdata=saveubjson('obj',obj);\n       dat=loadubjson(ubjdata)\n       dat=loadubjson(['examples' filesep 'example1.ubj'])\n       dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n</pre>\n\n=== saveubjson.m ===\n\n<pre>\n  json=saveubjson(rootname,obj,filename)\n     or\n  json=saveubjson(rootname,obj,opt)\n  json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n \n  convert a MATLAB object (cell, struct or array) into a Universal \n  Binary JSON (UBJSON) binary string\n \n  author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n  created on 2013/08/17\n \n  $Id: saveubjson.m 440 2014-09-17 19:59:45Z fangq $\n \n  input:\n       rootname: the name of the root-object, when set to '', the root name\n         is ignored, however, when opt.ForceRootName is set to 1 (see below),\n         the MATLAB variable name will be used as the root name.\n       obj: a MATLAB object (array, cell, cell array, struct, struct array)\n       filename: a string for the file name to save the output UBJSON data\n       opt: a struct for additional options, ignore to use default values.\n         opt can have the following fields (first in [.|.] is the default)\n \n         opt.FileName [''|string]: a file name to save the output JSON data\n         opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n                          array in JSON array format; if sets to 1, an\n                          array will be shown as a struct with fields\n                          \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n                          sparse arrays, the non-zero elements will be\n                          saved to _ArrayData_ field in triplet-format i.e.\n                          (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n                          with a value of 1; for a complex array, the \n                          _ArrayData_ array will include two columns \n                          (4 for sparse) to record the real and imaginary \n                          parts, and also \"_ArrayIsComplex_\":1 is added. \n         opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n                          will use true/false rather than 1/0.\n         opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n                          numerical element will be shown without a square\n                          bracket, unless it is the root object; if 0, square\n                          brackets are forced for any numerical arrays.\n         opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n                          will use the name of the passed obj variable as the \n                          root object name; if obj is an expression and \n                          does not have a name, 'root' will be used; if this \n                          is set to 0 and rootname is empty, the root level \n                          will be merged down to the lower level.\n         opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n                          for example, if opt.JSON='foo', the JSON data is\n                          wrapped inside a function call as 'foo(...);'\n         opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n                          back to the string form\n \n         opt can be replaced by a list of ('param',value) pairs. The param \n         string is equivallent to a field in opt and is case sensitive.\n  output:\n       json: a binary string in the UBJSON format (see http://ubjson.org)\n \n  examples:\n       jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n                'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n                'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n                           2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n                'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n                'SpecialData',[nan, inf, -inf]);\n       saveubjson('jsonmesh',jsonmesh)\n       saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n</pre>\n\n\n=== examples ===\n\nUnder the \"examples\" folder, you can find several scripts to demonstrate the\nbasic utilities of JSONLab. Running the \"demo_jsonlab_basic.m\" script, you \nwill see the conversions from MATLAB data structure to JSON text and backward.\nIn \"jsonlab_selftest.m\", we load complex JSON files downloaded from the Internet\nand validate the loadjson/savejson functions for regression testing purposes.\nSimilarly, a \"demo_ubjson_basic.m\" script is provided to test the saveubjson\nand loadubjson pairs for various matlab data structures.\n\nPlease run these examples and understand how JSONLab works before you use\nit to process your data.\n\n-------------------------------------------------------------------------------\n\nIV. Known Issues and TODOs\n\nJSONLab has several known limitations. We are striving to make it more general\nand robust. Hopefully in a few future releases, the limitations become less.\n\nHere are the known issues:\n\n# 3D or higher dimensional cell/struct-arrays will be converted to 2D arrays;\n# When processing names containing multi-byte characters, Octave and MATLAB \\\ncan give different field-names; you can use feature('DefaultCharacterSet','latin1') \\\nin MATLAB to get consistant results\n# savejson can not handle class and dataset.\n# saveubjson converts a logical array into a uint8 ([U]) array\n# an unofficial N-D array count syntax is implemented in saveubjson. We are \\\nactively communicating with the UBJSON spec maintainer to investigate the \\\npossibility of making it upstream\n# loadubjson can not parse all UBJSON Specification (Draft 9) compliant \\\nfiles, however, it can parse all UBJSON files produced by saveubjson.\n\n-------------------------------------------------------------------------------\n\nV. Contribution and feedback\n\nJSONLab is an open-source project. This means you can not only use it and modify\nit as you wish, but also you can contribute your changes back to JSONLab so\nthat everyone else can enjoy the improvement. For anyone who want to contribute,\nplease download JSONLab source code from it's subversion repository by using the\nfollowing command:\n\n svn checkout svn://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab jsonlab\n\nYou can make changes to the files as needed. Once you are satisfied with your\nchanges, and ready to share it with others, please cd the root directory of \nJSONLab, and type\n\n svn diff > yourname_featurename.patch\n\nYou then email the .patch file to JSONLab's maintainer, Qianqian Fang, at\nthe email address shown in the beginning of this file. Qianqian will review \nthe changes and commit it to the subversion if they are satisfactory.\n\nWe appreciate any suggestions and feedbacks from you. Please use iso2mesh's\nmailing list to report any questions you may have with JSONLab:\n\nhttp://groups.google.com/group/iso2mesh-users?hl=en&pli=1\n\n(Subscription to the mailing list is needed in order to post messages).\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/jsonopt.m",
    "content": "function val=jsonopt(key,default,varargin)\n%\n% val=jsonopt(key,default,optstruct)\n%\n% setting options based on a struct. The struct can be produced\n% by varargin2struct from a list of 'param','value' pairs\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n%\n% $Id: loadjson.m 371 2012-06-20 12:43:06Z fangq $\n%\n% input:\n%      key: a string with which one look up a value from a struct\n%      default: if the key does not exist, return default\n%      optstruct: a struct where each sub-field is a key \n%\n% output:\n%      val: if key exists, val=optstruct.key; otherwise val=default\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n% \n\nval=default;\nif(nargin<=2) return; end\nopt=varargin{1};\nif(isstruct(opt) && isfield(opt,key))\n    val=getfield(opt,key);\nend\n\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/loadjson.m",
    "content": "function data = loadjson(fname,varargin)\n%\n% data=loadjson(fname,opt)\n%    or\n% data=loadjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09, including previous works from \n%\n%         Nedialko Krouchev: http://www.mathworks.com/matlabcentral/fileexchange/25713\n%            created on 2009/11/02\n%         François Glineur: http://www.mathworks.com/matlabcentral/fileexchange/23393\n%            created on  2009/03/22\n%         Joel Feenstra:\n%         http://www.mathworks.com/matlabcentral/fileexchange/20565\n%            created on 2008/07/03\n%\n% $Id: loadjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a JSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.FastArrayParser [1|0 or integer]: if set to 1, use a\n%                         speed-optimized array parser when loading an \n%                         array object. The fast array parser may \n%                         collapse block arrays into a single large\n%                         array similar to rules defined in cell2mat; 0 to \n%                         use a legacy parser; if set to a larger-than-1\n%                         value, this option will specify the minimum\n%                         dimension to enable the fast array parser. For\n%                         example, if the input is a 3D array, setting\n%                         FastArrayParser to 1 will return a 3D array;\n%                         setting to 2 will return a cell array of 2D\n%                         arrays; setting to 3 will return to a 2D cell\n%                         array of 1D vectors; setting to 4 will return a\n%                         3D cell array.\n%           opt.ShowProgress [0|1]: if set to 1, loadjson displays a progress bar.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      dat=loadjson('{\"obj\":{\"string\":\"value\",\"array\":[1,2,3]}}')\n%      dat=loadjson(['examples' filesep 'example1.json'])\n%      dat=loadjson(['examples' filesep 'example1.json'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\n\nif(jsonopt('ShowProgress',0,opt)==1)\n    opt.progressbar_=waitbar(0,'loading ...');\nend\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\nif(isfield(opt,'progressbar_'))\n    close(opt.progressbar_);\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=data(j).x0x5F_ArraySize_;\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    if next_char ~= '}'\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            parse_char(':');\n            val = parse_value(varargin{:});\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}'\n                break;\n            end\n            parse_char(',');\n        end\n    end\n    parse_char('}');\n\n%%-------------------------------------------------------------------------\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim2=[];\n    arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:});\n    pbar=jsonopt('progressbar_',-1,varargin{:});\n\n    if next_char ~= ']'\n\tif(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))\n            [endpos, e1l, e1r, maxlevel]=matching_bracket(inStr,pos);\n            arraystr=['[' inStr(pos:endpos)];\n            arraystr=regexprep(arraystr,'\"_NaN_\"','NaN');\n            arraystr=regexprep(arraystr,'\"([-+]*)_Inf_\"','$1Inf');\n            arraystr(arraystr==sprintf('\\n'))=[];\n            arraystr(arraystr==sprintf('\\r'))=[];\n            %arraystr=regexprep(arraystr,'\\s*,',','); % this is slow,sometimes needed\n            if(~isempty(e1l) && ~isempty(e1r)) % the array is in 2D or higher D\n        \tastr=inStr((e1l+1):(e1r-1));\n        \tastr=regexprep(astr,'\"_NaN_\"','NaN');\n        \tastr=regexprep(astr,'\"([-+]*)_Inf_\"','$1Inf');\n        \tastr(astr==sprintf('\\n'))=[];\n        \tastr(astr==sprintf('\\r'))=[];\n        \tastr(astr==' ')='';\n        \tif(isempty(find(astr=='[', 1))) % array is 2D\n                    dim2=length(sscanf(astr,'%f,',[1 inf]));\n        \tend\n            else % array is 1D\n        \tastr=arraystr(2:end-1);\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',[1,inf]);\n        \tif(nextidx>=length(astr)-1)\n                    object=obj;\n                    pos=endpos;\n                    parse_char(']');\n                    return;\n        \tend\n            end\n            if(~isempty(dim2))\n        \tastr=arraystr;\n        \tastr(astr=='[')='';\n        \tastr(astr==']')='';\n        \tastr(astr==' ')='';\n        \t[obj, count, errmsg, nextidx]=sscanf(astr,'%f,',inf);\n        \tif(nextidx>=length(astr)-1)\n                    object=reshape(obj,dim2,numel(obj)/dim2)';\n                    pos=endpos;\n                    parse_char(']');\n                    if(pbar>0)\n                        waitbar(pos/length(inStr),pbar,'loading ...');\n                    end\n                    return;\n        \tend\n            end\n            arraystr=regexprep(arraystr,'\\]\\s*,','];');\n\telse\n            arraystr='[';\n\tend\n        try\n           if(isoct && regexp(arraystr,'\"','once'))\n                error('Octave eval can produce empty cells for JSON-like input');\n           end\n           object=eval(arraystr);\n           pos=endpos;\n        catch\n         while 1\n            newopt=varargin2struct(varargin{:},'JSONLAB_ArrayDepth_',arraydepth+1);\n            val = parse_value(newopt);\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            parse_char(',');\n         end\n        end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    parse_char(']');\n    \n    if(pbar>0)\n        waitbar(pos/length(inStr),pbar,'loading ...');\n    end\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr len  esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    if inStr(pos) ~= '\"'\n        error_pos('String starting with \" expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    str = '';\n    while pos <= len\n        while index_esc <= len_esc && esc(index_esc) < pos\n            index_esc = index_esc + 1;\n        end\n        if index_esc > len_esc\n            str = [str inStr(pos:len)];\n            pos = len + 1;\n            break;\n        else\n            str = [str inStr(pos:esc(index_esc)-1)];\n            pos = esc(index_esc);\n        end\n        nstr = length(str); switch inStr(pos)\n            case '\"'\n                pos = pos + 1;\n                if(~isempty(str))\n                    if(strcmp(str,'_Inf_'))\n                        str=Inf;\n                    elseif(strcmp(str,'-_Inf_'))\n                        str=-Inf;\n                    elseif(strcmp(str,'_NaN_'))\n                        str=NaN;\n                    end\n                end\n                return;\n            case '\\'\n                if pos+1 > len\n                    error_pos('End of file reached right after escape character');\n                end\n                pos = pos + 1;\n                switch inStr(pos)\n                    case {'\"' '\\' '/'}\n                        str(nstr+1) = inStr(pos);\n                        pos = pos + 1;\n                    case {'b' 'f' 'n' 'r' 't'}\n                        str(nstr+1) = sprintf(['\\' inStr(pos)]);\n                        pos = pos + 1;\n                    case 'u'\n                        if pos+4 > len\n                            error_pos('End of file reached in escaped unicode character');\n                        end\n                        str(nstr+(1:6)) = inStr(pos-1:pos+4);\n                        pos = pos + 5;\n                end\n            otherwise % should never happen\n                str(nstr+1) = inStr(pos), keyboard\n                pos = pos + 1;\n        end\n    end\n    error_pos('End of file while expecting end of inStr');\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct\n    currstr=inStr(pos:end);\n    numstr=0;\n    if(isoct~=0)\n        numstr=regexp(currstr,'^\\s*-?(?:0|[1-9]\\d*)(?:\\.\\d+)?(?:[eE][+\\-]?\\d+)?','end');\n        [num, one] = sscanf(currstr, '%f', 1);\n        delta=numstr+1;\n    else\n        [num, one, err, delta] = sscanf(currstr, '%f', 1);\n        if ~isempty(err)\n            error_pos('Error reading number at position %d');\n        end\n    end\n    pos = pos + delta-1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n    \n    pbar=jsonopt('progressbar_',-1,varargin{:});\n    if(pbar>0)\n        waitbar(pos/len,pbar,'loading ...');\n    end\n    \n    switch(inStr(pos))\n        case '\"'\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'-','0','1','2','3','4','5','6','7','8','9'}\n            val = parse_number(varargin{:});\n            return;\n        case 't'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'true')\n                val = true;\n                pos = pos + 4;\n                return;\n            end\n        case 'f'\n            if pos+4 <= len && strcmpi(inStr(pos:pos+4), 'false')\n                val = false;\n                pos = pos + 5;\n                return;\n            end\n        case 'n'\n            if pos+3 <= len && strcmpi(inStr(pos:pos+3), 'null')\n                val = [];\n                pos = pos + 4;\n                return;\n            end\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos, e1l, e1r, maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/loadubjson.m",
    "content": "function data = loadubjson(fname,varargin)\n%\n% data=loadubjson(fname,opt)\n%    or\n% data=loadubjson(fname,'param1',value1,'param2',value2,...)\n%\n% parse a JSON (JavaScript Object Notation) file or string\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/01\n%\n% $Id: loadubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      fname: input file name, if fname contains \"{}\" or \"[]\", fname\n%             will be interpreted as a UBJSON string\n%      opt: a struct to store parsing options, opt can be replaced by \n%           a list of ('param',value) pairs - the param string is equivallent\n%           to a field in opt. opt can have the following \n%           fields (first in [.|.] is the default)\n%\n%           opt.SimplifyCell [0|1]: if set to 1, loadubjson will call cell2mat\n%                         for each element of the JSON data, and group \n%                         arrays based on the cell2mat rules.\n%           opt.IntEndian [B|L]: specify the endianness of the integer fields\n%                         in the UBJSON input data. B - Big-Endian format for \n%                         integers (as required in the UBJSON specification); \n%                         L - input integer fields are in Little-Endian order.\n%\n% output:\n%      dat: a cell array, where {...} blocks are converted into cell arrays,\n%           and [...] are converted to arrays\n%\n% examples:\n%      obj=struct('string','value','array',[1 2 3]);\n%      ubjdata=saveubjson('obj',obj);\n%      dat=loadubjson(ubjdata)\n%      dat=loadubjson(['examples' filesep 'example1.ubj'])\n%      dat=loadubjson(['examples' filesep 'example1.ubj'],'SimplifyCell',1)\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nglobal pos inStr len  esc index_esc len_esc isoct arraytoken fileendian systemendian\n\nif(regexp(fname,'[\\{\\}\\]\\[]','once'))\n   string=fname;\nelseif(exist(fname,'file'))\n   fid = fopen(fname,'rb');\n   string = fread(fid,inf,'uint8=>char')';\n   fclose(fid);\nelse\n   error('input file does not exist');\nend\n\npos = 1; len = length(string); inStr = string;\nisoct=exist('OCTAVE_VERSION','builtin');\narraytoken=find(inStr=='[' | inStr==']' | inStr=='\"');\njstr=regexprep(inStr,'\\\\\\\\','  ');\nescquote=regexp(jstr,'\\\\\"');\narraytoken=sort([arraytoken escquote]);\n\n% String delimiters and escape chars identified to improve speed:\nesc = find(inStr=='\"' | inStr=='\\' ); % comparable to: regexp(inStr, '[\"\\\\]');\nindex_esc = 1; len_esc = length(esc);\n\nopt=varargin2struct(varargin{:});\nfileendian=upper(jsonopt('IntEndian','B',opt));\n[os,maxelem,systemendian]=computer;\n\njsoncount=1;\nwhile pos <= len\n    switch(next_char)\n        case '{'\n            data{jsoncount} = parse_object(opt);\n        case '['\n            data{jsoncount} = parse_array(opt);\n        otherwise\n            error_pos('Outer level structure must be an object or an array');\n    end\n    jsoncount=jsoncount+1;\nend % while\n\njsoncount=length(data);\nif(jsoncount==1 && iscell(data))\n    data=data{1};\nend\n\nif(~isempty(data))\n      if(isstruct(data)) % data can be a struct array\n          data=jstruct2array(data);\n      elseif(iscell(data))\n          data=jcell2array(data);\n      end\nend\n\n\n%%\nfunction newdata=parse_collection(id,data,obj)\n\nif(jsoncount>0 && exist('data','var')) \n    if(~iscell(data))\n       newdata=cell(1);\n       newdata{1}=data;\n       data=newdata;\n    end\nend\n\n%%\nfunction newdata=jcell2array(data)\nlen=length(data);\nnewdata=data;\nfor i=1:len\n      if(isstruct(data{i}))\n          newdata{i}=jstruct2array(data{i});\n      elseif(iscell(data{i}))\n          newdata{i}=jcell2array(data{i});\n      end\nend\n\n%%-------------------------------------------------------------------------\nfunction newdata=jstruct2array(data)\nfn=fieldnames(data);\nnewdata=data;\nlen=length(data);\nfor i=1:length(fn) % depth-first\n    for j=1:len\n        if(isstruct(getfield(data(j),fn{i})))\n            newdata(j)=setfield(newdata(j),fn{i},jstruct2array(getfield(data(j),fn{i})));\n        end\n    end\nend\nif(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))\n  newdata=cell(len,1);\n  for j=1:len\n    ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);\n    iscpx=0;\n    if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))\n        if(data(j).x0x5F_ArrayIsComplex_)\n           iscpx=1;\n        end\n    end\n    if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))\n        if(data(j).x0x5F_ArrayIsSparse_)\n            if(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n                dim=double(data(j).x0x5F_ArraySize_);\n                if(iscpx && size(ndata,2)==4-any(dim==1))\n                    ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));\n                end\n                if isempty(ndata)\n                    % All-zeros sparse\n                    ndata=sparse(dim(1),prod(dim(2:end)));\n                elseif dim(1)==1\n                    % Sparse row vector\n                    ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));\n                elseif dim(2)==1\n                    % Sparse column vector\n                    ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));\n                else\n                    % Generic sparse array.\n                    ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));\n                end\n            else\n                if(iscpx && size(ndata,2)==4)\n                    ndata(:,3)=complex(ndata(:,3),ndata(:,4));\n                end\n                ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));\n            end\n        end\n    elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))\n        if(iscpx && size(ndata,2)==2)\n             ndata=complex(ndata(:,1),ndata(:,2));\n        end\n        ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);\n    end\n    newdata{j}=ndata;\n  end\n  if(len==1)\n      newdata=newdata{1};\n  end\nend\n\n%%-------------------------------------------------------------------------\nfunction object = parse_object(varargin)\n    parse_char('{');\n    object = [];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1); % TODO\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        count=double(parse_number());\n    end\n    if next_char ~= '}'\n        num=0;\n        while 1\n            str = parseStr(varargin{:});\n            if isempty(str)\n                error_pos('Name of value at position %d cannot be empty');\n            end\n            %parse_char(':');\n            val = parse_value(varargin{:});\n            num=num+1;\n            eval( sprintf( 'object.%s  = val;', valid_field(str) ) );\n            if next_char == '}' || (count>=0 && num>=count)\n                break;\n            end\n            %parse_char(',');\n        end\n    end\n    if(count==-1)\n        parse_char('}');\n    end\n\n%%-------------------------------------------------------------------------\nfunction [cid,len]=elem_info(type)\nid=strfind('iUIlLdD',type);\ndataclass={'int8','uint8','int16','int32','int64','single','double'};\nbytelen=[1,1,2,4,8,4,8];\nif(id>0)\n    cid=dataclass{id};\n    len=bytelen(id);\nelse\n    error_pos('unsupported type at position %d');\nend\n%%-------------------------------------------------------------------------\n\n\nfunction [data adv]=parse_block(type,count,varargin)\nglobal pos inStr isoct fileendian systemendian\n[cid,len]=elem_info(type);\ndatastr=inStr(pos:pos+len*count-1);\nif(isoct)\n    newdata=int8(datastr);\nelse\n    newdata=uint8(datastr);\nend\nid=strfind('iUIlLdD',type);\nif(id<=5 && fileendian~=systemendian)\n    newdata=swapbytes(typecast(newdata,cid));\nend\ndata=typecast(newdata,cid);\nadv=double(len*count);\n\n%%-------------------------------------------------------------------------\n\n\nfunction object = parse_array(varargin) % JSON array is written in row-major order\nglobal pos inStr isoct\n    parse_char('[');\n    object = cell(0, 1);\n    dim=[];\n    type='';\n    count=-1;\n    if(next_char == '$')\n        type=inStr(pos+1);\n        pos=pos+2;\n    end\n    if(next_char == '#')\n        pos=pos+1;\n        if(next_char=='[')\n            dim=parse_array(varargin{:});\n            count=prod(double(dim));\n        else\n            count=double(parse_number());\n        end\n    end\n    if(~isempty(type))\n        if(count>=0)\n            [object adv]=parse_block(type,count,varargin{:});\n            if(~isempty(dim))\n                object=reshape(object,dim);\n            end\n            pos=pos+adv;\n            return;\n        else\n            endpos=matching_bracket(inStr,pos);\n            [cid,len]=elem_info(type);\n            count=(endpos-pos)/len;\n            [object adv]=parse_block(type,count,varargin{:});\n            pos=pos+adv;\n            parse_char(']');\n            return;\n        end\n    end\n    if next_char ~= ']'\n         while 1\n            val = parse_value(varargin{:});\n            object{end+1} = val;\n            if next_char == ']'\n                break;\n            end\n            %parse_char(',');\n         end\n    end\n    if(jsonopt('SimplifyCell',0,varargin{:})==1)\n      try\n        oldobj=object;\n        object=cell2mat(object')';\n        if(iscell(oldobj) && isstruct(object) && numel(object)>1 && jsonopt('SimplifyCellArray',1,varargin{:})==0)\n            object=oldobj;\n        elseif(size(object,1)>1 && ndims(object)==2)\n            object=object';\n        end\n      catch\n      end\n    end\n    if(count==-1)\n        parse_char(']');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction parse_char(c)\n    global pos inStr len\n    skip_whitespace;\n    if pos > len || inStr(pos) ~= c\n        error_pos(sprintf('Expected %c at position %%d', c));\n    else\n        pos = pos + 1;\n        skip_whitespace;\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction c = next_char\n    global pos inStr len\n    skip_whitespace;\n    if pos > len\n        c = [];\n    else\n        c = inStr(pos);\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction skip_whitespace\n    global pos inStr len\n    while pos <= len && isspace(inStr(pos))\n        pos = pos + 1;\n    end\n\n%%-------------------------------------------------------------------------\nfunction str = parseStr(varargin)\n    global pos inStr esc index_esc len_esc\n % len, ns = length(inStr), keyboard\n    type=inStr(pos);\n    if type ~= 'S' && type ~= 'C' && type ~= 'H'\n        error_pos('String starting with S expected at position %d');\n    else\n        pos = pos + 1;\n    end\n    if(type == 'C')\n        str=inStr(pos);\n        pos=pos+1;\n        return;\n    end\n    bytelen=double(parse_number());\n    if(length(inStr)>=pos+bytelen-1)\n        str=inStr(pos:pos+bytelen-1);\n        pos=pos+bytelen;\n    else\n        error_pos('End of file while expecting end of inStr');\n    end\n\n%%-------------------------------------------------------------------------\n\nfunction num = parse_number(varargin)\n    global pos inStr len isoct fileendian systemendian\n    id=strfind('iUIlLdD',inStr(pos));\n    if(isempty(id))\n        error_pos('expecting a number at position %d');\n    end\n    type={'int8','uint8','int16','int32','int64','single','double'};\n    bytelen=[1,1,2,4,8,4,8];\n    datastr=inStr(pos+1:pos+bytelen(id));\n    if(isoct)\n        newdata=int8(datastr);\n    else\n        newdata=uint8(datastr);\n    end\n    if(id<=5 && fileendian~=systemendian)\n        newdata=swapbytes(typecast(newdata,type{id}));\n    end\n    num=typecast(newdata,type{id});\n    pos = pos + bytelen(id)+1;\n\n%%-------------------------------------------------------------------------\n\nfunction val = parse_value(varargin)\n    global pos inStr len\n    true = 1; false = 0;\n\n    switch(inStr(pos))\n        case {'S','C','H'}\n            val = parseStr(varargin{:});\n            return;\n        case '['\n            val = parse_array(varargin{:});\n            return;\n        case '{'\n            val = parse_object(varargin{:});\n            if isstruct(val)\n                if(~isempty(strmatch('x0x5F_ArrayType_',fieldnames(val), 'exact')))\n                    val=jstruct2array(val);\n                end\n            elseif isempty(val)\n                val = struct;\n            end\n            return;\n        case {'i','U','I','l','L','d','D'}\n            val = parse_number(varargin{:});\n            return;\n        case 'T'\n            val = true;\n            pos = pos + 1;\n            return;\n        case 'F'\n            val = false;\n            pos = pos + 1;\n            return;\n        case {'Z','N'}\n            val = [];\n            pos = pos + 1;\n            return;\n    end\n    error_pos('Value expected at position %d');\n%%-------------------------------------------------------------------------\n\nfunction error_pos(msg)\n    global pos inStr len\n    poShow = max(min([pos-15 pos-1 pos pos+20],len),1);\n    if poShow(3) == poShow(2)\n        poShow(3:4) = poShow(2)+[0 -1];  % display nothing after\n    end\n    msg = [sprintf(msg, pos) ': ' ...\n    inStr(poShow(1):poShow(2)) '<error>' inStr(poShow(3):poShow(4)) ];\n    error( ['JSONparser:invalidFormat: ' msg] );\n\n%%-------------------------------------------------------------------------\n\nfunction str = valid_field(str)\nglobal isoct\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n    %str(~isletter(str) & ~('0' <= str & str <= '9')) = '_';\n\n%%-------------------------------------------------------------------------\nfunction endpos = matching_quote(str,pos)\nlen=length(str);\nwhile(pos<len)\n    if(str(pos)=='\"')\n        if(~(pos>1 && str(pos-1)=='\\'))\n            endpos=pos;\n            return;\n        end        \n    end\n    pos=pos+1;\nend\nerror('unmatched quotation mark');\n%%-------------------------------------------------------------------------\nfunction [endpos e1l e1r maxlevel] = matching_bracket(str,pos)\nglobal arraytoken\nlevel=1;\nmaxlevel=level;\nendpos=0;\nbpos=arraytoken(arraytoken>=pos);\ntokens=str(bpos);\nlen=length(tokens);\npos=1;\ne1l=[];\ne1r=[];\nwhile(pos<=len)\n    c=tokens(pos);\n    if(c==']')\n        level=level-1;\n        if(isempty(e1r)) e1r=bpos(pos); end\n        if(level==0)\n            endpos=bpos(pos);\n            return\n        end\n    end\n    if(c=='[')\n        if(isempty(e1l)) e1l=bpos(pos); end\n        level=level+1;\n        maxlevel=max(maxlevel,level);\n    end\n    if(c=='\"')\n        pos=matching_quote(tokens,pos+1);\n    end\n    pos=pos+1;\nend\nif(endpos==0) \n    error('unmatched \"]\"');\nend\n\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/mergestruct.m",
    "content": "function s=mergestruct(s1,s2)\n%\n% s=mergestruct(s1,s2)\n%\n% merge two struct objects into one\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      s1,s2: a struct object, s1 and s2 can not be arrays\n%\n% output:\n%      s: the merged struct object. fields in s1 and s2 will be combined in s.\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(~isstruct(s1) || ~isstruct(s2))\n    error('input parameters contain non-struct');\nend\nif(length(s1)>1 || length(s2)>1)\n    error('can not merge struct arrays');\nend\nfn=fieldnames(s2);\ns=s1;\nfor i=1:length(fn)              \n    s=setfield(s,fn{i},getfield(s2,fn{i}));\nend\n\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/savejson.m",
    "content": "function json=savejson(rootname,obj,varargin)\n%\n% json=savejson(rootname,obj,filename)\n%    or\n% json=savejson(rootname,obj,opt)\n% json=savejson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a JSON (JavaScript\n% Object Notation) string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2011/09/09\n%\n% $Id: savejson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array).\n%      filename: a string for the file name to save the output JSON data.\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.FloatFormat ['%.10g'|string]: format to show each numeric element\n%                         of a 1D/2D array;\n%        opt.ArrayIndent [1|0]: if 1, output explicit data array with\n%                         precedent indentation; if 0, no indentation\n%        opt.ArrayToStruct[0|1]: when set to 0, savejson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [0|1]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, savejson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.Inf ['\"$1_Inf_\"'|string]: a customized regular expression pattern\n%                         to represent +/-Inf. The matched pattern is '([-+]*)Inf'\n%                         and $1 represents the sign. For those who want to use\n%                         1e999 to represent Inf, they can set opt.Inf to '$11e999'\n%        opt.NaN ['\"_NaN_\"'|string]: a customized regular expression pattern\n%                         to represent NaN\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSONP='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%        opt.SaveBinary [0|1]: 1 - save the JSON file in binary mode; 0 - text mode.\n%        opt.Compact [0|1]: 1- out compact JSON format (remove all newlines and tabs)\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a string in the JSON format (see http://json.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      savejson('jmesh',jsonmesh)\n%      savejson('',jsonmesh,'ArrayIndent',0,'FloatFormat','\\t%.5g')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\n\nwhitespaces=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nif(jsonopt('Compact',0,opt)==1)\n    whitespaces=struct('tab','','newline','','sep',',');\nend\nif(~isfield(opt,'whitespaces_'))\n    opt.whitespaces_=whitespaces;\nend\n\nnl=whitespaces.newline;\n\njson=obj2json(rootname,obj,rootlevel,opt);\nif(rootisarray)\n    json=sprintf('%s%s',json,nl);\nelse\n    json=sprintf('{%s%s%s}\\n',nl,json,nl);\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=sprintf('%s(%s);%s',jsonp,json,nl);\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    if(jsonopt('SaveBinary',0,opt)==1)\n\t    fid = fopen(opt.FileName, 'wb');\n\t    fwrite(fid,json);\n    else\n\t    fid = fopen(opt.FileName, 'wt');\n\t    fwrite(fid,json,'char');\n    end\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2json(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2json(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2json(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2json(name,item,level,varargin{:});\nelse\n    txt=mat2json(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2json(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=jsonopt('whitespaces_',struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n')),varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nif(len>1)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": [%s',padding0, checkname(name,varargin{:}),nl); name=''; \n    else\n        txt=sprintf('%s[%s',padding0,nl); \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=sprintf('%s\"%s\": []',padding0, checkname(name,varargin{:})); name=''; \n    else\n        txt=sprintf('%s[]',padding0); \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n    for i=1:dim(1)\n       txt=sprintf('%s%s',txt,obj2json(name,item{i,j},level+(dim(1)>1)+1,varargin{:}));\n       if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    end\n    if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n    if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n    %if(j==dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2json(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding0=repmat(ws.tab,1,level);\npadding2=repmat(ws.tab,1,level+1);\npadding1=repmat(ws.tab,1,level+(dim(1)>1)+(len>1));\nnl=ws.newline;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding0,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding0,nl); end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=sprintf('%s%s[%s',txt,padding2,nl); end\n  for i=1:dim(1)\n    names = fieldnames(item(i,j));\n    if(~isempty(name) && len==1)\n        txt=sprintf('%s%s\"%s\": {%s',txt,padding1, checkname(name,varargin{:}),nl); \n    else\n        txt=sprintf('%s%s{%s',txt,padding1,nl); \n    end\n    if(~isempty(names))\n      for e=1:length(names)\n\t    txt=sprintf('%s%s',txt,obj2json(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:}));\n        if(e<length(names)) txt=sprintf('%s%s',txt,','); end\n        txt=sprintf('%s%s',txt,nl);\n      end\n    end\n    txt=sprintf('%s%s}',txt,padding1);\n    if(i<dim(1)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\n  end\n  if(dim(1)>1) txt=sprintf('%s%s%s]',txt,nl,padding2); end\n  if(j<dim(2)) txt=sprintf('%s%s',txt,sprintf(',%s',nl)); end\nend\nif(len>1) txt=sprintf('%s%s%s]',txt,nl,padding0); end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2json(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(~isempty(name)) \n    if(len>1) txt=sprintf('%s\"%s\": [%s',padding1,checkname(name,varargin{:}),nl); end\nelse\n    if(len>1) txt=sprintf('%s[%s',padding1,nl); end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    if(isoct)\n        val=regexprep(item(e,:),'\\\\','\\\\');\n        val=regexprep(val,'\"','\\\"');\n        val=regexprep(val,'^\"','\\\"');\n    else\n        val=regexprep(item(e,:),'\\\\','\\\\\\\\');\n        val=regexprep(val,'\"','\\\\\"');\n        val=regexprep(val,'^\"','\\\\\"');\n    end\n    val=escapejsonstring(val);\n    if(len==1)\n        obj=['\"' checkname(name,varargin{:}) '\": ' '\"',val,'\"'];\n\tif(isempty(name)) obj=['\"',val,'\"']; end\n        txt=sprintf('%s%s%s%s',txt,padding1,obj);\n    else\n        txt=sprintf('%s%s%s%s',txt,padding0,['\"',val,'\"']);\n    end\n    if(e==len) sep=''; end\n    txt=sprintf('%s%s',txt,sep);\nend\nif(len>1) txt=sprintf('%s%s%s%s',txt,nl,padding1,']'); end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2json(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\npadding1=repmat(ws.tab,1,level);\npadding0=repmat(ws.tab,1,level+1);\nnl=ws.newline;\nsep=ws.sep;\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) ||jsonopt('ArrayToStruct',0,varargin{:}))\n    if(isempty(name))\n    \ttxt=sprintf('%s{%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    else\n    \ttxt=sprintf('%s\"%s\": {%s%s\"_ArrayType_\": \"%s\",%s%s\"_ArraySize_\": %s,%s',...\n              padding1,checkname(name,varargin{:}),nl,padding0,class(item),nl,padding0,regexprep(mat2str(size(item)),'\\s+',','),nl);\n    end\nelse\n    if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1 && level>0)\n        numtxt=regexprep(regexprep(matdata2json(item,level+1,varargin{:}),'^\\[',''),']','');\n    else\n        numtxt=matdata2json(item,level+1,varargin{:});\n    end\n    if(isempty(name))\n    \ttxt=sprintf('%s%s',padding1,numtxt);\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n           \ttxt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        else\n    \t    txt=sprintf('%s\"%s\": %s',padding1,checkname(name,varargin{:}),numtxt);\n        end\n    end\n    return;\nend\ndataformat='%s%s%s%s%s';\n\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n    end\n    txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsSparse_\": ','1', sep);\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([iy(:),data'],level+2,varargin{:}), nl);\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,data],level+2,varargin{:}), nl);\n    else\n        % General case, store row and column indices.\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n           matdata2json([ix,iy,data],level+2,varargin{:}), nl);\n    end\nelse\n    if(isreal(item))\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json(item(:)',level+2,varargin{:}), nl);\n    else\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayIsComplex_\": ','1', sep);\n        txt=sprintf(dataformat,txt,padding0,'\"_ArrayData_\": ',...\n            matdata2json([real(item(:)) imag(item(:))],level+2,varargin{:}), nl);\n    end\nend\ntxt=sprintf('%s%s%s',txt,padding1,'}');\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2json(mat,level,varargin)\n\nws=struct('tab',sprintf('\\t'),'newline',sprintf('\\n'),'sep',sprintf(',\\n'));\nws=jsonopt('whitespaces_',ws,varargin{:});\ntab=ws.tab;\nnl=ws.newline;\n\nif(size(mat,1)==1)\n    pre='';\n    post='';\n    level=level-1;\nelse\n    pre=sprintf('[%s',nl);\n    post=sprintf('%s%s]',nl,repmat(tab,1,level-1));\nend\n\nif(isempty(mat))\n    txt='null';\n    return;\nend\nfloatformat=jsonopt('FloatFormat','%.10g',varargin{:});\n%if(numel(mat)>1)\n    formatstr=['[' repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf('],%s',nl)]];\n%else\n%    formatstr=[repmat([floatformat ','],1,size(mat,2)-1) [floatformat sprintf(',\\n')]];\n%end\n\nif(nargin>=2 && size(mat,1)>1 && jsonopt('ArrayIndent',1,varargin{:})==1)\n    formatstr=[repmat(tab,1,level) formatstr];\nend\n\ntxt=sprintf(formatstr,mat');\ntxt(end-length(nl):end)=[];\nif(islogical(mat) && jsonopt('ParseLogical',0,varargin{:})==1)\n   txt=regexprep(txt,'1','true');\n   txt=regexprep(txt,'0','false');\nend\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],\\n['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\ntxt=[pre txt post];\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n\n%%-------------------------------------------------------------------------\nfunction newstr=escapejsonstring(str)\nnewstr=str;\nisoct=exist('OCTAVE_VERSION','builtin');\nif(isoct)\n   vv=sscanf(OCTAVE_VERSION,'%f');\n   if(vv(1)>=3.8) isoct=0; end\nend\nif(isoct)\n  escapechars={'\\a','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},escapechars{i});\n  end\nelse\n  escapechars={'\\a','\\b','\\f','\\n','\\r','\\t','\\v'};\n  for i=1:length(escapechars);\n    newstr=regexprep(newstr,escapechars{i},regexprep(escapechars{i},'\\\\','\\\\\\\\'));\n  end\nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/saveubjson.m",
    "content": "function json=saveubjson(rootname,obj,varargin)\n%\n% json=saveubjson(rootname,obj,filename)\n%    or\n% json=saveubjson(rootname,obj,opt)\n% json=saveubjson(rootname,obj,'param1',value1,'param2',value2,...)\n%\n% convert a MATLAB object (cell, struct or array) into a Universal \n% Binary JSON (UBJSON) binary string\n%\n% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% created on 2013/08/17\n%\n% $Id: saveubjson.m 460 2015-01-03 00:30:45Z fangq $\n%\n% input:\n%      rootname: the name of the root-object, when set to '', the root name\n%        is ignored, however, when opt.ForceRootName is set to 1 (see below),\n%        the MATLAB variable name will be used as the root name.\n%      obj: a MATLAB object (array, cell, cell array, struct, struct array)\n%      filename: a string for the file name to save the output UBJSON data\n%      opt: a struct for additional options, ignore to use default values.\n%        opt can have the following fields (first in [.|.] is the default)\n%\n%        opt.FileName [''|string]: a file name to save the output JSON data\n%        opt.ArrayToStruct[0|1]: when set to 0, saveubjson outputs 1D/2D\n%                         array in JSON array format; if sets to 1, an\n%                         array will be shown as a struct with fields\n%                         \"_ArrayType_\", \"_ArraySize_\" and \"_ArrayData_\"; for\n%                         sparse arrays, the non-zero elements will be\n%                         saved to _ArrayData_ field in triplet-format i.e.\n%                         (ix,iy,val) and \"_ArrayIsSparse_\" will be added\n%                         with a value of 1; for a complex array, the \n%                         _ArrayData_ array will include two columns \n%                         (4 for sparse) to record the real and imaginary \n%                         parts, and also \"_ArrayIsComplex_\":1 is added. \n%        opt.ParseLogical [1|0]: if this is set to 1, logical array elem\n%                         will use true/false rather than 1/0.\n%        opt.NoRowBracket [1|0]: if this is set to 1, arrays with a single\n%                         numerical element will be shown without a square\n%                         bracket, unless it is the root object; if 0, square\n%                         brackets are forced for any numerical arrays.\n%        opt.ForceRootName [0|1]: when set to 1 and rootname is empty, saveubjson\n%                         will use the name of the passed obj variable as the \n%                         root object name; if obj is an expression and \n%                         does not have a name, 'root' will be used; if this \n%                         is set to 0 and rootname is empty, the root level \n%                         will be merged down to the lower level.\n%        opt.JSONP [''|string]: to generate a JSONP output (JSON with padding),\n%                         for example, if opt.JSON='foo', the JSON data is\n%                         wrapped inside a function call as 'foo(...);'\n%        opt.UnpackHex [1|0]: conver the 0x[hex code] output by loadjson \n%                         back to the string form\n%\n%        opt can be replaced by a list of ('param',value) pairs. The param \n%        string is equivallent to a field in opt and is case sensitive.\n% output:\n%      json: a binary string in the UBJSON format (see http://ubjson.org)\n%\n% examples:\n%      jsonmesh=struct('MeshNode',[0 0 0;1 0 0;0 1 0;1 1 0;0 0 1;1 0 1;0 1 1;1 1 1],... \n%               'MeshTetra',[1 2 4 8;1 3 4 8;1 2 6 8;1 5 6 8;1 5 7 8;1 3 7 8],...\n%               'MeshTri',[1 2 4;1 2 6;1 3 4;1 3 7;1 5 6;1 5 7;...\n%                          2 8 4;2 8 6;3 8 4;3 8 7;5 8 6;5 8 7],...\n%               'MeshCreator','FangQ','MeshTitle','T6 Cube',...\n%               'SpecialData',[nan, inf, -inf]);\n%      saveubjson('jsonmesh',jsonmesh)\n%      saveubjson('jsonmesh',jsonmesh,'meshdata.ubj')\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details\n%\n% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nif(nargin==1)\n   varname=inputname(1);\n   obj=rootname;\n   if(isempty(varname)) \n      varname='root';\n   end\n   rootname=varname;\nelse\n   varname=inputname(2);\nend\nif(length(varargin)==1 && ischar(varargin{1}))\n   opt=struct('FileName',varargin{1});\nelse\n   opt=varargin2struct(varargin{:});\nend\nopt.IsOctave=exist('OCTAVE_VERSION','builtin');\nrootisarray=0;\nrootlevel=1;\nforceroot=jsonopt('ForceRootName',0,opt);\nif((isnumeric(obj) || islogical(obj) || ischar(obj) || isstruct(obj) || iscell(obj)) && isempty(rootname) && forceroot==0)\n    rootisarray=1;\n    rootlevel=0;\nelse\n    if(isempty(rootname))\n        rootname=varname;\n    end\nend\nif((isstruct(obj) || iscell(obj))&& isempty(rootname) && forceroot)\n    rootname='root';\nend\njson=obj2ubjson(rootname,obj,rootlevel,opt);\nif(~rootisarray)\n    json=['{' json '}'];\nend\n\njsonp=jsonopt('JSONP','',opt);\nif(~isempty(jsonp))\n    json=[jsonp '(' json ')'];\nend\n\n% save to a file if FileName is set, suggested by Patrick Rapin\nif(~isempty(jsonopt('FileName','',opt)))\n    fid = fopen(opt.FileName, 'wb');\n    fwrite(fid,json);\n    fclose(fid);\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=obj2ubjson(name,item,level,varargin)\n\nif(iscell(item))\n    txt=cell2ubjson(name,item,level,varargin{:});\nelseif(isstruct(item))\n    txt=struct2ubjson(name,item,level,varargin{:});\nelseif(ischar(item))\n    txt=str2ubjson(name,item,level,varargin{:});\nelse\n    txt=mat2ubjson(name,item,level,varargin{:});\nend\n\n%%-------------------------------------------------------------------------\nfunction txt=cell2ubjson(name,item,level,varargin)\ntxt='';\nif(~iscell(item))\n        error('input is not a cell');\nend\n\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item); % let's handle 1D cell first\nif(len>1) \n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) '[']; name=''; \n    else\n        txt='['; \n    end\nelseif(len==0)\n    if(~isempty(name))\n        txt=[S_(checkname(name,varargin{:})) 'Z']; name=''; \n    else\n        txt='Z'; \n    end\nend\nfor j=1:dim(2)\n    if(dim(1)>1) txt=[txt '[']; end\n    for i=1:dim(1)\n       txt=[txt obj2ubjson(name,item{i,j},level+(len>1),varargin{:})];\n    end\n    if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=struct2ubjson(name,item,level,varargin)\ntxt='';\nif(~isstruct(item))\n\terror('input is not a struct');\nend\ndim=size(item);\nif(ndims(squeeze(item))>2) % for 3D or higher dimensions, flatten to 2D for now\n    item=reshape(item,dim(1),numel(item)/dim(1));\n    dim=size(item);\nend\nlen=numel(item);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nfor j=1:dim(2)\n  if(dim(1)>1) txt=[txt '[']; end\n  for i=1:dim(1)\n     names = fieldnames(item(i,j));\n     if(~isempty(name) && len==1)\n        txt=[txt S_(checkname(name,varargin{:})) '{']; \n     else\n        txt=[txt '{']; \n     end\n     if(~isempty(names))\n       for e=1:length(names)\n\t     txt=[txt obj2ubjson(names{e},getfield(item(i,j),...\n             names{e}),level+(dim(1)>1)+1+(len>1),varargin{:})];\n       end\n     end\n     txt=[txt '}'];\n  end\n  if(dim(1)>1) txt=[txt ']']; end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=str2ubjson(name,item,level,varargin)\ntxt='';\nif(~ischar(item))\n        error('input is not a string');\nend\nitem=reshape(item, max(size(item),[1 0]));\nlen=size(item,1);\n\nif(~isempty(name)) \n    if(len>1) txt=[S_(checkname(name,varargin{:})) '[']; end\nelse\n    if(len>1) txt='['; end\nend\nisoct=jsonopt('IsOctave',0,varargin{:});\nfor e=1:len\n    val=item(e,:);\n    if(len==1)\n        obj=['' S_(checkname(name,varargin{:})) '' '',S_(val),''];\n\tif(isempty(name)) obj=['',S_(val),'']; end\n        txt=[txt,'',obj];\n    else\n        txt=[txt,'',['',S_(val),'']];\n    end\nend\nif(len>1) txt=[txt ']']; end\n\n%%-------------------------------------------------------------------------\nfunction txt=mat2ubjson(name,item,level,varargin)\nif(~isnumeric(item) && ~islogical(item))\n        error('input is not an array');\nend\n\nif(length(size(item))>2 || issparse(item) || ~isreal(item) || ...\n   isempty(item) || jsonopt('ArrayToStruct',0,varargin{:}))\n      cid=I_(uint32(max(size(item))));\n      if(isempty(name))\n    \ttxt=['{' S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1)) ];\n      else\n          if(isempty(item))\n              txt=[S_(checkname(name,varargin{:})),'Z'];\n              return;\n          else\n    \t      txt=[S_(checkname(name,varargin{:})),'{',S_('_ArrayType_'),S_(class(item)),S_('_ArraySize_'),I_a(size(item),cid(1))];\n          end\n      end\nelse\n    if(isempty(name))\n    \ttxt=matdata2ubjson(item,level+1,varargin{:});\n    else\n        if(numel(item)==1 && jsonopt('NoRowBracket',1,varargin{:})==1)\n            numtxt=regexprep(regexprep(matdata2ubjson(item,level+1,varargin{:}),'^\\[',''),']','');\n           \ttxt=[S_(checkname(name,varargin{:})) numtxt];\n        else\n    \t    txt=[S_(checkname(name,varargin{:})),matdata2ubjson(item,level+1,varargin{:})];\n        end\n    end\n    return;\nend\nif(issparse(item))\n    [ix,iy]=find(item);\n    data=full(item(find(item)));\n    if(~isreal(item))\n       data=[real(data(:)),imag(data(:))];\n       if(size(item,1)==1)\n           % Kludge to have data's 'transposedness' match item's.\n           % (Necessary for complex row vector handling below.)\n           data=data';\n       end\n       txt=[txt,S_('_ArrayIsComplex_'),'T'];\n    end\n    txt=[txt,S_('_ArrayIsSparse_'),'T'];\n    if(size(item,1)==1)\n        % Row vector, store only column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([iy(:),data'],level+2,varargin{:})];\n    elseif(size(item,2)==1)\n        % Column vector, store only row indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,data],level+2,varargin{:})];\n    else\n        % General case, store row and column indices.\n        txt=[txt,S_('_ArrayData_'),...\n           matdata2ubjson([ix,iy,data],level+2,varargin{:})];\n    end\nelse\n    if(isreal(item))\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson(item(:)',level+2,varargin{:})];\n    else\n        txt=[txt,S_('_ArrayIsComplex_'),'T'];\n        txt=[txt,S_('_ArrayData_'),...\n            matdata2ubjson([real(item(:)) imag(item(:))],level+2,varargin{:})];\n    end\nend\ntxt=[txt,'}'];\n\n%%-------------------------------------------------------------------------\nfunction txt=matdata2ubjson(mat,level,varargin)\nif(isempty(mat))\n    txt='Z';\n    return;\nend\nif(size(mat,1)==1)\n    level=level-1;\nend\ntype='';\nhasnegtive=(mat<0);\nif(isa(mat,'integer') || isinteger(mat) || (isfloat(mat) && all(mod(mat(:),1) == 0)))\n    if(isempty(hasnegtive))\n       if(max(mat(:))<=2^8)\n           type='U';\n       end\n    end\n    if(isempty(type))\n        % todo - need to consider negative ones separately\n        id= histc(abs(max(mat(:))),[0 2^7 2^15 2^31 2^63]);\n        if(isempty(find(id)))\n            error('high-precision data is not yet supported');\n        end\n        key='iIlL';\n\ttype=key(find(id));\n    end\n    txt=[I_a(mat(:),type,size(mat))];\nelseif(islogical(mat))\n    logicalval='FT';\n    if(numel(mat)==1)\n        txt=logicalval(mat+1);\n    else\n        txt=['[$U#' I_a(size(mat),'l') typecast(swapbytes(uint8(mat(:)')),'uint8')];\n    end\nelse\n    if(numel(mat)==1)\n        txt=['[' D_(mat) ']'];\n    else\n        txt=D_a(mat(:),'D',size(mat));\n    end\nend\n\n%txt=regexprep(mat2str(mat),'\\s+',',');\n%txt=regexprep(txt,';',sprintf('],['));\n% if(nargin>=2 && size(mat,1)>1)\n%     txt=regexprep(txt,'\\[',[repmat(sprintf('\\t'),1,level) '[']);\n% end\nif(any(isinf(mat(:))))\n    txt=regexprep(txt,'([-+]*)Inf',jsonopt('Inf','\"$1_Inf_\"',varargin{:}));\nend\nif(any(isnan(mat(:))))\n    txt=regexprep(txt,'NaN',jsonopt('NaN','\"_NaN_\"',varargin{:}));\nend\n\n%%-------------------------------------------------------------------------\nfunction newname=checkname(name,varargin)\nisunpack=jsonopt('UnpackHex',1,varargin{:});\nnewname=name;\nif(isempty(regexp(name,'0x([0-9a-fA-F]+)_','once')))\n    return\nend\nif(isunpack)\n    isoct=jsonopt('IsOctave',0,varargin{:});\n    if(~isoct)\n        newname=regexprep(name,'(^x|_){1}0x([0-9a-fA-F]+)_','${native2unicode(hex2dec($2))}');\n    else\n        pos=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','start');\n        pend=regexp(name,'(^x|_){1}0x([0-9a-fA-F]+)_','end');\n        if(isempty(pos)) return; end\n        str0=name;\n        pos0=[0 pend(:)' length(name)];\n        newname='';\n        for i=1:length(pos)\n            newname=[newname str0(pos0(i)+1:pos(i)-1) char(hex2dec(str0(pos(i)+3:pend(i)-1)))];\n        end\n        if(pos(end)~=length(name))\n            newname=[newname str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\nend\n%%-------------------------------------------------------------------------\nfunction val=S_(str)\nif(length(str)==1)\n  val=['C' str];\nelse\n  val=['S' I_(int32(length(str))) str];\nend\n%%-------------------------------------------------------------------------\nfunction val=I_(num)\nif(~isinteger(num))\n    error('input is not an integer');\nend\nif(num>=0 && num<255)\n   val=['U' data2byte(swapbytes(cast(num,'uint8')),'uint8')];\n   return;\nend\nkey='iIlL';\ncid={'int8','int16','int32','int64'};\nfor i=1:4\n  if((num>0 && num<2^(i*8-1)) || (num<0 && num>=-2^(i*8-1)))\n    val=[key(i) data2byte(swapbytes(cast(num,cid{i})),'uint8')];\n    return;\n  end\nend\nerror('unsupported integer');\n\n%%-------------------------------------------------------------------------\nfunction val=D_(num)\nif(~isfloat(num))\n    error('input is not a float');\nend\n\nif(isa(num,'single'))\n  val=['d' data2byte(num,'uint8')];\nelse\n  val=['D' data2byte(num,'uint8')];\nend\n%%-------------------------------------------------------------------------\nfunction data=I_a(num,type,dim,format)\nid=find(ismember('iUIlL',type));\n\nif(id==0)\n  error('unsupported integer array');\nend\n\n% based on UBJSON specs, all integer types are stored in big endian format\n\nif(id==1)\n  data=data2byte(swapbytes(int8(num)),'uint8');\n  blen=1;\nelseif(id==2)\n  data=data2byte(swapbytes(uint8(num)),'uint8');\n  blen=1;\nelseif(id==3)\n  data=data2byte(swapbytes(int16(num)),'uint8');\n  blen=2;\nelseif(id==4)\n  data=data2byte(swapbytes(int32(num)),'uint8');\n  blen=4;\nelseif(id==5)\n  data=data2byte(swapbytes(int64(num)),'uint8');\n  blen=8;\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/blen)) data(:)'];\n  end\n  data=['[' data(:)'];\nelse\n  data=reshape(data,blen,numel(data)/blen);\n  data(2:blen+1,:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction data=D_a(num,type,dim,format)\nid=find(ismember('dD',type));\n\nif(id==0)\n  error('unsupported float array');\nend\n\nif(id==1)\n  data=data2byte(single(num),'uint8');\nelseif(id==2)\n  data=data2byte(double(num),'uint8');\nend\n\nif(nargin>=3 && length(dim)>=2 && prod(dim)~=dim(2))\n  format='opt';\nend\nif((nargin<4 || strcmp(format,'opt')) && numel(num)>1)\n  if(nargin>=3 && (length(dim)==1 || (length(dim)>=2 && prod(dim)~=dim(2))))\n      cid=I_(uint32(max(dim)));\n      data=['$' type '#' I_a(dim,cid(1)) data(:)'];\n  else\n      data=['$' type '#' I_(int32(numel(data)/(id*4))) data(:)'];\n  end\n  data=['[' data];\nelse\n  data=reshape(data,(id*4),length(data)/(id*4));\n  data(2:(id*4+1),:)=data;\n  data(1,:)=type;\n  data=data(:)';\n  data=['[' data(:)' ']'];\nend\n%%-------------------------------------------------------------------------\nfunction bytes=data2byte(varargin)\nbytes=typecast(varargin{:});\nbytes=bytes(:)';\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/jsonlab/varargin2struct.m",
    "content": "function opt=varargin2struct(varargin)\n%\n% opt=varargin2struct('param1',value1,'param2',value2,...)\n%   or\n% opt=varargin2struct(...,optstruct,...)\n%\n% convert a series of input parameters into a structure\n%\n% authors:Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)\n% date: 2012/12/22\n%\n% input:\n%      'param', value: the input parameters should be pairs of a string and a value\n%       optstruct: if a parameter is a struct, the fields will be merged to the output struct\n%\n% output:\n%      opt: a struct where opt.param1=value1, opt.param2=value2 ...\n%\n% license:\n%     BSD, see LICENSE_BSD.txt files for details \n%\n% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)\n%\n\nlen=length(varargin);\nopt=struct;\nif(len==0) return; end\ni=1;\nwhile(i<=len)\n    if(isstruct(varargin{i}))\n        opt=mergestruct(opt,varargin{i});\n    elseif(ischar(varargin{i}) && i<len)\n        opt=setfield(opt,varargin{i},varargin{i+1});\n        i=i+1;\n    else\n        error('input must be in the form of ...,''name'',value,... pairs or structs');\n    end\n    i=i+1;\nend\n\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/makeValidFieldName.m",
    "content": "function str = makeValidFieldName(str)\n% From MATLAB doc: field names must begin with a letter, which may be\n% followed by any combination of letters, digits, and underscores.\n% Invalid characters will be converted to underscores, and the prefix\n% \"x0x[Hex code]_\" will be added if the first character is not a letter.\n    isoct=exist('OCTAVE_VERSION','builtin');\n    pos=regexp(str,'^[^A-Za-z]','once');\n    if(~isempty(pos))\n        if(~isoct)\n            str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');\n        else\n            str=sprintf('x0x%X_%s',char(str(1)),str(2:end));\n        end\n    end\n    if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return;  end\n    if(~isoct)\n        str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');\n    else\n        pos=regexp(str,'[^0-9A-Za-z_]');\n        if(isempty(pos)) return; end\n        str0=str;\n        pos0=[0 pos(:)' length(str)];\n        str='';\n        for i=1:length(pos)\n            str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];\n        end\n        if(pos(end)~=length(str))\n            str=[str str0(pos0(end-1)+1:pos0(end))];\n        end\n    end\n"
  },
  {
    "path": "machine-learning-ex8/ex8/lib/submitWithConfiguration.m",
    "content": "function submitWithConfiguration(conf)\n  addpath('./lib/jsonlab');\n\n  parts = parts(conf);\n\n  fprintf('== Submitting solutions | %s...\\n', conf.itemName);\n\n  tokenFile = 'token.mat';\n  if exist(tokenFile, 'file')\n    load(tokenFile);\n    [email token] = promptToken(email, token, tokenFile);\n  else\n    [email token] = promptToken('', '', tokenFile);\n  end\n\n  if isempty(token)\n    fprintf('!! Submission Cancelled\\n');\n    return\n  end\n\n  try\n    response = submitParts(conf, email, token, parts);\n  catch\n    e = lasterror();\n    fprintf( ...\n      '!! Submission failed: unexpected error: %s\\n', ...\n      e.message);\n    fprintf('!! Please try again later.\\n');\n    return\n  end\n\n  if isfield(response, 'errorMessage')\n    fprintf('!! Submission failed: %s\\n', response.errorMessage);\n  else\n    showFeedback(parts, response);\n    save(tokenFile, 'email', 'token');\n  end\nend\n\nfunction [email token] = promptToken(email, existingToken, tokenFile)\n  if (~isempty(email) && ~isempty(existingToken))\n    prompt = sprintf( ...\n      'Use token from last successful submission (%s)? (Y/n): ', ...\n      email);\n    reenter = input(prompt, 's');\n\n    if (isempty(reenter) || reenter(1) == 'Y' || reenter(1) == 'y')\n      token = existingToken;\n      return;\n    else\n      delete(tokenFile);\n    end\n  end\n  email = input('Login (email address): ', 's');\n  token = input('Token: ', 's');\nend\n\nfunction isValid = isValidPartOptionIndex(partOptions, i)\n  isValid = (~isempty(i)) && (1 <= i) && (i <= numel(partOptions));\nend\n\nfunction response = submitParts(conf, email, token, parts)\n  body = makePostBody(conf, email, token, parts);\n  submissionUrl = submissionUrl();\n  params = {'jsonBody', body};\n  responseBody = urlread(submissionUrl, 'post', params);\n  response = loadjson(responseBody);\nend\n\nfunction body = makePostBody(conf, email, token, parts)\n  bodyStruct.assignmentSlug = conf.assignmentSlug;\n  bodyStruct.submitterEmail = email;\n  bodyStruct.secret = token;\n  bodyStruct.parts = makePartsStruct(conf, parts);\n\n  opt.Compact = 1;\n  body = savejson('', bodyStruct, opt);\nend\n\nfunction partsStruct = makePartsStruct(conf, parts)\n  for part = parts\n    partId = part{:}.id;\n    fieldName = makeValidFieldName(partId);\n    outputStruct.output = conf.output(partId);\n    partsStruct.(fieldName) = outputStruct;\n  end\nend\n\nfunction [parts] = parts(conf)\n  parts = {};\n  for partArray = conf.partArrays\n    part.id = partArray{:}{1};\n    part.sourceFiles = partArray{:}{2};\n    part.name = partArray{:}{3};\n    parts{end + 1} = part;\n  end\nend\n\nfunction showFeedback(parts, response)\n  fprintf('== \\n');\n  fprintf('== %43s | %9s | %-s\\n', 'Part Name', 'Score', 'Feedback');\n  fprintf('== %43s | %9s | %-s\\n', '---------', '-----', '--------');\n  for part = parts\n    score = '';\n    partFeedback = '';\n    partFeedback = response.partFeedbacks.(makeValidFieldName(part{:}.id));\n    partEvaluation = response.partEvaluations.(makeValidFieldName(part{:}.id));\n    score = sprintf('%d / %3d', partEvaluation.score, partEvaluation.maxScore);\n    fprintf('== %43s | %9s | %-s\\n', part{:}.name, score, partFeedback);\n  end\n  evaluation = response.evaluation;\n  totalScore = sprintf('%d / %d', evaluation.score, evaluation.maxScore);\n  fprintf('==                                   --------------------------------\\n');\n  fprintf('== %43s | %9s | %-s\\n', '', totalScore, '');\n  fprintf('== \\n');\nend\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n%\n% Service configuration\n%\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\nfunction submissionUrl = submissionUrl()\n  submissionUrl = 'https://www-origin.coursera.org/api/onDemandProgrammingImmediateFormSubmissions.v1';\nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/loadMovieList.m",
    "content": "function movieList = loadMovieList()\n%GETMOVIELIST reads the fixed movie list in movie.txt and returns a\n%cell array of the words\n%   movieList = GETMOVIELIST() reads the fixed movie list in movie.txt \n%   and returns a cell array of the words in movieList.\n\n\n%% Read the fixed movieulary list\nfid = fopen('movie_ids.txt');\n\n% Store all movies in cell array movie{}\nn = 1682;  % Total number of movies \n\nmovieList = cell(n, 1);\nfor i = 1:n\n    % Read line\n    line = fgets(fid);\n    % Word Index (can ignore since it will be = i)\n    [idx, movieName] = strtok(line, ' ');\n    % Actual Word\n    movieList{i} = strtrim(movieName);\nend\nfclose(fid);\n\nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/movie_ids.txt",
    "content": "1 Toy Story (1995)\n2 GoldenEye (1995)\n3 Four Rooms (1995)\n4 Get Shorty (1995)\n5 Copycat (1995)\n6 Shanghai Triad (Yao a yao yao dao waipo qiao) (1995)\n7 Twelve Monkeys (1995)\n8 Babe (1995)\n9 Dead Man Walking (1995)\n10 Richard III (1995)\n11 Seven (Se7en) (1995)\n12 Usual Suspects, The (1995)\n13 Mighty Aphrodite (1995)\n14 Postino, Il (1994)\n15 Mr. Holland's Opus (1995)\n16 French Twist (Gazon maudit) (1995)\n17 From Dusk Till Dawn (1996)\n18 White Balloon, The (1995)\n19 Antonia's Line (1995)\n20 Angels and Insects (1995)\n21 Muppet Treasure Island (1996)\n22 Braveheart (1995)\n23 Taxi Driver (1976)\n24 Rumble in the Bronx (1995)\n25 Birdcage, The (1996)\n26 Brothers McMullen, The (1995)\n27 Bad Boys (1995)\n28 Apollo 13 (1995)\n29 Batman Forever (1995)\n30 Belle de jour (1967)\n31 Crimson Tide (1995)\n32 Crumb (1994)\n33 Desperado (1995)\n34 Doom Generation, The (1995)\n35 Free Willy 2: The Adventure Home (1995)\n36 Mad Love (1995)\n37 Nadja (1994)\n38 Net, The (1995)\n39 Strange Days (1995)\n40 To Wong Foo, Thanks for Everything! Julie Newmar (1995)\n41 Billy Madison (1995)\n42 Clerks (1994)\n43 Disclosure (1994)\n44 Dolores Claiborne (1994)\n45 Eat Drink Man Woman (1994)\n46 Exotica (1994)\n47 Ed Wood (1994)\n48 Hoop Dreams (1994)\n49 I.Q. (1994)\n50 Star Wars (1977)\n51 Legends of the Fall (1994)\n52 Madness of King George, The (1994)\n53 Natural Born Killers (1994)\n54 Outbreak (1995)\n55 Professional, The (1994)\n56 Pulp Fiction (1994)\n57 Priest (1994)\n58 Quiz Show (1994)\n59 Three Colors: Red (1994)\n60 Three Colors: Blue (1993)\n61 Three Colors: White (1994)\n62 Stargate (1994)\n63 Santa Clause, The (1994)\n64 Shawshank Redemption, The (1994)\n65 What's Eating Gilbert Grape (1993)\n66 While You Were Sleeping (1995)\n67 Ace Ventura: Pet Detective (1994)\n68 Crow, The (1994)\n69 Forrest Gump (1994)\n70 Four Weddings and a Funeral (1994)\n71 Lion King, The (1994)\n72 Mask, The (1994)\n73 Maverick (1994)\n74 Faster Pussycat! Kill! Kill! (1965)\n75 Brother Minister: The Assassination of Malcolm X (1994)\n76 Carlito's Way (1993)\n77 Firm, The (1993)\n78 Free Willy (1993)\n79 Fugitive, The (1993)\n80 Hot Shots! Part Deux (1993)\n81 Hudsucker Proxy, The (1994)\n82 Jurassic Park (1993)\n83 Much Ado About Nothing (1993)\n84 Robert A. Heinlein's The Puppet Masters (1994)\n85 Ref, The (1994)\n86 Remains of the Day, The (1993)\n87 Searching for Bobby Fischer (1993)\n88 Sleepless in Seattle (1993)\n89 Blade Runner (1982)\n90 So I Married an Axe Murderer (1993)\n91 Nightmare Before Christmas, The (1993)\n92 True Romance (1993)\n93 Welcome to the Dollhouse (1995)\n94 Home Alone (1990)\n95 Aladdin (1992)\n96 Terminator 2: Judgment Day (1991)\n97 Dances with Wolves (1990)\n98 Silence of the Lambs, The (1991)\n99 Snow White and the Seven Dwarfs (1937)\n100 Fargo (1996)\n101 Heavy Metal (1981)\n102 Aristocats, The (1970)\n103 All Dogs Go to Heaven 2 (1996)\n104 Theodore Rex (1995)\n105 Sgt. Bilko (1996)\n106 Diabolique (1996)\n107 Moll Flanders (1996)\n108 Kids in the Hall: Brain Candy (1996)\n109 Mystery Science Theater 3000: The Movie (1996)\n110 Operation Dumbo Drop (1995)\n111 Truth About Cats & Dogs, The (1996)\n112 Flipper (1996)\n113 Horseman on the Roof, The (Hussard sur le toit, Le) (1995)\n114 Wallace & Gromit: The Best of Aardman Animation (1996)\n115 Haunted World of Edward D. Wood Jr., The (1995)\n116 Cold Comfort Farm (1995)\n117 Rock, The (1996)\n118 Twister (1996)\n119 Maya Lin: A Strong Clear Vision (1994)\n120 Striptease (1996)\n121 Independence Day (ID4) (1996)\n122 Cable Guy, The (1996)\n123 Frighteners, The (1996)\n124 Lone Star (1996)\n125 Phenomenon (1996)\n126 Spitfire Grill, The (1996)\n127 Godfather, The (1972)\n128 Supercop (1992)\n129 Bound (1996)\n130 Kansas City (1996)\n131 Breakfast at Tiffany's (1961)\n132 Wizard of Oz, The (1939)\n133 Gone with the Wind (1939)\n134 Citizen Kane (1941)\n135 2001: A Space Odyssey (1968)\n136 Mr. Smith Goes to Washington (1939)\n137 Big Night (1996)\n138 D3: The Mighty Ducks (1996)\n139 Love Bug, The (1969)\n140 Homeward Bound: The Incredible Journey (1993)\n141 20,000 Leagues Under the Sea (1954)\n142 Bedknobs and Broomsticks (1971)\n143 Sound of Music, The (1965)\n144 Die Hard (1988)\n145 Lawnmower Man, The (1992)\n146 Unhook the Stars (1996)\n147 Long Kiss Goodnight, The (1996)\n148 Ghost and the Darkness, The (1996)\n149 Jude (1996)\n150 Swingers (1996)\n151 Willy Wonka and the Chocolate Factory (1971)\n152 Sleeper (1973)\n153 Fish Called Wanda, A (1988)\n154 Monty Python's Life of Brian (1979)\n155 Dirty Dancing (1987)\n156 Reservoir Dogs (1992)\n157 Platoon (1986)\n158 Weekend at Bernie's (1989)\n159 Basic Instinct (1992)\n160 Glengarry Glen Ross (1992)\n161 Top Gun (1986)\n162 On Golden Pond (1981)\n163 Return of the Pink Panther, The (1974)\n164 Abyss, The (1989)\n165 Jean de Florette (1986)\n166 Manon of the Spring (Manon des sources) (1986)\n167 Private Benjamin (1980)\n168 Monty Python and the Holy Grail (1974)\n169 Wrong Trousers, The (1993)\n170 Cinema Paradiso (1988)\n171 Delicatessen (1991)\n172 Empire Strikes Back, The (1980)\n173 Princess Bride, The (1987)\n174 Raiders of the Lost Ark (1981)\n175 Brazil (1985)\n176 Aliens (1986)\n177 Good, The Bad and The Ugly, The (1966)\n178 12 Angry Men (1957)\n179 Clockwork Orange, A (1971)\n180 Apocalypse Now (1979)\n181 Return of the Jedi (1983)\n182 GoodFellas (1990)\n183 Alien (1979)\n184 Army of Darkness (1993)\n185 Psycho (1960)\n186 Blues Brothers, The (1980)\n187 Godfather: Part II, The (1974)\n188 Full Metal Jacket (1987)\n189 Grand Day Out, A (1992)\n190 Henry V (1989)\n191 Amadeus (1984)\n192 Raging Bull (1980)\n193 Right Stuff, The (1983)\n194 Sting, The (1973)\n195 Terminator, The (1984)\n196 Dead Poets Society (1989)\n197 Graduate, The (1967)\n198 Nikita (La Femme Nikita) (1990)\n199 Bridge on the River Kwai, The (1957)\n200 Shining, The (1980)\n201 Evil Dead II (1987)\n202 Groundhog Day (1993)\n203 Unforgiven (1992)\n204 Back to the Future (1985)\n205 Patton (1970)\n206 Akira (1988)\n207 Cyrano de Bergerac (1990)\n208 Young Frankenstein (1974)\n209 This Is Spinal Tap (1984)\n210 Indiana Jones and the Last Crusade (1989)\n211 M*A*S*H (1970)\n212 Unbearable Lightness of Being, The (1988)\n213 Room with a View, A (1986)\n214 Pink Floyd - The Wall (1982)\n215 Field of Dreams (1989)\n216 When Harry Met Sally... (1989)\n217 Bram Stoker's Dracula (1992)\n218 Cape Fear (1991)\n219 Nightmare on Elm Street, A (1984)\n220 Mirror Has Two Faces, The (1996)\n221 Breaking the Waves (1996)\n222 Star Trek: First Contact (1996)\n223 Sling Blade (1996)\n224 Ridicule (1996)\n225 101 Dalmatians (1996)\n226 Die Hard 2 (1990)\n227 Star Trek VI: The Undiscovered Country (1991)\n228 Star Trek: The Wrath of Khan (1982)\n229 Star Trek III: The Search for Spock (1984)\n230 Star Trek IV: The Voyage Home (1986)\n231 Batman Returns (1992)\n232 Young Guns (1988)\n233 Under Siege (1992)\n234 Jaws (1975)\n235 Mars Attacks! (1996)\n236 Citizen Ruth (1996)\n237 Jerry Maguire (1996)\n238 Raising Arizona (1987)\n239 Sneakers (1992)\n240 Beavis and Butt-head Do America (1996)\n241 Last of the Mohicans, The (1992)\n242 Kolya (1996)\n243 Jungle2Jungle (1997)\n244 Smilla's Sense of Snow (1997)\n245 Devil's Own, The (1997)\n246 Chasing Amy (1997)\n247 Turbo: A Power Rangers Movie (1997)\n248 Grosse Pointe Blank (1997)\n249 Austin Powers: International Man of Mystery (1997)\n250 Fifth Element, The (1997)\n251 Shall We Dance? (1996)\n252 Lost World: Jurassic Park, The (1997)\n253 Pillow Book, The (1995)\n254 Batman & Robin (1997)\n255 My Best Friend's Wedding (1997)\n256 When the Cats Away (Chacun cherche son chat) (1996)\n257 Men in Black (1997)\n258 Contact (1997)\n259 George of the Jungle (1997)\n260 Event Horizon (1997)\n261 Air Bud (1997)\n262 In the Company of Men (1997)\n263 Steel (1997)\n264 Mimic (1997)\n265 Hunt for Red October, The (1990)\n266 Kull the Conqueror (1997)\n267 unknown\n268 Chasing Amy (1997)\n269 Full Monty, The (1997)\n270 Gattaca (1997)\n271 Starship Troopers (1997)\n272 Good Will Hunting (1997)\n273 Heat (1995)\n274 Sabrina (1995)\n275 Sense and Sensibility (1995)\n276 Leaving Las Vegas (1995)\n277 Restoration (1995)\n278 Bed of Roses (1996)\n279 Once Upon a Time... When We Were Colored (1995)\n280 Up Close and Personal (1996)\n281 River Wild, The (1994)\n282 Time to Kill, A (1996)\n283 Emma (1996)\n284 Tin Cup (1996)\n285 Secrets & Lies (1996)\n286 English Patient, The (1996)\n287 Marvin's Room (1996)\n288 Scream (1996)\n289 Evita (1996)\n290 Fierce Creatures (1997)\n291 Absolute Power (1997)\n292 Rosewood (1997)\n293 Donnie Brasco (1997)\n294 Liar Liar (1997)\n295 Breakdown (1997)\n296 Promesse, La (1996)\n297 Ulee's Gold (1997)\n298 Face/Off (1997)\n299 Hoodlum (1997)\n300 Air Force One (1997)\n301 In & Out (1997)\n302 L.A. Confidential (1997)\n303 Ulee's Gold (1997)\n304 Fly Away Home (1996)\n305 Ice Storm, The (1997)\n306 Mrs. Brown (Her Majesty, Mrs. Brown) (1997)\n307 Devil's Advocate, The (1997)\n308 FairyTale: A True Story (1997)\n309 Deceiver (1997)\n310 Rainmaker, The (1997)\n311 Wings of the Dove, The (1997)\n312 Midnight in the Garden of Good and Evil (1997)\n313 Titanic (1997)\n314 3 Ninjas: High Noon At Mega Mountain (1998)\n315 Apt Pupil (1998)\n316 As Good As It Gets (1997)\n317 In the Name of the Father (1993)\n318 Schindler's List (1993)\n319 Everyone Says I Love You (1996)\n320 Paradise Lost: The Child Murders at Robin Hood Hills (1996)\n321 Mother (1996)\n322 Murder at 1600 (1997)\n323 Dante's Peak (1997)\n324 Lost Highway (1997)\n325 Crash (1996)\n326 G.I. Jane (1997)\n327 Cop Land (1997)\n328 Conspiracy Theory (1997)\n329 Desperate Measures (1998)\n330 187 (1997)\n331 Edge, The (1997)\n332 Kiss the Girls (1997)\n333 Game, The (1997)\n334 U Turn (1997)\n335 How to Be a Player (1997)\n336 Playing God (1997)\n337 House of Yes, The (1997)\n338 Bean (1997)\n339 Mad City (1997)\n340 Boogie Nights (1997)\n341 Critical Care (1997)\n342 Man Who Knew Too Little, The (1997)\n343 Alien: Resurrection (1997)\n344 Apostle, The (1997)\n345 Deconstructing Harry (1997)\n346 Jackie Brown (1997)\n347 Wag the Dog (1997)\n348 Desperate Measures (1998)\n349 Hard Rain (1998)\n350 Fallen (1998)\n351 Prophecy II, The (1998)\n352 Spice World (1997)\n353 Deep Rising (1998)\n354 Wedding Singer, The (1998)\n355 Sphere (1998)\n356 Client, The (1994)\n357 One Flew Over the Cuckoo's Nest (1975)\n358 Spawn (1997)\n359 Assignment, The (1997)\n360 Wonderland (1997)\n361 Incognito (1997)\n362 Blues Brothers 2000 (1998)\n363 Sudden Death (1995)\n364 Ace Ventura: When Nature Calls (1995)\n365 Powder (1995)\n366 Dangerous Minds (1995)\n367 Clueless (1995)\n368 Bio-Dome (1996)\n369 Black Sheep (1996)\n370 Mary Reilly (1996)\n371 Bridges of Madison County, The (1995)\n372 Jeffrey (1995)\n373 Judge Dredd (1995)\n374 Mighty Morphin Power Rangers: The Movie (1995)\n375 Showgirls (1995)\n376 Houseguest (1994)\n377 Heavyweights (1994)\n378 Miracle on 34th Street (1994)\n379 Tales From the Crypt Presents: Demon Knight (1995)\n380 Star Trek: Generations (1994)\n381 Muriel's Wedding (1994)\n382 Adventures of Priscilla, Queen of the Desert, The (1994)\n383 Flintstones, The (1994)\n384 Naked Gun 33 1/3: The Final Insult (1994)\n385 True Lies (1994)\n386 Addams Family Values (1993)\n387 Age of Innocence, The (1993)\n388 Beverly Hills Cop III (1994)\n389 Black Beauty (1994)\n390 Fear of a Black Hat (1993)\n391 Last Action Hero (1993)\n392 Man Without a Face, The (1993)\n393 Mrs. Doubtfire (1993)\n394 Radioland Murders (1994)\n395 Robin Hood: Men in Tights (1993)\n396 Serial Mom (1994)\n397 Striking Distance (1993)\n398 Super Mario Bros. (1993)\n399 Three Musketeers, The (1993)\n400 Little Rascals, The (1994)\n401 Brady Bunch Movie, The (1995)\n402 Ghost (1990)\n403 Batman (1989)\n404 Pinocchio (1940)\n405 Mission: Impossible (1996)\n406 Thinner (1996)\n407 Spy Hard (1996)\n408 Close Shave, A (1995)\n409 Jack (1996)\n410 Kingpin (1996)\n411 Nutty Professor, The (1996)\n412 Very Brady Sequel, A (1996)\n413 Tales from the Crypt Presents: Bordello of Blood (1996)\n414 My Favorite Year (1982)\n415 Apple Dumpling Gang, The (1975)\n416 Old Yeller (1957)\n417 Parent Trap, The (1961)\n418 Cinderella (1950)\n419 Mary Poppins (1964)\n420 Alice in Wonderland (1951)\n421 William Shakespeare's Romeo and Juliet (1996)\n422 Aladdin and the King of Thieves (1996)\n423 E.T. the Extra-Terrestrial (1982)\n424 Children of the Corn: The Gathering (1996)\n425 Bob Roberts (1992)\n426 Transformers: The Movie, The (1986)\n427 To Kill a Mockingbird (1962)\n428 Harold and Maude (1971)\n429 Day the Earth Stood Still, The (1951)\n430 Duck Soup (1933)\n431 Highlander (1986)\n432 Fantasia (1940)\n433 Heathers (1989)\n434 Forbidden Planet (1956)\n435 Butch Cassidy and the Sundance Kid (1969)\n436 American Werewolf in London, An (1981)\n437 Amityville 1992: It's About Time (1992)\n438 Amityville 3-D (1983)\n439 Amityville: A New Generation (1993)\n440 Amityville II: The Possession (1982)\n441 Amityville Horror, The (1979)\n442 Amityville Curse, The (1990)\n443 Birds, The (1963)\n444 Blob, The (1958)\n445 Body Snatcher, The (1945)\n446 Burnt Offerings (1976)\n447 Carrie (1976)\n448 Omen, The (1976)\n449 Star Trek: The Motion Picture (1979)\n450 Star Trek V: The Final Frontier (1989)\n451 Grease (1978)\n452 Jaws 2 (1978)\n453 Jaws 3-D (1983)\n454 Bastard Out of Carolina (1996)\n455 Jackie Chan's First Strike (1996)\n456 Beverly Hills Ninja (1997)\n457 Free Willy 3: The Rescue (1997)\n458 Nixon (1995)\n459 Cry, the Beloved Country (1995)\n460 Crossing Guard, The (1995)\n461 Smoke (1995)\n462 Like Water For Chocolate (Como agua para chocolate) (1992)\n463 Secret of Roan Inish, The (1994)\n464 Vanya on 42nd Street (1994)\n465 Jungle Book, The (1994)\n466 Red Rock West (1992)\n467 Bronx Tale, A (1993)\n468 Rudy (1993)\n469 Short Cuts (1993)\n470 Tombstone (1993)\n471 Courage Under Fire (1996)\n472 Dragonheart (1996)\n473 James and the Giant Peach (1996)\n474 Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1963)\n475 Trainspotting (1996)\n476 First Wives Club, The (1996)\n477 Matilda (1996)\n478 Philadelphia Story, The (1940)\n479 Vertigo (1958)\n480 North by Northwest (1959)\n481 Apartment, The (1960)\n482 Some Like It Hot (1959)\n483 Casablanca (1942)\n484 Maltese Falcon, The (1941)\n485 My Fair Lady (1964)\n486 Sabrina (1954)\n487 Roman Holiday (1953)\n488 Sunset Blvd. (1950)\n489 Notorious (1946)\n490 To Catch a Thief (1955)\n491 Adventures of Robin Hood, The (1938)\n492 East of Eden (1955)\n493 Thin Man, The (1934)\n494 His Girl Friday (1940)\n495 Around the World in 80 Days (1956)\n496 It's a Wonderful Life (1946)\n497 Bringing Up Baby (1938)\n498 African Queen, The (1951)\n499 Cat on a Hot Tin Roof (1958)\n500 Fly Away Home (1996)\n501 Dumbo (1941)\n502 Bananas (1971)\n503 Candidate, The (1972)\n504 Bonnie and Clyde (1967)\n505 Dial M for Murder (1954)\n506 Rebel Without a Cause (1955)\n507 Streetcar Named Desire, A (1951)\n508 People vs. Larry Flynt, The (1996)\n509 My Left Foot (1989)\n510 Magnificent Seven, The (1954)\n511 Lawrence of Arabia (1962)\n512 Wings of Desire (1987)\n513 Third Man, The (1949)\n514 Annie Hall (1977)\n515 Boot, Das (1981)\n516 Local Hero (1983)\n517 Manhattan (1979)\n518 Miller's Crossing (1990)\n519 Treasure of the Sierra Madre, The (1948)\n520 Great Escape, The (1963)\n521 Deer Hunter, The (1978)\n522 Down by Law (1986)\n523 Cool Hand Luke (1967)\n524 Great Dictator, The (1940)\n525 Big Sleep, The (1946)\n526 Ben-Hur (1959)\n527 Gandhi (1982)\n528 Killing Fields, The (1984)\n529 My Life as a Dog (Mitt liv som hund) (1985)\n530 Man Who Would Be King, The (1975)\n531 Shine (1996)\n532 Kama Sutra: A Tale of Love (1996)\n533 Daytrippers, The (1996)\n534 Traveller (1997)\n535 Addicted to Love (1997)\n536 Ponette (1996)\n537 My Own Private Idaho (1991)\n538 Anastasia (1997)\n539 Mouse Hunt (1997)\n540 Money Train (1995)\n541 Mortal Kombat (1995)\n542 Pocahontas (1995)\n543 Misrables, Les (1995)\n544 Things to Do in Denver when You're Dead (1995)\n545 Vampire in Brooklyn (1995)\n546 Broken Arrow (1996)\n547 Young Poisoner's Handbook, The (1995)\n548 NeverEnding Story III, The (1994)\n549 Rob Roy (1995)\n550 Die Hard: With a Vengeance (1995)\n551 Lord of Illusions (1995)\n552 Species (1995)\n553 Walk in the Clouds, A (1995)\n554 Waterworld (1995)\n555 White Man's Burden (1995)\n556 Wild Bill (1995)\n557 Farinelli: il castrato (1994)\n558 Heavenly Creatures (1994)\n559 Interview with the Vampire (1994)\n560 Kid in King Arthur's Court, A (1995)\n561 Mary Shelley's Frankenstein (1994)\n562 Quick and the Dead, The (1995)\n563 Stephen King's The Langoliers (1995)\n564 Tales from the Hood (1995)\n565 Village of the Damned (1995)\n566 Clear and Present Danger (1994)\n567 Wes Craven's New Nightmare (1994)\n568 Speed (1994)\n569 Wolf (1994)\n570 Wyatt Earp (1994)\n571 Another Stakeout (1993)\n572 Blown Away (1994)\n573 Body Snatchers (1993)\n574 Boxing Helena (1993)\n575 City Slickers II: The Legend of Curly's Gold (1994)\n576 Cliffhanger (1993)\n577 Coneheads (1993)\n578 Demolition Man (1993)\n579 Fatal Instinct (1993)\n580 Englishman Who Went Up a Hill, But Came Down a Mountain, The (1995)\n581 Kalifornia (1993)\n582 Piano, The (1993)\n583 Romeo Is Bleeding (1993)\n584 Secret Garden, The (1993)\n585 Son in Law (1993)\n586 Terminal Velocity (1994)\n587 Hour of the Pig, The (1993)\n588 Beauty and the Beast (1991)\n589 Wild Bunch, The (1969)\n590 Hellraiser: Bloodline (1996)\n591 Primal Fear (1996)\n592 True Crime (1995)\n593 Stalingrad (1993)\n594 Heavy (1995)\n595 Fan, The (1996)\n596 Hunchback of Notre Dame, The (1996)\n597 Eraser (1996)\n598 Big Squeeze, The (1996)\n599 Police Story 4: Project S (Chao ji ji hua) (1993)\n600 Daniel Defoe's Robinson Crusoe (1996)\n601 For Whom the Bell Tolls (1943)\n602 American in Paris, An (1951)\n603 Rear Window (1954)\n604 It Happened One Night (1934)\n605 Meet Me in St. Louis (1944)\n606 All About Eve (1950)\n607 Rebecca (1940)\n608 Spellbound (1945)\n609 Father of the Bride (1950)\n610 Gigi (1958)\n611 Laura (1944)\n612 Lost Horizon (1937)\n613 My Man Godfrey (1936)\n614 Giant (1956)\n615 39 Steps, The (1935)\n616 Night of the Living Dead (1968)\n617 Blue Angel, The (Blaue Engel, Der) (1930)\n618 Picnic (1955)\n619 Extreme Measures (1996)\n620 Chamber, The (1996)\n621 Davy Crockett, King of the Wild Frontier (1955)\n622 Swiss Family Robinson (1960)\n623 Angels in the Outfield (1994)\n624 Three Caballeros, The (1945)\n625 Sword in the Stone, The (1963)\n626 So Dear to My Heart (1949)\n627 Robin Hood: Prince of Thieves (1991)\n628 Sleepers (1996)\n629 Victor/Victoria (1982)\n630 Great Race, The (1965)\n631 Crying Game, The (1992)\n632 Sophie's Choice (1982)\n633 Christmas Carol, A (1938)\n634 Microcosmos: Le peuple de l'herbe (1996)\n635 Fog, The (1980)\n636 Escape from New York (1981)\n637 Howling, The (1981)\n638 Return of Martin Guerre, The (Retour de Martin Guerre, Le) (1982)\n639 Tin Drum, The (Blechtrommel, Die) (1979)\n640 Cook the Thief His Wife & Her Lover, The (1989)\n641 Paths of Glory (1957)\n642 Grifters, The (1990)\n643 The Innocent (1994)\n644 Thin Blue Line, The (1988)\n645 Paris Is Burning (1990)\n646 Once Upon a Time in the West (1969)\n647 Ran (1985)\n648 Quiet Man, The (1952)\n649 Once Upon a Time in America (1984)\n650 Seventh Seal, The (Sjunde inseglet, Det) (1957)\n651 Glory (1989)\n652 Rosencrantz and Guildenstern Are Dead (1990)\n653 Touch of Evil (1958)\n654 Chinatown (1974)\n655 Stand by Me (1986)\n656 M (1931)\n657 Manchurian Candidate, The (1962)\n658 Pump Up the Volume (1990)\n659 Arsenic and Old Lace (1944)\n660 Fried Green Tomatoes (1991)\n661 High Noon (1952)\n662 Somewhere in Time (1980)\n663 Being There (1979)\n664 Paris, Texas (1984)\n665 Alien 3 (1992)\n666 Blood For Dracula (Andy Warhol's Dracula) (1974)\n667 Audrey Rose (1977)\n668 Blood Beach (1981)\n669 Body Parts (1991)\n670 Body Snatchers (1993)\n671 Bride of Frankenstein (1935)\n672 Candyman (1992)\n673 Cape Fear (1962)\n674 Cat People (1982)\n675 Nosferatu (Nosferatu, eine Symphonie des Grauens) (1922)\n676 Crucible, The (1996)\n677 Fire on the Mountain (1996)\n678 Volcano (1997)\n679 Conan the Barbarian (1981)\n680 Kull the Conqueror (1997)\n681 Wishmaster (1997)\n682 I Know What You Did Last Summer (1997)\n683 Rocket Man (1997)\n684 In the Line of Fire (1993)\n685 Executive Decision (1996)\n686 Perfect World, A (1993)\n687 McHale's Navy (1997)\n688 Leave It to Beaver (1997)\n689 Jackal, The (1997)\n690 Seven Years in Tibet (1997)\n691 Dark City (1998)\n692 American President, The (1995)\n693 Casino (1995)\n694 Persuasion (1995)\n695 Kicking and Screaming (1995)\n696 City Hall (1996)\n697 Basketball Diaries, The (1995)\n698 Browning Version, The (1994)\n699 Little Women (1994)\n700 Miami Rhapsody (1995)\n701 Wonderful, Horrible Life of Leni Riefenstahl, The (1993)\n702 Barcelona (1994)\n703 Widows' Peak (1994)\n704 House of the Spirits, The (1993)\n705 Singin' in the Rain (1952)\n706 Bad Moon (1996)\n707 Enchanted April (1991)\n708 Sex, Lies, and Videotape (1989)\n709 Strictly Ballroom (1992)\n710 Better Off Dead... (1985)\n711 Substance of Fire, The (1996)\n712 Tin Men (1987)\n713 Othello (1995)\n714 Carrington (1995)\n715 To Die For (1995)\n716 Home for the Holidays (1995)\n717 Juror, The (1996)\n718 In the Bleak Midwinter (1995)\n719 Canadian Bacon (1994)\n720 First Knight (1995)\n721 Mallrats (1995)\n722 Nine Months (1995)\n723 Boys on the Side (1995)\n724 Circle of Friends (1995)\n725 Exit to Eden (1994)\n726 Fluke (1995)\n727 Immortal Beloved (1994)\n728 Junior (1994)\n729 Nell (1994)\n730 Queen Margot (Reine Margot, La) (1994)\n731 Corrina, Corrina (1994)\n732 Dave (1993)\n733 Go Fish (1994)\n734 Made in America (1993)\n735 Philadelphia (1993)\n736 Shadowlands (1993)\n737 Sirens (1994)\n738 Threesome (1994)\n739 Pretty Woman (1990)\n740 Jane Eyre (1996)\n741 Last Supper, The (1995)\n742 Ransom (1996)\n743 Crow: City of Angels, The (1996)\n744 Michael Collins (1996)\n745 Ruling Class, The (1972)\n746 Real Genius (1985)\n747 Benny & Joon (1993)\n748 Saint, The (1997)\n749 MatchMaker, The (1997)\n750 Amistad (1997)\n751 Tomorrow Never Dies (1997)\n752 Replacement Killers, The (1998)\n753 Burnt By the Sun (1994)\n754 Red Corner (1997)\n755 Jumanji (1995)\n756 Father of the Bride Part II (1995)\n757 Across the Sea of Time (1995)\n758 Lawnmower Man 2: Beyond Cyberspace (1996)\n759 Fair Game (1995)\n760 Screamers (1995)\n761 Nick of Time (1995)\n762 Beautiful Girls (1996)\n763 Happy Gilmore (1996)\n764 If Lucy Fell (1996)\n765 Boomerang (1992)\n766 Man of the Year (1995)\n767 Addiction, The (1995)\n768 Casper (1995)\n769 Congo (1995)\n770 Devil in a Blue Dress (1995)\n771 Johnny Mnemonic (1995)\n772 Kids (1995)\n773 Mute Witness (1994)\n774 Prophecy, The (1995)\n775 Something to Talk About (1995)\n776 Three Wishes (1995)\n777 Castle Freak (1995)\n778 Don Juan DeMarco (1995)\n779 Drop Zone (1994)\n780 Dumb & Dumber (1994)\n781 French Kiss (1995)\n782 Little Odessa (1994)\n783 Milk Money (1994)\n784 Beyond Bedlam (1993)\n785 Only You (1994)\n786 Perez Family, The (1995)\n787 Roommates (1995)\n788 Relative Fear (1994)\n789 Swimming with Sharks (1995)\n790 Tommy Boy (1995)\n791 Baby-Sitters Club, The (1995)\n792 Bullets Over Broadway (1994)\n793 Crooklyn (1994)\n794 It Could Happen to You (1994)\n795 Richie Rich (1994)\n796 Speechless (1994)\n797 Timecop (1994)\n798 Bad Company (1995)\n799 Boys Life (1995)\n800 In the Mouth of Madness (1995)\n801 Air Up There, The (1994)\n802 Hard Target (1993)\n803 Heaven & Earth (1993)\n804 Jimmy Hollywood (1994)\n805 Manhattan Murder Mystery (1993)\n806 Menace II Society (1993)\n807 Poetic Justice (1993)\n808 Program, The (1993)\n809 Rising Sun (1993)\n810 Shadow, The (1994)\n811 Thirty-Two Short Films About Glenn Gould (1993)\n812 Andre (1994)\n813 Celluloid Closet, The (1995)\n814 Great Day in Harlem, A (1994)\n815 One Fine Day (1996)\n816 Candyman: Farewell to the Flesh (1995)\n817 Frisk (1995)\n818 Girl 6 (1996)\n819 Eddie (1996)\n820 Space Jam (1996)\n821 Mrs. Winterbourne (1996)\n822 Faces (1968)\n823 Mulholland Falls (1996)\n824 Great White Hype, The (1996)\n825 Arrival, The (1996)\n826 Phantom, The (1996)\n827 Daylight (1996)\n828 Alaska (1996)\n829 Fled (1996)\n830 Power 98 (1995)\n831 Escape from L.A. (1996)\n832 Bogus (1996)\n833 Bulletproof (1996)\n834 Halloween: The Curse of Michael Myers (1995)\n835 Gay Divorcee, The (1934)\n836 Ninotchka (1939)\n837 Meet John Doe (1941)\n838 In the Line of Duty 2 (1987)\n839 Loch Ness (1995)\n840 Last Man Standing (1996)\n841 Glimmer Man, The (1996)\n842 Pollyanna (1960)\n843 Shaggy Dog, The (1959)\n844 Freeway (1996)\n845 That Thing You Do! (1996)\n846 To Gillian on Her 37th Birthday (1996)\n847 Looking for Richard (1996)\n848 Murder, My Sweet (1944)\n849 Days of Thunder (1990)\n850 Perfect Candidate, A (1996)\n851 Two or Three Things I Know About Her (1966)\n852 Bloody Child, The (1996)\n853 Braindead (1992)\n854 Bad Taste (1987)\n855 Diva (1981)\n856 Night on Earth (1991)\n857 Paris Was a Woman (1995)\n858 Amityville: Dollhouse (1996)\n859 April Fool's Day (1986)\n860 Believers, The (1987)\n861 Nosferatu a Venezia (1986)\n862 Jingle All the Way (1996)\n863 Garden of Finzi-Contini, The (Giardino dei Finzi-Contini, Il) (1970)\n864 My Fellow Americans (1996)\n865 Ice Storm, The (1997)\n866 Michael (1996)\n867 Whole Wide World, The (1996)\n868 Hearts and Minds (1996)\n869 Fools Rush In (1997)\n870 Touch (1997)\n871 Vegas Vacation (1997)\n872 Love Jones (1997)\n873 Picture Perfect (1997)\n874 Career Girls (1997)\n875 She's So Lovely (1997)\n876 Money Talks (1997)\n877 Excess Baggage (1997)\n878 That Darn Cat! (1997)\n879 Peacemaker, The (1997)\n880 Soul Food (1997)\n881 Money Talks (1997)\n882 Washington Square (1997)\n883 Telling Lies in America (1997)\n884 Year of the Horse (1997)\n885 Phantoms (1998)\n886 Life Less Ordinary, A (1997)\n887 Eve's Bayou (1997)\n888 One Night Stand (1997)\n889 Tango Lesson, The (1997)\n890 Mortal Kombat: Annihilation (1997)\n891 Bent (1997)\n892 Flubber (1997)\n893 For Richer or Poorer (1997)\n894 Home Alone 3 (1997)\n895 Scream 2 (1997)\n896 Sweet Hereafter, The (1997)\n897 Time Tracers (1995)\n898 Postman, The (1997)\n899 Winter Guest, The (1997)\n900 Kundun (1997)\n901 Mr. Magoo (1997)\n902 Big Lebowski, The (1998)\n903 Afterglow (1997)\n904 Ma vie en rose (My Life in Pink) (1997)\n905 Great Expectations (1998)\n906 Oscar & Lucinda (1997)\n907 Vermin (1998)\n908 Half Baked (1998)\n909 Dangerous Beauty (1998)\n910 Nil By Mouth (1997)\n911 Twilight (1998)\n912 U.S. Marshalls (1998)\n913 Love and Death on Long Island (1997)\n914 Wild Things (1998)\n915 Primary Colors (1998)\n916 Lost in Space (1998)\n917 Mercury Rising (1998)\n918 City of Angels (1998)\n919 City of Lost Children, The (1995)\n920 Two Bits (1995)\n921 Farewell My Concubine (1993)\n922 Dead Man (1995)\n923 Raise the Red Lantern (1991)\n924 White Squall (1996)\n925 Unforgettable (1996)\n926 Down Periscope (1996)\n927 Flower of My Secret, The (Flor de mi secreto, La) (1995)\n928 Craft, The (1996)\n929 Harriet the Spy (1996)\n930 Chain Reaction (1996)\n931 Island of Dr. Moreau, The (1996)\n932 First Kid (1996)\n933 Funeral, The (1996)\n934 Preacher's Wife, The (1996)\n935 Paradise Road (1997)\n936 Brassed Off (1996)\n937 Thousand Acres, A (1997)\n938 Smile Like Yours, A (1997)\n939 Murder in the First (1995)\n940 Airheads (1994)\n941 With Honors (1994)\n942 What's Love Got to Do with It (1993)\n943 Killing Zoe (1994)\n944 Renaissance Man (1994)\n945 Charade (1963)\n946 Fox and the Hound, The (1981)\n947 Big Blue, The (Grand bleu, Le) (1988)\n948 Booty Call (1997)\n949 How to Make an American Quilt (1995)\n950 Georgia (1995)\n951 Indian in the Cupboard, The (1995)\n952 Blue in the Face (1995)\n953 Unstrung Heroes (1995)\n954 Unzipped (1995)\n955 Before Sunrise (1995)\n956 Nobody's Fool (1994)\n957 Pushing Hands (1992)\n958 To Live (Huozhe) (1994)\n959 Dazed and Confused (1993)\n960 Naked (1993)\n961 Orlando (1993)\n962 Ruby in Paradise (1993)\n963 Some Folks Call It a Sling Blade (1993)\n964 Month by the Lake, A (1995)\n965 Funny Face (1957)\n966 Affair to Remember, An (1957)\n967 Little Lord Fauntleroy (1936)\n968 Inspector General, The (1949)\n969 Winnie the Pooh and the Blustery Day (1968)\n970 Hear My Song (1991)\n971 Mediterraneo (1991)\n972 Passion Fish (1992)\n973 Grateful Dead (1995)\n974 Eye for an Eye (1996)\n975 Fear (1996)\n976 Solo (1996)\n977 Substitute, The (1996)\n978 Heaven's Prisoners (1996)\n979 Trigger Effect, The (1996)\n980 Mother Night (1996)\n981 Dangerous Ground (1997)\n982 Maximum Risk (1996)\n983 Rich Man's Wife, The (1996)\n984 Shadow Conspiracy (1997)\n985 Blood & Wine (1997)\n986 Turbulence (1997)\n987 Underworld (1997)\n988 Beautician and the Beast, The (1997)\n989 Cats Don't Dance (1997)\n990 Anna Karenina (1997)\n991 Keys to Tulsa (1997)\n992 Head Above Water (1996)\n993 Hercules (1997)\n994 Last Time I Committed Suicide, The (1997)\n995 Kiss Me, Guido (1997)\n996 Big Green, The (1995)\n997 Stuart Saves His Family (1995)\n998 Cabin Boy (1994)\n999 Clean Slate (1994)\n1000 Lightning Jack (1994)\n1001 Stupids, The (1996)\n1002 Pest, The (1997)\n1003 That Darn Cat! (1997)\n1004 Geronimo: An American Legend (1993)\n1005 Double vie de Vronique, La (Double Life of Veronique, The) (1991)\n1006 Until the End of the World (Bis ans Ende der Welt) (1991)\n1007 Waiting for Guffman (1996)\n1008 I Shot Andy Warhol (1996)\n1009 Stealing Beauty (1996)\n1010 Basquiat (1996)\n1011 2 Days in the Valley (1996)\n1012 Private Parts (1997)\n1013 Anaconda (1997)\n1014 Romy and Michele's High School Reunion (1997)\n1015 Shiloh (1997)\n1016 Con Air (1997)\n1017 Trees Lounge (1996)\n1018 Tie Me Up! Tie Me Down! (1990)\n1019 Die xue shuang xiong (Killer, The) (1989)\n1020 Gaslight (1944)\n1021 8 1/2 (1963)\n1022 Fast, Cheap & Out of Control (1997)\n1023 Fathers' Day (1997)\n1024 Mrs. Dalloway (1997)\n1025 Fire Down Below (1997)\n1026 Lay of the Land, The (1997)\n1027 Shooter, The (1995)\n1028 Grumpier Old Men (1995)\n1029 Jury Duty (1995)\n1030 Beverly Hillbillies, The (1993)\n1031 Lassie (1994)\n1032 Little Big League (1994)\n1033 Homeward Bound II: Lost in San Francisco (1996)\n1034 Quest, The (1996)\n1035 Cool Runnings (1993)\n1036 Drop Dead Fred (1991)\n1037 Grease 2 (1982)\n1038 Switchback (1997)\n1039 Hamlet (1996)\n1040 Two if by Sea (1996)\n1041 Forget Paris (1995)\n1042 Just Cause (1995)\n1043 Rent-a-Kid (1995)\n1044 Paper, The (1994)\n1045 Fearless (1993)\n1046 Malice (1993)\n1047 Multiplicity (1996)\n1048 She's the One (1996)\n1049 House Arrest (1996)\n1050 Ghost and Mrs. Muir, The (1947)\n1051 Associate, The (1996)\n1052 Dracula: Dead and Loving It (1995)\n1053 Now and Then (1995)\n1054 Mr. Wrong (1996)\n1055 Simple Twist of Fate, A (1994)\n1056 Cronos (1992)\n1057 Pallbearer, The (1996)\n1058 War, The (1994)\n1059 Don't Be a Menace to South Central While Drinking Your Juice in the Hood (1996)\n1060 Adventures of Pinocchio, The (1996)\n1061 Evening Star, The (1996)\n1062 Four Days in September (1997)\n1063 Little Princess, A (1995)\n1064 Crossfire (1947)\n1065 Koyaanisqatsi (1983)\n1066 Balto (1995)\n1067 Bottle Rocket (1996)\n1068 Star Maker, The (Uomo delle stelle, L') (1995)\n1069 Amateur (1994)\n1070 Living in Oblivion (1995)\n1071 Party Girl (1995)\n1072 Pyromaniac's Love Story, A (1995)\n1073 Shallow Grave (1994)\n1074 Reality Bites (1994)\n1075 Man of No Importance, A (1994)\n1076 Pagemaster, The (1994)\n1077 Love and a .45 (1994)\n1078 Oliver & Company (1988)\n1079 Joe's Apartment (1996)\n1080 Celestial Clockwork (1994)\n1081 Curdled (1996)\n1082 Female Perversions (1996)\n1083 Albino Alligator (1996)\n1084 Anne Frank Remembered (1995)\n1085 Carried Away (1996)\n1086 It's My Party (1995)\n1087 Bloodsport 2 (1995)\n1088 Double Team (1997)\n1089 Speed 2: Cruise Control (1997)\n1090 Sliver (1993)\n1091 Pete's Dragon (1977)\n1092 Dear God (1996)\n1093 Live Nude Girls (1995)\n1094 Thin Line Between Love and Hate, A (1996)\n1095 High School High (1996)\n1096 Commandments (1997)\n1097 Hate (Haine, La) (1995)\n1098 Flirting With Disaster (1996)\n1099 Red Firecracker, Green Firecracker (1994)\n1100 What Happened Was... (1994)\n1101 Six Degrees of Separation (1993)\n1102 Two Much (1996)\n1103 Trust (1990)\n1104 C'est arriv prs de chez vous (1992)\n1105 Firestorm (1998)\n1106 Newton Boys, The (1998)\n1107 Beyond Rangoon (1995)\n1108 Feast of July (1995)\n1109 Death and the Maiden (1994)\n1110 Tank Girl (1995)\n1111 Double Happiness (1994)\n1112 Cobb (1994)\n1113 Mrs. Parker and the Vicious Circle (1994)\n1114 Faithful (1996)\n1115 Twelfth Night (1996)\n1116 Mark of Zorro, The (1940)\n1117 Surviving Picasso (1996)\n1118 Up in Smoke (1978)\n1119 Some Kind of Wonderful (1987)\n1120 I'm Not Rappaport (1996)\n1121 Umbrellas of Cherbourg, The (Parapluies de Cherbourg, Les) (1964)\n1122 They Made Me a Criminal (1939)\n1123 Last Time I Saw Paris, The (1954)\n1124 Farewell to Arms, A (1932)\n1125 Innocents, The (1961)\n1126 Old Man and the Sea, The (1958)\n1127 Truman Show, The (1998)\n1128 Heidi Fleiss: Hollywood Madam (1995) \n1129 Chungking Express (1994)\n1130 Jupiter's Wife (1994)\n1131 Safe (1995)\n1132 Feeling Minnesota (1996)\n1133 Escape to Witch Mountain (1975)\n1134 Get on the Bus (1996)\n1135 Doors, The (1991)\n1136 Ghosts of Mississippi (1996)\n1137 Beautiful Thing (1996)\n1138 Best Men (1997)\n1139 Hackers (1995)\n1140 Road to Wellville, The (1994)\n1141 War Room, The (1993)\n1142 When We Were Kings (1996)\n1143 Hard Eight (1996)\n1144 Quiet Room, The (1996)\n1145 Blue Chips (1994)\n1146 Calendar Girl (1993)\n1147 My Family (1995)\n1148 Tom & Viv (1994)\n1149 Walkabout (1971)\n1150 Last Dance (1996)\n1151 Original Gangstas (1996)\n1152 In Love and War (1996)\n1153 Backbeat (1993)\n1154 Alphaville (1965)\n1155 Rendezvous in Paris (Rendez-vous de Paris, Les) (1995)\n1156 Cyclo (1995)\n1157 Relic, The (1997)\n1158 Fille seule, La (A Single Girl) (1995)\n1159 Stalker (1979)\n1160 Love! Valour! Compassion! (1997)\n1161 Palookaville (1996)\n1162 Phat Beach (1996)\n1163 Portrait of a Lady, The (1996)\n1164 Zeus and Roxanne (1997)\n1165 Big Bully (1996)\n1166 Love & Human Remains (1993)\n1167 Sum of Us, The (1994)\n1168 Little Buddha (1993)\n1169 Fresh (1994)\n1170 Spanking the Monkey (1994)\n1171 Wild Reeds (1994)\n1172 Women, The (1939)\n1173 Bliss (1997)\n1174 Caught (1996)\n1175 Hugo Pool (1997)\n1176 Welcome To Sarajevo (1997)\n1177 Dunston Checks In (1996)\n1178 Major Payne (1994)\n1179 Man of the House (1995)\n1180 I Love Trouble (1994)\n1181 Low Down Dirty Shame, A (1994)\n1182 Cops and Robbersons (1994)\n1183 Cowboy Way, The (1994)\n1184 Endless Summer 2, The (1994)\n1185 In the Army Now (1994)\n1186 Inkwell, The (1994)\n1187 Switchblade Sisters (1975)\n1188 Young Guns II (1990)\n1189 Prefontaine (1997)\n1190 That Old Feeling (1997)\n1191 Letter From Death Row, A (1998)\n1192 Boys of St. Vincent, The (1993)\n1193 Before the Rain (Pred dozhdot) (1994)\n1194 Once Were Warriors (1994)\n1195 Strawberry and Chocolate (Fresa y chocolate) (1993)\n1196 Savage Nights (Nuits fauves, Les) (1992)\n1197 Family Thing, A (1996)\n1198 Purple Noon (1960)\n1199 Cemetery Man (Dellamorte Dellamore) (1994)\n1200 Kim (1950)\n1201 Marlene Dietrich: Shadow and Light (1996) \n1202 Maybe, Maybe Not (Bewegte Mann, Der) (1994)\n1203 Top Hat (1935)\n1204 To Be or Not to Be (1942)\n1205 Secret Agent, The (1996)\n1206 Amos & Andrew (1993)\n1207 Jade (1995)\n1208 Kiss of Death (1995)\n1209 Mixed Nuts (1994)\n1210 Virtuosity (1995)\n1211 Blue Sky (1994)\n1212 Flesh and Bone (1993)\n1213 Guilty as Sin (1993)\n1214 In the Realm of the Senses (Ai no corrida) (1976)\n1215 Barb Wire (1996)\n1216 Kissed (1996)\n1217 Assassins (1995)\n1218 Friday (1995)\n1219 Goofy Movie, A (1995)\n1220 Higher Learning (1995)\n1221 When a Man Loves a Woman (1994)\n1222 Judgment Night (1993)\n1223 King of the Hill (1993)\n1224 Scout, The (1994)\n1225 Angus (1995)\n1226 Night Falls on Manhattan (1997)\n1227 Awfully Big Adventure, An (1995)\n1228 Under Siege 2: Dark Territory (1995)\n1229 Poison Ivy II (1995)\n1230 Ready to Wear (Pret-A-Porter) (1994)\n1231 Marked for Death (1990)\n1232 Madonna: Truth or Dare (1991)\n1233 Nnette et Boni (1996)\n1234 Chairman of the Board (1998)\n1235 Big Bang Theory, The (1994)\n1236 Other Voices, Other Rooms (1997)\n1237 Twisted (1996)\n1238 Full Speed (1996)\n1239 Cutthroat Island (1995)\n1240 Ghost in the Shell (Kokaku kidotai) (1995)\n1241 Van, The (1996)\n1242 Old Lady Who Walked in the Sea, The (Vieille qui marchait dans la mer, La) (1991)\n1243 Night Flier (1997)\n1244 Metro (1997)\n1245 Gridlock'd (1997)\n1246 Bushwhacked (1995)\n1247 Bad Girls (1994)\n1248 Blink (1994)\n1249 For Love or Money (1993)\n1250 Best of the Best 3: No Turning Back (1995)\n1251 A Chef in Love (1996)\n1252 Contempt (Mpris, Le) (1963)\n1253 Tie That Binds, The (1995)\n1254 Gone Fishin' (1997)\n1255 Broken English (1996)\n1256 Designated Mourner, The (1997)\n1257 Designated Mourner, The (1997)\n1258 Trial and Error (1997)\n1259 Pie in the Sky (1995)\n1260 Total Eclipse (1995)\n1261 Run of the Country, The (1995)\n1262 Walking and Talking (1996)\n1263 Foxfire (1996)\n1264 Nothing to Lose (1994)\n1265 Star Maps (1997)\n1266 Bread and Chocolate (Pane e cioccolata) (1973)\n1267 Clockers (1995)\n1268 Bitter Moon (1992)\n1269 Love in the Afternoon (1957)\n1270 Life with Mikey (1993)\n1271 North (1994)\n1272 Talking About Sex (1994)\n1273 Color of Night (1994)\n1274 Robocop 3 (1993)\n1275 Killer (Bulletproof Heart) (1994)\n1276 Sunset Park (1996)\n1277 Set It Off (1996)\n1278 Selena (1997)\n1279 Wild America (1997)\n1280 Gang Related (1997)\n1281 Manny & Lo (1996)\n1282 Grass Harp, The (1995)\n1283 Out to Sea (1997)\n1284 Before and After (1996)\n1285 Princess Caraboo (1994)\n1286 Shall We Dance? (1937)\n1287 Ed (1996)\n1288 Denise Calls Up (1995)\n1289 Jack and Sarah (1995)\n1290 Country Life (1994)\n1291 Celtic Pride (1996)\n1292 Simple Wish, A (1997)\n1293 Star Kid (1997)\n1294 Ayn Rand: A Sense of Life (1997)\n1295 Kicked in the Head (1997)\n1296 Indian Summer (1996)\n1297 Love Affair (1994)\n1298 Band Wagon, The (1953)\n1299 Penny Serenade (1941)\n1300 'Til There Was You (1997)\n1301 Stripes (1981)\n1302 Late Bloomers (1996)\n1303 Getaway, The (1994)\n1304 New York Cop (1996)\n1305 National Lampoon's Senior Trip (1995)\n1306 Delta of Venus (1994)\n1307 Carmen Miranda: Bananas Is My Business (1994)\n1308 Babyfever (1994)\n1309 Very Natural Thing, A (1974)\n1310 Walk in the Sun, A (1945)\n1311 Waiting to Exhale (1995)\n1312 Pompatus of Love, The (1996)\n1313 Palmetto (1998)\n1314 Surviving the Game (1994)\n1315 Inventing the Abbotts (1997)\n1316 Horse Whisperer, The (1998)\n1317 Journey of August King, The (1995)\n1318 Catwalk (1995)\n1319 Neon Bible, The (1995)\n1320 Homage (1995)\n1321 Open Season (1996)\n1322 Metisse (Caf au Lait) (1993)\n1323 Wooden Man's Bride, The (Wu Kui) (1994)\n1324 Loaded (1994)\n1325 August (1996)\n1326 Boys (1996)\n1327 Captives (1994)\n1328 Of Love and Shadows (1994)\n1329 Low Life, The (1994)\n1330 An Unforgettable Summer (1994)\n1331 Last Klezmer: Leopold Kozlowski, His Life and Music, The (1995)\n1332 My Life and Times With Antonin Artaud (En compagnie d'Antonin Artaud) (1993)\n1333 Midnight Dancers (Sibak) (1994)\n1334 Somebody to Love (1994)\n1335 American Buffalo (1996)\n1336 Kazaam (1996)\n1337 Larger Than Life (1996)\n1338 Two Deaths (1995)\n1339 Stefano Quantestorie (1993)\n1340 Crude Oasis, The (1995)\n1341 Hedd Wyn (1992)\n1342 Convent, The (Convento, O) (1995)\n1343 Lotto Land (1995)\n1344 Story of Xinghua, The (1993)\n1345 Day the Sun Turned Cold, The (Tianguo niezi) (1994)\n1346 Dingo (1992)\n1347 Ballad of Narayama, The (Narayama Bushiko) (1958)\n1348 Every Other Weekend (1990)\n1349 Mille bolle blu (1993)\n1350 Crows and Sparrows (1949)\n1351 Lover's Knot (1996)\n1352 Shadow of Angels (Schatten der Engel) (1976)\n1353 1-900 (1994)\n1354 Venice/Venice (1992)\n1355 Infinity (1996)\n1356 Ed's Next Move (1996)\n1357 For the Moment (1994)\n1358 The Deadly Cure (1996)\n1359 Boys in Venice (1996)\n1360 Sexual Life of the Belgians, The (1994)\n1361 Search for One-eye Jimmy, The (1996)\n1362 American Strays (1996)\n1363 Leopard Son, The (1996)\n1364 Bird of Prey (1996)\n1365 Johnny 100 Pesos (1993)\n1366 JLG/JLG - autoportrait de dcembre (1994)\n1367 Faust (1994)\n1368 Mina Tannenbaum (1994)\n1369 Forbidden Christ, The (Cristo proibito, Il) (1950)\n1370 I Can't Sleep (J'ai pas sommeil) (1994)\n1371 Machine, The (1994)\n1372 Stranger, The (1994)\n1373 Good Morning (1971)\n1374 Falling in Love Again (1980)\n1375 Cement Garden, The (1993)\n1376 Meet Wally Sparks (1997)\n1377 Hotel de Love (1996)\n1378 Rhyme & Reason (1997)\n1379 Love and Other Catastrophes (1996)\n1380 Hollow Reed (1996)\n1381 Losing Chase (1996)\n1382 Bonheur, Le (1965)\n1383 Second Jungle Book: Mowgli & Baloo, The (1997)\n1384 Squeeze (1996)\n1385 Roseanna's Grave (For Roseanna) (1997)\n1386 Tetsuo II: Body Hammer (1992)\n1387 Fall (1997)\n1388 Gabbeh (1996)\n1389 Mondo (1996)\n1390 Innocent Sleep, The (1995)\n1391 For Ever Mozart (1996)\n1392 Locusts, The (1997)\n1393 Stag (1997)\n1394 Swept from the Sea (1997)\n1395 Hurricane Streets (1998)\n1396 Stonewall (1995)\n1397 Of Human Bondage (1934)\n1398 Anna (1996)\n1399 Stranger in the House (1997)\n1400 Picture Bride (1995)\n1401 M. Butterfly (1993)\n1402 Ciao, Professore! (1993)\n1403 Caro Diario (Dear Diary) (1994)\n1404 Withnail and I (1987)\n1405 Boy's Life 2 (1997)\n1406 When Night Is Falling (1995)\n1407 Specialist, The (1994)\n1408 Gordy (1995)\n1409 Swan Princess, The (1994)\n1410 Harlem (1993)\n1411 Barbarella (1968)\n1412 Land Before Time III: The Time of the Great Giving (1995) (V)\n1413 Street Fighter (1994)\n1414 Coldblooded (1995)\n1415 Next Karate Kid, The (1994)\n1416 No Escape (1994)\n1417 Turning, The (1992)\n1418 Joy Luck Club, The (1993)\n1419 Highlander III: The Sorcerer (1994)\n1420 Gilligan's Island: The Movie (1998)\n1421 My Crazy Life (Mi vida loca) (1993)\n1422 Suture (1993)\n1423 Walking Dead, The (1995)\n1424 I Like It Like That (1994)\n1425 I'll Do Anything (1994)\n1426 Grace of My Heart (1996)\n1427 Drunks (1995)\n1428 SubUrbia (1997)\n1429 Sliding Doors (1998)\n1430 Ill Gotten Gains (1997)\n1431 Legal Deceit (1997)\n1432 Mighty, The (1998)\n1433 Men of Means (1998)\n1434 Shooting Fish (1997)\n1435 Steal Big, Steal Little (1995)\n1436 Mr. Jones (1993)\n1437 House Party 3 (1994)\n1438 Panther (1995)\n1439 Jason's Lyric (1994)\n1440 Above the Rim (1994)\n1441 Moonlight and Valentino (1995)\n1442 Scarlet Letter, The (1995)\n1443 8 Seconds (1994)\n1444 That Darn Cat! (1965)\n1445 Ladybird Ladybird (1994)\n1446 Bye Bye, Love (1995)\n1447 Century (1993)\n1448 My Favorite Season (1993)\n1449 Pather Panchali (1955)\n1450 Golden Earrings (1947)\n1451 Foreign Correspondent (1940)\n1452 Lady of Burlesque (1943)\n1453 Angel on My Shoulder (1946)\n1454 Angel and the Badman (1947)\n1455 Outlaw, The (1943)\n1456 Beat the Devil (1954)\n1457 Love Is All There Is (1996)\n1458 Damsel in Distress, A (1937)\n1459 Madame Butterfly (1995)\n1460 Sleepover (1995)\n1461 Here Comes Cookie (1935)\n1462 Thieves (Voleurs, Les) (1996)\n1463 Boys, Les (1997)\n1464 Stars Fell on Henrietta, The (1995)\n1465 Last Summer in the Hamptons (1995)\n1466 Margaret's Museum (1995)\n1467 Saint of Fort Washington, The (1993)\n1468 Cure, The (1995)\n1469 Tom and Huck (1995)\n1470 Gumby: The Movie (1995)\n1471 Hideaway (1995)\n1472 Visitors, The (Visiteurs, Les) (1993)\n1473 Little Princess, The (1939)\n1474 Nina Takes a Lover (1994)\n1475 Bhaji on the Beach (1993)\n1476 Raw Deal (1948)\n1477 Nightwatch (1997)\n1478 Dead Presidents (1995)\n1479 Reckless (1995)\n1480 Herbie Rides Again (1974)\n1481 S.F.W. (1994)\n1482 Gate of Heavenly Peace, The (1995)\n1483 Man in the Iron Mask, The (1998)\n1484 Jerky Boys, The (1994)\n1485 Colonel Chabert, Le (1994)\n1486 Girl in the Cadillac (1995)\n1487 Even Cowgirls Get the Blues (1993)\n1488 Germinal (1993)\n1489 Chasers (1994)\n1490 Fausto (1993)\n1491 Tough and Deadly (1995)\n1492 Window to Paris (1994)\n1493 Modern Affair, A (1995)\n1494 Mostro, Il (1994)\n1495 Flirt (1995)\n1496 Carpool (1996)\n1497 Line King: Al Hirschfeld, The (1996)\n1498 Farmer & Chase (1995)\n1499 Grosse Fatigue (1994)\n1500 Santa with Muscles (1996)\n1501 Prisoner of the Mountains (Kavkazsky Plennik) (1996)\n1502 Naked in New York (1994)\n1503 Gold Diggers: The Secret of Bear Mountain (1995)\n1504 Bewegte Mann, Der (1994)\n1505 Killer: A Journal of Murder (1995)\n1506 Nelly & Monsieur Arnaud (1995)\n1507 Three Lives and Only One Death (1996)\n1508 Babysitter, The (1995)\n1509 Getting Even with Dad (1994)\n1510 Mad Dog Time (1996)\n1511 Children of the Revolution (1996)\n1512 World of Apu, The (Apur Sansar) (1959)\n1513 Sprung (1997)\n1514 Dream With the Fishes (1997)\n1515 Wings of Courage (1995)\n1516 Wedding Gift, The (1994)\n1517 Race the Sun (1996)\n1518 Losing Isaiah (1995)\n1519 New Jersey Drive (1995)\n1520 Fear, The (1995)\n1521 Mr. Wonderful (1993)\n1522 Trial by Jury (1994)\n1523 Good Man in Africa, A (1994)\n1524 Kaspar Hauser (1993)\n1525 Object of My Affection, The (1998)\n1526 Witness (1985)\n1527 Senseless (1998)\n1528 Nowhere (1997)\n1529 Underground (1995)\n1530 Jefferson in Paris (1995)\n1531 Far From Home: The Adventures of Yellow Dog (1995)\n1532 Foreign Student (1994)\n1533 I Don't Want to Talk About It (De eso no se habla) (1993)\n1534 Twin Town (1997)\n1535 Enfer, L' (1994)\n1536 Aiqing wansui (1994)\n1537 Cosi (1996)\n1538 All Over Me (1997)\n1539 Being Human (1993)\n1540 Amazing Panda Adventure, The (1995)\n1541 Beans of Egypt, Maine, The (1994)\n1542 Scarlet Letter, The (1926)\n1543 Johns (1996)\n1544 It Takes Two (1995)\n1545 Frankie Starlight (1995)\n1546 Shadows (Cienie) (1988)\n1547 Show, The (1995)\n1548 The Courtyard (1995)\n1549 Dream Man (1995)\n1550 Destiny Turns on the Radio (1995)\n1551 Glass Shield, The (1994)\n1552 Hunted, The (1995)\n1553 Underneath, The (1995)\n1554 Safe Passage (1994)\n1555 Secret Adventures of Tom Thumb, The (1993)\n1556 Condition Red (1995)\n1557 Yankee Zulu (1994)\n1558 Aparajito (1956)\n1559 Hostile Intentions (1994)\n1560 Clean Slate (Coup de Torchon) (1981)\n1561 Tigrero: A Film That Was Never Made (1994)\n1562 Eye of Vichy, The (Oeil de Vichy, L') (1993)\n1563 Promise, The (Versprechen, Das) (1994)\n1564 To Cross the Rubicon (1991)\n1565 Daens (1992)\n1566 Man from Down Under, The (1943)\n1567 Careful (1992)\n1568 Vermont Is For Lovers (1992)\n1569 Vie est belle, La (Life is Rosey) (1987)\n1570 Quartier Mozart (1992)\n1571 Touki Bouki (Journey of the Hyena) (1973)\n1572 Wend Kuuni (God's Gift) (1982)\n1573 Spirits of the Dead (Tre passi nel delirio) (1968)\n1574 Pharaoh's Army (1995)\n1575 I, Worst of All (Yo, la peor de todas) (1990)\n1576 Hungarian Fairy Tale, A (1987)\n1577 Death in the Garden (Mort en ce jardin, La) (1956)\n1578 Collectionneuse, La (1967)\n1579 Baton Rouge (1988)\n1580 Liebelei (1933)\n1581 Woman in Question, The (1950)\n1582 T-Men (1947)\n1583 Invitation, The (Zaproszenie) (1986)\n1584 Symphonie pastorale, La (1946)\n1585 American Dream (1990)\n1586 Lashou shentan (1992)\n1587 Terror in a Texas Town (1958)\n1588 Salut cousin! (1996)\n1589 Schizopolis (1996)\n1590 To Have, or Not (1995)\n1591 Duoluo tianshi (1995)\n1592 Magic Hour, The (1998)\n1593 Death in Brunswick (1991)\n1594 Everest (1998)\n1595 Shopping (1994)\n1596 Nemesis 2: Nebula (1995)\n1597 Romper Stomper (1992)\n1598 City of Industry (1997)\n1599 Someone Else's America (1995)\n1600 Guantanamera (1994)\n1601 Office Killer (1997)\n1602 Price Above Rubies, A (1998)\n1603 Angela (1995)\n1604 He Walked by Night (1948)\n1605 Love Serenade (1996)\n1606 Deceiver (1997)\n1607 Hurricane Streets (1998)\n1608 Buddy (1997)\n1609 B*A*P*S (1997)\n1610 Truth or Consequences, N.M. (1997)\n1611 Intimate Relations (1996)\n1612 Leading Man, The (1996)\n1613 Tokyo Fist (1995)\n1614 Reluctant Debutante, The (1958)\n1615 Warriors of Virtue (1997)\n1616 Desert Winds (1995)\n1617 Hugo Pool (1997)\n1618 King of New York (1990)\n1619 All Things Fair (1996)\n1620 Sixth Man, The (1997)\n1621 Butterfly Kiss (1995)\n1622 Paris, France (1993)\n1623 Crmonie, La (1995)\n1624 Hush (1998)\n1625 Nightwatch (1997)\n1626 Nobody Loves Me (Keiner liebt mich) (1994)\n1627 Wife, The (1995)\n1628 Lamerica (1994)\n1629 Nico Icon (1995)\n1630 Silence of the Palace, The (Saimt el Qusur) (1994)\n1631 Slingshot, The (1993)\n1632 Land and Freedom (Tierra y libertad) (1995)\n1633  kldum klaka (Cold Fever) (1994)\n1634 Etz Hadomim Tafus (Under the Domin Tree) (1994)\n1635 Two Friends (1986) \n1636 Brothers in Trouble (1995)\n1637 Girls Town (1996)\n1638 Normal Life (1996)\n1639 Bitter Sugar (Azucar Amargo) (1996)\n1640 Eighth Day, The (1996)\n1641 Dadetown (1995)\n1642 Some Mother's Son (1996)\n1643 Angel Baby (1995)\n1644 Sudden Manhattan (1996)\n1645 Butcher Boy, The (1998)\n1646 Men With Guns (1997)\n1647 Hana-bi (1997)\n1648 Niagara, Niagara (1997)\n1649 Big One, The (1997)\n1650 Butcher Boy, The (1998)\n1651 Spanish Prisoner, The (1997)\n1652 Temptress Moon (Feng Yue) (1996)\n1653 Entertaining Angels: The Dorothy Day Story (1996)\n1654 Chairman of the Board (1998)\n1655 Favor, The (1994)\n1656 Little City (1998)\n1657 Target (1995)\n1658 Substance of Fire, The (1996)\n1659 Getting Away With Murder (1996)\n1660 Small Faces (1995)\n1661 New Age, The (1994)\n1662 Rough Magic (1995)\n1663 Nothing Personal (1995)\n1664 8 Heads in a Duffel Bag (1997)\n1665 Brother's Kiss, A (1997)\n1666 Ripe (1996)\n1667 Next Step, The (1995)\n1668 Wedding Bell Blues (1996)\n1669 MURDER and murder (1996)\n1670 Tainted (1998)\n1671 Further Gesture, A (1996)\n1672 Kika (1993)\n1673 Mirage (1995)\n1674 Mamma Roma (1962)\n1675 Sunchaser, The (1996)\n1676 War at Home, The (1996)\n1677 Sweet Nothing (1995)\n1678 Mat' i syn (1997)\n1679 B. Monkey (1998)\n1680 Sliding Doors (1998)\n1681 You So Crazy (1994)\n1682 Scream of Stone (Schrei aus Stein) (1991)\n"
  },
  {
    "path": "machine-learning-ex8/ex8/multivariateGaussian.m",
    "content": "function p = multivariateGaussian(X, mu, Sigma2)\n%MULTIVARIATEGAUSSIAN Computes the probability density function of the\n%multivariate gaussian distribution.\n%    p = MULTIVARIATEGAUSSIAN(X, mu, Sigma2) Computes the probability \n%    density function of the examples X under the multivariate gaussian \n%    distribution with parameters mu and Sigma2. If Sigma2 is a matrix, it is\n%    treated as the covariance matrix. If Sigma2 is a vector, it is treated\n%    as the \\sigma^2 values of the variances in each dimension (a diagonal\n%    covariance matrix)\n%\n\nk = length(mu);\n\nif (size(Sigma2, 2) == 1) || (size(Sigma2, 1) == 1)\n    Sigma2 = diag(Sigma2);\nend\n\nX = bsxfun(@minus, X, mu(:)');\np = (2 * pi) ^ (- k / 2) * det(Sigma2) ^ (-0.5) * ...\n    exp(-0.5 * sum(bsxfun(@times, X * pinv(Sigma2), X), 2));\n\nend"
  },
  {
    "path": "machine-learning-ex8/ex8/normalizeRatings.m",
    "content": "function [Ynorm, Ymean] = normalizeRatings(Y, R)\n%NORMALIZERATINGS Preprocess data by subtracting mean rating for every \n%movie (every row)\n%   [Ynorm, Ymean] = NORMALIZERATINGS(Y, R) normalized Y so that each movie\n%   has a rating of 0 on average, and returns the mean rating in Ymean.\n%\n\n[m, n] = size(Y);\nYmean = zeros(m, 1);\nYnorm = zeros(size(Y));\nfor i = 1:m\n    idx = find(R(i, :) == 1);\n    Ymean(i) = mean(Y(i, idx));\n    Ynorm(i, idx) = Y(i, idx) - Ymean(i);\nend\n\nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/selectThreshold.m",
    "content": "function [bestEpsilon bestF1] = selectThreshold(yval, pval)\n%SELECTTHRESHOLD Find the best threshold (epsilon) to use for selecting\n%outliers\n%   [bestEpsilon bestF1] = SELECTTHRESHOLD(yval, pval) finds the best\n%   threshold to use for selecting outliers based on the results from a\n%   validation set (pval) and the ground truth (yval).\n%\n\nbestEpsilon = 0;\nbestF1 = 0;\nF1 = 0;\n\nstepsize = (max(pval) - min(pval)) / 1000;\nfor epsilon = min(pval):stepsize:max(pval)\n    \n    % ====================== YOUR CODE HERE ======================\n    % Instructions: Compute the F1 score of choosing epsilon as the\n    %               threshold and place the value in F1. The code at the\n    %               end of the loop will compare the F1 score for this\n    %               choice of epsilon and set it to be the best epsilon if\n    %               it is better than the current choice of epsilon.\n    %               \n    % Note: You can use predictions = (pval < epsilon) to get a binary vector\n    %       of 0's and 1's of the outlier predictions\n    \n    cvPrediction = pval<epsilon;\n    tp = sum((cvPrediction == 1) & (yval == 1));\n    fp = sum((cvPrediction == 1) & (yval == 0));\n    fn = sum((cvPrediction == 0) & (yval == 1));\n    precision = tp/(tp+fp);\n    recision = tp/(tp+fn);\n    F1 = (2*precision*recision)/(precision+recision);\n\n\n\n\n\n\n\n    % =============================================================\n\n    if F1 > bestF1\n       bestF1 = F1;\n       bestEpsilon = epsilon;\n    end\nend\n\nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/submit.m",
    "content": "function submit()\n  addpath('./lib');\n\n  conf.assignmentSlug = 'anomaly-detection-and-recommender-systems';\n  conf.itemName = 'Anomaly Detection and Recommender Systems';\n  conf.partArrays = { ...\n    { ...\n      '1', ...\n      { 'estimateGaussian.m' }, ...\n      'Estimate Gaussian Parameters', ...\n    }, ...\n    { ...\n      '2', ...\n      { 'selectThreshold.m' }, ...\n      'Select Threshold', ...\n    }, ...\n    { ...\n      '3', ...\n      { 'cofiCostFunc.m' }, ...\n      'Collaborative Filtering Cost', ...\n    }, ...\n    { ...\n      '4', ...\n      { 'cofiCostFunc.m' }, ...\n      'Collaborative Filtering Gradient', ...\n    }, ...\n    { ...\n      '5', ...\n      { 'cofiCostFunc.m' }, ...\n      'Regularized Cost', ...\n    }, ...\n    { ...\n      '6', ...\n      { 'cofiCostFunc.m' }, ...\n      'Regularized Gradient', ...\n    }, ...\n  };\n  conf.output = @output;\n\n  submitWithConfiguration(conf);\nend\n\nfunction out = output(partId, auxstring)\n  % Random Test Cases\n  n_u = 3; n_m = 4; n = 5;\n  X = reshape(sin(1:n_m*n), n_m, n);\n  Theta = reshape(cos(1:n_u*n), n_u, n);\n  Y = reshape(sin(1:2:2*n_m*n_u), n_m, n_u);\n  R = Y > 0.5;\n  pval = [abs(Y(:)) ; 0.001; 1];\n  yval = [R(:) ; 1; 0];\n  params = [X(:); Theta(:)];\n  if partId == '1'\n    [mu sigma2] = estimateGaussian(X);\n    out = sprintf('%0.5f ', [mu(:); sigma2(:)]);\n  elseif partId == '2'\n    [bestEpsilon bestF1] = selectThreshold(yval, pval);\n    out = sprintf('%0.5f ', [bestEpsilon(:); bestF1(:)]);\n  elseif partId == '3'\n    [J] = cofiCostFunc(params, Y, R, n_u, n_m, ...\n                       n, 0);\n    out = sprintf('%0.5f ', J(:));\n  elseif partId == '4'\n    [J, grad] = cofiCostFunc(params, Y, R, n_u, n_m, ...\n                             n, 0);\n    out = sprintf('%0.5f ', grad(:));\n  elseif partId == '5'\n    [J] = cofiCostFunc(params, Y, R, n_u, n_m, ...\n                       n, 1.5);\n    out = sprintf('%0.5f ', J(:));\n  elseif partId == '6'\n    [J, grad] = cofiCostFunc(params, Y, R, n_u, n_m, ...\n                             n, 1.5);\n    out = sprintf('%0.5f ', grad(:));\n  end \nend\n"
  },
  {
    "path": "machine-learning-ex8/ex8/visualizeFit.m",
    "content": "function visualizeFit(X, mu, sigma2)\n%VISUALIZEFIT Visualize the dataset and its estimated distribution.\n%   VISUALIZEFIT(X, p, mu, sigma2) This visualization shows you the \n%   probability density function of the Gaussian distribution. Each example\n%   has a location (x1, x2) that depends on its feature values.\n%\n\n[X1,X2] = meshgrid(0:.5:35); \nZ = multivariateGaussian([X1(:) X2(:)],mu,sigma2);\nZ = reshape(Z,size(X1));\n\nplot(X(:, 1), X(:, 2),'bx');\nhold on;\n% Do not plot if there are infinities\nif (sum(isinf(Z)) == 0)\n    contour(X1, X2, Z, 10.^(-20:3:0)');\nend\nhold off;\n\nend"
  },
  {
    "path": "readme.md",
    "content": "Machine Learning Exercises\n======\nCoursera上斯坦福大学的机器学习编程作业\n\n### 1. [machine-learning-ex1][1]\n - 线性回归\n - 梯度下降\n - 正规方程\n![machine-learning-ex1][1.1]\n\n\n### 2. [machine-learning-ex2][2]\n - 逻辑回归\n - 正则化\n![machine-learning-ex2][2.1]\n\n### 3. [machine-learning-ex3][3]\n - 多分类逻辑回归\n - 神经网络预测(权值weights已知)\n![machine-learning-ex3][3.1]\n\n### 4. [machine-learning-ex4][4]\n - bp神经网络识别手写数字\n![machine-learning-ex4][4.1]\n  \n### 5. [machine-learning-ex5][5]\n - 学习曲线  \n - 验证集   \n![machine-learning-ex5][5.1]\n\n### 6. [machine-learning-ex6][6]\n - 支持向量SVM  \n - 高斯核函数\n - 线性核函数\n - 垃圾邮件分类\n![machine-learning-ex6][6.1]\n  \n### 7. [machine-learning-ex7][7]\n - k-means\n - 降维PCA（主成分分析）\n \n![machine-learning-ex7][7.1]\n\n### 8. [machine-learning-ex8][8]\n - 异常检测（高斯分布）\n - 协同过滤\n \n![machine-learning-ex8][8.1]\n\n  \n  [1]:https://github.com/lawlite19/MachineLearningEx/tree/master/machine-learning-ex1\n  [1.1]: ./images/machine-learning-ex1_01.png \"machine-learning-ex1_01.png\"\n  \n  [2]:https://github.com/lawlite19/MachineLearningEx/tree/master/machine-learning-ex2\n  [2.1]: ./images/machine-learning-ex2_01.png \"machine-learning-ex2_01.png\"\n  \n  [3]:https://github.com/lawlite19/MachineLearningEx/blob/master/machine-learning-ex3\n  [3.1]: ./images/machine-learning-ex3_01.png \"machine-learning-ex3_01.png\"\n\n  [4]:https://github.com/lawlite19/MachineLearningEx/blob/master/machine-learning-ex4\n  [4.1]: ./images/machine-learning-ex4_01.png \"machine-learning-ex4_01.png\"\n  \n  [5]:https://github.com/lawlite19/MachineLearningEx/blob/master/machine-learning-ex5\n  [5.1]: ./images/machine-learning-ex5_01.png \"machine-learning-ex5_01.png\"\n  \n  [6]:https://github.com/lawlite19/MachineLearningEx/blob/master/machine-learning-ex6\n  [6.1]: ./images/machine-learning-ex6_01.png \"machine-learning-ex6_01.png\"\n  \n  [7]:https://github.com/lawlite19/MachineLearningEx/blob/master/machine-learning-ex7\n  [7.1]: ./images/machine-learning-ex7_01.png \"machine-learning-ex7_01.png\"\n  \n  [8]:https://github.com/lawlite19/MachineLearningEx/blob/master/machine-learning-ex8\n  [8.1]: ./images/machine-learning-ex8_01.png \"machine-learning-ex8_01.png\"\n"
  }
]