<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Loren on the Art of MATLAB</title>
	
	<link>http://blogs.mathworks.com/loren</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Wed, 08 Feb 2012 16:09:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/mathworks/loren" /><feedburner:info uri="mathworks/loren" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Using GPUs in MATLAB</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/ouvhwaD6oEY/</link>
		<comments>http://blogs.mathworks.com/loren/2012/02/06/using-gpus-in-matlab/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 16:41:38 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[New Feature]]></category>
		<category><![CDATA[Parallel]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=346</guid>
		<description><![CDATA[Today I&#8217;d like to introduce guest blogger Sarah Wait Zaranek who works for the MATLAB Marketing team. Sarah previously has written about speeding up code from a customer to get acceptable performance. She will be discussing how to use GPUs to accelerate your MATLAB applications. Contents Overview GPU Background Learning About Our GPU A Simple [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p>Today I&#8217;d like to introduce guest blogger <a href="mailto:sarah.zaranek@mathworks.com">Sarah Wait Zaranek</a> who works for the MATLAB Marketing team. Sarah previously has <a href="http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/">written</a> about speeding up code from a customer to get acceptable performance. She will be discussing how to use GPUs to accelerate
         your MATLAB applications.
      </p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">Overview</a></li>
         <li><a href="#2">GPU Background</a></li>
         <li><a href="#5">Learning About Our GPU</a></li>
         <li><a href="#7">A Simple Example using Overloaded Functions</a></li>
         <li><a href="#12">Speedup For Our Simple Example</a></li>
         <li><a href="#14">Understanding and Limiting Your Data Transfer Overhead</a></li>
         <li><a href="#16">Solving the Wave Equation</a></li>
         <li><a href="#20">Code Changes to Run Algorithm on GPU</a></li>
         <li><a href="#21">Code Changes to Transfer Data to the GPU and Back Again</a></li>
         <li><a href="#24">Comparing CPU and GPU Execution Speeds</a></li>
         <li><a href="#27">Other Ways to Use the GPU with MATLAB</a></li>
         <li><a href="#28">Your Thoughts?</a></li>
      </ul>
   </div>
   <h3>Overview<a name="1"></a></h3>
   <p>In this post, we first will introduce the basics of using the GPU with MATLAB and then move onto solving a 2nd-order wave
      equation using this GPU functionality. This blog post is inspired by a recent MATLAB Digest <a href="http://www.mathworks.com/company/newsletters/articles/gpu-programming-in-matlab.html">article</a> on GPU Computing that I coauthored with one of our developers, Jill Reese. Since the original demo was made, the GPU functions
      available in MATLAB have grown. If you compare the code below to the code in the paper- they are slightly different, reflecting
      these new capabilites. Additionally, small changes were made to enable easier explanation of the code in this blog format.
      The GPU functionality shown in this post requires the Parallel Computing Toolbox.
   </p>
   <h3>GPU Background<a name="2"></a></h3>
   <p>Originally used to accelerate graphics rendering, GPUs are now increasingly applied to scientific calculations. Unlike a traditional
      CPU, which includes no more than a handful of cores, a GPU has a massively parallel array of integer and floating-point processors,
      as well as dedicated, high-speed memory. A typical GPU comprises hundreds of these smaller processors.  These processors can
      be used to greatly speed-up particular types of applications.
   </p>
   <p>A good rule of thumb is that your problem may be a good fit for the GPU if it is:</p>
   <div>
      <ul>
         <li>Massively parallel: The computations can be broken down into hundreds or thousands of independent units of work.  You will
            see the best performance when all of the cores are kept busy, exploiting the inherent parallel nature of the GPU. Seemingly
            simple, vectorized MATLAB calculations on arrays with hundreds of thousands of elements often can fit into this category.
         </li>
      </ul>
   </div>
   <div>
      <ul>
         <li>Computationally intensive: The time spent on computation significantly exceeds the time spent on transferring data to and
            from GPU memory. Because a GPU is attached to the host CPU via the PCI Express bus, the memory access is slower than with
            a traditional CPU. This means that your overall computational speedup is limited by the amount of data transfer that occurs
            in your algorithm.
         </li>
      </ul>
   </div>
   <p>Applications that do not satisfy these criteria might actually run more slowly on a GPU than on a CPU.</p>
   <h3>Learning About Our GPU<a name="5"></a></h3>
   <p>With that background, we can now start working with the GPU in MATLAB. Let's start first by querying our GPU to see just what
      we are working with:
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">gpuDevice</pre><pre style="font-style:oblique">ans = 
  parallel.gpu.CUDADevice handle
  Package: parallel.gpu

  Properties:
                      Name: 'Tesla C2050 / C2070'
                     Index: 1
         ComputeCapability: '2.0'
            SupportsDouble: 1
             DriverVersion: 4
        MaxThreadsPerBlock: 1024
          MaxShmemPerBlock: 49152
        MaxThreadBlockSize: [1024 1024 64]
               MaxGridSize: [65535 65535]
                 SIMDWidth: 32
               TotalMemory: 3.1820e+009
                FreeMemory: 2.6005e+009
       MultiprocessorCount: 14
              ClockRateKHz: 1147000
               ComputeMode: 'Default'
      GPUOverlapsTransfers: 1
    KernelExecutionTimeout: 1
          CanMapHostMemory: 1
           DeviceSupported: 1
            DeviceSelected: 1
</pre><p>We are running on a Tesla C2050.  Currently, Parallel Computing Toolbox supports NVDIA GPUs with Compute Capability 1.3 or
      greater. <a href="http://www.nvidia.com/object/cuda_gpus.html">Here</a> is a link to see if your card is supported.
   </p>
   <h3>A Simple Example using Overloaded Functions<a name="7"></a></h3>
   <p>Over 100 operations (e.g. <tt>fft</tt>, <tt>ifft</tt>, <tt>eig</tt>) are now available as built-in MATLAB functions that can be executed directly on the GPU by providing an input argument of
      the type GPUArray. These GPU-enabled functions are overloaded&#8212;in other words, they operate differently depending on the data
      type of the arguments passed to them. <a href="http://www.mathworks.com/help/releases/R2011b/toolbox/distcomp/bsic4fr-1.html">Here</a> is a list of all the overloaded functions.
   </p>
   <p>Let's create a GPUArray and perform a <tt>fft</tt> using the GPU. However, let's first do this on the CPU so that we can see the difference in code and performance
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">A1 = rand(3000,3000);
tic;
B1 = fft(A1);
time1 = toc;</pre><p>To perform the same operation on the GPU, we first use <tt>gpuArray</tt> to transfer data from the MATLAB workspace to device memory. Then we can run <tt>fft</tt>, which is one of the overloaded functions on that data:
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">A2 = gpuArray(A1);
tic;
B2 = fft(A2);
time2 = toc;</pre><p>The second case is executed on the GPU rather than the CPU since its input (a GPUArray) is held on the GPU. The result, <tt>B2</tt>, is stored on the GPU. However, it is still visible in the MATLAB workspace. By running <tt>class(B2)</tt>, we can see that it is also a GPUArray.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">class(B2)</pre><pre style="font-style:oblique">ans =
parallel.gpu.GPUArray
</pre><p>To bring the data back to the CPU, we run <tt>gather</tt>.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">B2 = gather(B2);
class(B2)</pre><pre style="font-style:oblique">ans =
double
</pre><p><tt>B2</tt> is now on the CPU and has a class of double.
   </p>
   <h3>Speedup For Our Simple Example<a name="12"></a></h3>
   <p>In this simple example, we can calculate the speedup of performing the <tt>fft</tt> on our data.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">speedUp = time1/time2;
disp(speedUp)</pre><pre style="font-style:oblique">    3.6122
</pre><p>It looks like our <tt>fft</tt> is running ~3.5x faster on the GPU. This is pretty good, especially since <tt>fft</tt> is multi-threaded in core MATLAB. However, to perform a realistic comparison, we should really include the time spent transferring
      the vector to and from the GPU. If we do that - we find that our acceleration is greatly reduced. Let's see what happens if
      we include the time to do our data transfer.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">tic;
A3 = gpuArray(A1);
B3 = fft(A3);
B3 = gather(B3);
time3 = toc;

speedUp = time1/time3;
disp(speedUp)</pre><pre style="font-style:oblique">    0.5676
</pre><h3>Understanding and Limiting Your Data Transfer Overhead<a name="14"></a></h3>
   <p>Data transfer overhead can become so significant that it degrades the application's overall performance, especially if you
      repeatedly exchange data between the CPU and GPU to execute relatively few computationally intensive operations. However not
      all hope is lost!  By limiting the data transfer between the GPU and the CPU, we can still acheive speedup.
   </p>
   <p>Instead of creating the data on the CPU and transferring it to the GPU - we can just create on the GPU directly.  There are
      several <a href="http://www.mathworks.com/help/releases/R2011b/toolbox/distcomp/bsic4fr-1.html">constructors</a> available as well as constructor-like functions such as <tt>meshgrid</tt>. Let's see how that effects our time.  To be accurate, we should also retime the original serial code including the time
      it takes to generate the random matrix on the CPU.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">tic;
A4 = rand(3000,3000);
B4 = fft(A4);
time4 = toc;

tic;
A5 = parallel.gpu.GPUArray.rand(3000,3000);
B5 = fft(A5);
B5 = gather(B5);
time5 = toc;

speedUp = time4/time5;
disp(speedUp);</pre><pre style="font-style:oblique">    1.4100
</pre><p>This is better, although we still do see the influence of gathering the data back from the GPU.  In this simple demo, the
      effect is exaggerated because we are running a single, already fast operation on the GPU. It is more efficient to perform
      several operations on the data while it is on the GPU, bringing the data back to the CPU only when required.
   </p>
   <h3>Solving the Wave Equation<a name="16"></a></h3>
   <p><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/346/WaveEquation.gif"> </p>
   <p>To put the above example into context, let's use the same GPU functionality on a more "real-world" problem. To do this,  we
      are going to solve a second-order wave equation:
   </p>
   <p><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/346/UsingGPUs_eq30596.png"> </p>
   <p>The algorithm we use to solve the wave equation combines a spectral method in space and a second-order central finite difference
      method in time. Specifically, we apply the Chebyshev spectral method, which uses Chebyshev polynomials as the basis functions.
      Our example is largely based on an example in Trefethen's book: <a href="http://www.mathworks.com/support/books/book48110.html">"Spectral Methods in MATLAB"</a></p>
   <h3>Code Changes to Run Algorithm on GPU<a name="20"></a></h3>
   <p>When accelerating our alogrithm, we focus on speeding up the code within the main time stepping while-loop. The operations
      in that part of the code (e.g. <tt>fft</tt> and <tt>ifft</tt>, matrix multiplication) are all overloaded functions that work with the GPU. As a result, we do not need to change the algorithm
      in any way to execute it on a GPU. We get to simply transfer the data to the GPU and back to the CPU when finished.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">type(<span style="color: #A020F0">'stepSolution.m'</span>)</pre><pre style="font-style:oblique">
function [vv,vvold] = stepsolution(vv,vvold,ii,N,dt,W1T,W2T,W3T,W4T,...
                                        WuxxT1,WuxxT2,WuyyT1,WuyyT2)
    V = [vv(ii,:) vv(ii,N:-1:2)];
    U = real(fft(V.')).';
    
    W1test = (U.*W1T).';
    W2test = (U.*W2T).';
    W1 = (real(ifft(W1test))).';
    W2 = (real(ifft(W2test))).';
    
    % Calculating 2nd derivative in x
    uxx(ii,ii) = W2(:,ii).* WuxxT1 - W1(:,ii).*WuxxT2;
    uxx([1,N+1],[1,N+1]) = 0;
    
    V = [vv(:,ii); vv((N:-1:2),ii)];
    U = real(fft(V));
    
    W1 = real(ifft(U.*W3T));
    W2 = real(ifft(U.*W4T));
    
    % Calculating 2nd derivative in y
    uyy(ii,ii) = W2(ii,:).* WuyyT1 - W1(ii,:).*WuyyT2;
    uyy([1,N+1],[1,N+1]) = 0;
    
    % Computing new value using 2nd order central finite difference in
    % time
    vvnew = 2*vv - vvold + dt*dt*(uxx+uyy);
    vvold = vv; vv = vvnew;

end


</pre><h3>Code Changes to Transfer Data to the GPU and Back Again<a name="21"></a></h3>
   <p>The variables <tt>dt</tt>, <tt>x</tt>, <tt>index1</tt>, and <tt>index2</tt> are calculated on the CPU and transferred over to the GPU using <tt>gpuArray</tt>.
   </p>
   <p>For example:</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">N = 256;
index1 = 1i*[0:N-1 0 1-N:-1];

index1 = gpuArray(index1);</pre><p>For the rest of the variables, we create them directly on the GPU using the overloaded versions of the transpose operator
      (<tt>'</tt>), <tt>repmat</tt> ,and <tt>meshgrid</tt>.
   </p>
   <p>For example:</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">W1T = repmat(index1,N-1,1);</pre><p>When, we are done with all our calculations on the GPU, we use <tt>gather</tt> once to bring data back from the GPU. Note, that we don't have to transfer our data back to the CPU between time steps. This
      all comes together to look like this:
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">type <span style="color: #A020F0">WaveGPU.m</span></pre><pre style="font-style:oblique">
function vvg = WaveGPU(N,Nsteps)
%% Solving 2nd Order Wave Equation Using Spectral Methods
% This example solves a 2nd order wave equation: utt = uxx + uyy, with u =
% 0 on the boundaries. It uses a 2nd order central finite difference in
% time and a Chebysehv spectral method in space (using FFT). 
%
% The code has been modified from an example in Spectral Methods in MATLAB
% by Trefethen, Lloyd N.

% Points in X and Y
x = cos(pi*(0:N)/N); % using Chebyshev points

% Send x to the GPU
x = gpuArray(x);
y = x';

% Calculating time step
dt = 6/N^2;

% Setting up grid
[xx,yy] = meshgrid(x,y);

% Calculate initial values
vv = exp(-40*((xx-.4).^2 + yy.^2));
vvold = vv;

ii = 2:N;
index1 = 1i*[0:N-1 0 1-N:-1];
index2 = -[0:N 1-N:-1].^2;

% Sending data to the GPU
dt = gpuArray(dt);
index1 = gpuArray(index1);
index2 = gpuArray(index2);

% Creating weights used for spectral differentiation 
W1T = repmat(index1,N-1,1);
W2T = repmat(index2,N-1,1);
W3T = repmat(index1.',1,N-1);
W4T = repmat(index2.',1,N-1);

WuxxT1 = repmat((1./(1-x(ii).^2)),N-1,1);
WuxxT2 = repmat(x(ii)./(1-x(ii).^2).^(3/2),N-1,1);
WuyyT1 = repmat(1./(1-y(ii).^2),1,N-1);
WuyyT2 = repmat(y(ii)./(1-y(ii).^2).^(3/2),1,N-1);

% Start time-stepping
n = 0;

while n &lt; Nsteps
    [vv,vvold] = stepsolution(vv,vvold,ii,N,dt,W1T,W2T,W3T,W4T,...
                                 WuxxT1,WuxxT2,WuyyT1,WuyyT2);
    n = n + 1;
end


% Gather vvg back from GPU memory when done
vvg = gather(vv);


</pre><h3>Comparing CPU and GPU Execution Speeds<a name="24"></a></h3>
   <p>We ran a benchmark in which we measured the amount of time it took to execute 50 time steps for grid sizes of 64, 128, 512,
      1024, and 2048 on an Intel Xeon Processor X5650 and then using an NVIDIA Tesla C2050 GPU.
   </p>
   <p><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/346/Benchmark.png"> </p>
   <p>For a grid size of 2048, the algorithm shows a 7.5x decrease in compute time from more than a minute on the CPU to less than
      10 seconds on the GPU.  The log scale plot shows that the CPU is actually faster for small grid sizes. As the technology evolves
      and matures, however, GPU solutions are increasingly able to handle smaller problems, a trend that we expect to continue.
   </p>
   <h3>Other Ways to Use the GPU with MATLAB<a name="27"></a></h3>
   <p>To accelerate an algorithm with multiple element-wise operations on a GPU, you can use <tt>arrayfun</tt>, which applies a function to each element of an GPUarray. Additionally if you have your own CUDA code, you can use the CUDAKernel
      interface to integrate this code with MATLAB.
   </p>
   <p>Ben Tordoff's previous guest blog post <a href="http://blogs.mathworks.com/loren/2011/07/18/a-mandelbrot-set-on-the-gpu/">Mandelbrots Sets on the GPU</a> shows a great example using both of these capabilities.
   </p>
     <p><em>Addendum</em>: There has been some confusion about using <tt>assignin</tt> and
<tt>evalin</tt> inside the body of <tt>parfor</tt> loops.  You can use these
functions if they use the 'base' workspace. However, please use caution
when using them since the 'base' workspace  will not be the same as the
client 'base' workspace.
   </p>   <h3>Your Thoughts?<a name="28"></a></h3>
   <p>Have you experimented with our new GPU functionality?  Let us know what you think or if you have any questions by leaving
      a comment for this post <a href="http://blogs.mathworks.com/loren/?p=346#respond">here</a>.
   </p><script language="JavaScript">
<!--

    function grabCode_43a6565ff583451ea3dcbf9a94261aab() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='43a6565ff583451ea3dcbf9a94261aab ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' 43a6565ff583451ea3dcbf9a94261aab';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Sarah Wait Zaranek';
        copyright = 'Copyright 2012 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_43a6565ff583451ea3dcbf9a94261aab()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
43a6565ff583451ea3dcbf9a94261aab ##### SOURCE BEGIN #####
%% Using GPUs in MATLAB
% Today Iâ€™d like to introduce guest blogger
% <mailto:sarah.zaranek@mathworks.com Sarah Wait Zaranek> who works for the
% MATLAB Marketing team. Sarah previously has
% <http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/
% written> about speeding up code from a customer to get acceptable
% performance. She will be discussing how to use GPUs to
% accelerate your MATLAB applications.

%% Overview  
% In this post, we first will introduce the basics of using the GPU with
% MATLAB and then move onto solving a 2nd-order wave equation using this
% GPU functionality. This blog post is inspired by a recent MATLAB Digest
% <http://www.mathworks.com/company/newsletters/articles/gpu-programming-in-matlab.html
% article> on GPU Computing that I coauthored with one of our developers,
% Jill Reese. Since the original demo was made, the GPU functions available
% in MATLAB have grown. If you compare the code below to the code in the
% paper- they are slightly different, reflecting these new capabilites.
% Additionally, small changes were made to enable easier explanation of the
% code in this blog format. The GPU functionality shown in this
% post requires the Parallel Computing Toolbox.
%
%% GPU Background
% Originally used to accelerate graphics rendering, GPUs are now
% increasingly applied to scientific calculations. Unlike a traditional
% CPU, which includes no more than a handful of cores, a GPU has a
% massively parallel array of integer and floating-point processors, as
% well as dedicated, high-speed memory. A typical GPU comprises hundreds of
% these smaller processors.  These processors can be used to greatly
% speed-up particular types of applications.
%
% A good rule of thumb is that your problem may be a good fit for the GPU
% if it is:
%%
% * Massively parallel: 
% The computations can be broken down into hundreds or thousands of
% independent units of work.  You will see the best performance when all of
% the cores are kept busy, exploiting the inherent parallel nature of the
% GPU. Seemingly simple, vectorized MATLAB calculations on arrays with 
% hundreds of thousands of elements often can fit into this category. 
%%
% * Computationally intensive: 
% The time spent on computation significantly exceeds the time spent on
% transferring data to and from GPU memory. Because a GPU is attached to
% the host CPU via the PCI Express bus, the memory access is slower than
% with a traditional CPU. This means that your overall computational
% speedup is limited by the amount of data transfer that occurs in your
% algorithm.
%
% Applications that do not satisfy these criteria might actually run more
% slowly on a GPU than on a CPU.

%% Learning About Our GPU
% With that background, we can now start working with the GPU in MATLAB.
% Let's start first by querying our GPU to see just what we are working
% with:

gpuDevice

%%
% We are running on a Tesla C2050.  Currently, Parallel Computing Toolbox
% supports NVDIA GPUs with Compute Capability 1.3 or greater. <http://www.nvidia.com/object/cuda_gpus.html
% Here> is a link to see if your card is supported. 

%% A Simple Example using Overloaded Functions 
% Over 100 operations (e.g. |fft|, |ifft|, |eig|) are now available as
% built-in MATLAB functions that can be executed directly on the GPU by
% providing an input argument of the type GPUArray. These GPU-enabled
% functions are overloadedâ€”in other words, they operate differently
% depending on the data type of the arguments passed to them.
% <http://www.mathworks.com/help/releases/R2011b/toolbox/distcomp/bsic4fr-1.html
% Here> is a list of all the overloaded functions.
%
% Let's create a GPUArray and perform a |fft| using the GPU. However,
% let's first do this on the CPU so that we can see the difference in code
% and performance

A1 = rand(3000,3000); 
tic;
B1 = fft(A1);
time1 = toc;

%%
% To perform the same operation on the GPU, we first use |gpuArray|
% to transfer data from the MATLAB workspace to device memory. Then
% we can run |fft|, which is one of the overloaded functions on that data:

A2 = gpuArray(A1); 
tic;
B2 = fft(A2);
time2 = toc;

%%
% The second case is executed on the GPU rather than the CPU since its
% input (a GPUArray) is held on the GPU. The result, |B2|, is stored on the 
% GPU. However, it is still visible in the MATLAB workspace. By running 
% |class(B2)|, we can see that it is also a GPUArray.

class(B2)

%%
% To bring the data back to the CPU, we run |gather|.

B2 = gather(B2);
class(B2)

%%
% |B2| is now on the CPU and has a class of double. 

%% Speedup For Our Simple Example
%
% In this simple example, we can calculate the speedup of performing the
% |fft| on our data.  
%
speedUp = time1/time2;
disp(speedUp)

%%
% It looks like our |fft| is running ~3.5x faster on the GPU. This is
% pretty good, especially since |fft| is multi-threaded in core MATLAB.
% However, to perform a realistic comparison, we should really include the
% time spent transferring the vector to and from the GPU. If we do that
% - we find that our acceleration is greatly reduced. Let's see what
% happens if we include the time to do our data transfer.

tic;
A3 = gpuArray(A1); 
B3 = fft(A3);
B3 = gather(B3);
time3 = toc;

speedUp = time1/time3;
disp(speedUp)

%% Understanding and Limiting Your Data Transfer Overhead 
%
% Data transfer overhead can become so significant that it degrades the
% application's overall performance, especially if you repeatedly exchange
% data between the CPU and GPU to execute relatively few computationally
% intensive operations. However not all hope is lost!  By limiting the data
% transfer between the GPU and the CPU, we can still acheive speedup.
%
% Instead of creating the data on the CPU and transferring it to the GPU -
% we can just create on the GPU directly.  There are several 
% <http://www.mathworks.com/help/releases/R2011b/toolbox/distcomp/bsic4fr-1.html
% constructors> available as well as constructor-like functions such as |meshgrid|.
% Let's see how that effects our time.  To be accurate, we should also
% retime the original serial code including the time it takes to generate
% the random matrix on the CPU.

tic;
A4 = rand(3000,3000); 
B4 = fft(A4);
time4 = toc;

tic;
A5 = parallel.gpu.GPUArray.rand(3000,3000); 
B5 = fft(A5);
B5 = gather(B5);
time5 = toc;

speedUp = time4/time5;
disp(speedUp);

%%
% This is better, although we still do see the influence of gathering the
% data back from the GPU.  In this simple demo, the effect is exaggerated
% because we are running a single, already fast operation on the GPU. It is
% more efficient to perform several operations on the data while it is on
% the GPU, bringing the data back to the CPU only when required. 

%% Solving the Wave Equation 
% <<WaveEquation.gif>>
%%
% To put the above example into context, let's use the same GPU
% functionality on a more "real-world" problem. To do this,  we are going
% to solve a second-order wave equation:
%%
% $$ \frac{\partial u}{\partial t} =  \frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2} $$

%%
% The algorithm we use to solve the wave equation combines a spectral
% method in space and a second-order central finite differenc method in
% time. Specifically, we apply the Chebyshev spectral method, which uses
% Chebyshev polynomials as the basis functions. Our example is largely
% based on an example in Trefethen's book:
% <http://www.mathworks.com/support/books/book48110.html "Spectral Methods
% in MATLAB">
%
%% Code Changes to Run Algorithm on GPU
% When accelerating our alogrithm, we focus on speeding up the code within
% the main time stepping while-loop. The operations in that part of the
% code (e.g. |fft| and |ifft|, matrix multiplication) are all overloaded
% functions that work with the GPU. As a result, we do not need to change
% the algorithm in any way to execute it on a GPU. We get to simply
% transfer the data to the GPU and back to the CPU when finished.

type('stepSolution.m')

%% Code Changes to Transfer Data to the GPU and Back Again
% The variables |dt|, |x|, |index1|, and |index2| are
% calculated on the CPU and transferred over to the GPU using |gpuArray|.
%
% For example:

N = 256;  
index1 = 1i*[0:N-1 0 1-N:-1];

index1 = gpuArray(index1);

%%
% For the rest of the variables, we create them directly on the GPU using
% the overloaded versions of the transpose operator (|'|), |repmat| ,and
% |meshgrid|. 
%
% For example:

W1T = repmat(index1,N-1,1);

%%
% When, we are done with all our calculations on the GPU, we use |gather|
% once to bring data back from the GPU. Note, that we don't have to
% transfer our data back to the CPU between time steps. This all comes
% together to look like this:

type WaveGPU.m

%% Comparing CPU and GPU Execution Speeds
% We ran a benchmark in which we measured the amount of time it took to
% execute 50 time steps for grid sizes of 64, 128, 512, 1024, and 2048 on
% an Intel Xeon Processor X5650 and then using an NVIDIA Tesla C2050 GPU.
%
%%
% <<Benchmark.png>>

%% 
% For a grid size of 2048, the algorithm shows a 7.5x decrease in compute
% time from more than a minute on the CPU to less than 10 seconds on the
% GPU.  The log scale plot shows that the CPU is actually faster
% for small grid sizes. As the technology evolves and matures, however, GPU
% solutions are increasingly able to handle smaller problems, a trend that
% we expect to continue.
% 
%% Other Ways to Use the GPU with MATLAB 
% To accelerate an algorithm with multiple element-wise operations on a
% GPU, you can use |arrayfun|, which applies a function to each element of
% an GPUarray. Additionally if you have your own CUDA code, you can use the
% CUDAKernel interface to integrate this code with MATLAB.
% 
% Ben Tordoff's previous guest blog post
% <http://blogs.mathworks.com/loren/2011/07/18/a-mandelbrot-set-on-the-gpu/
% Mandelbrots Sets on the GPU> shows a great example using both of these
% capabilities.

%% Your Thoughts?
% Have you experimented with our new GPU functionality?  Let us know what
% you think or if you have any questions by leaving a comment for this
% post <http://blogs.mathworks.com/loren/?p=346#respond here>.

##### SOURCE END ##### 43a6565ff583451ea3dcbf9a94261aab
-->
<img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/ouvhwaD6oEY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2012/02/06/using-gpus-in-matlab/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2012/02/06/using-gpus-in-matlab/</feedburner:origLink></item>
		<item>
		<title>MATLAB Documentation</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/eYlnEEH9he0/</link>
		<comments>http://blogs.mathworks.com/loren/2012/01/27/matlab-documentation/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 20:12:08 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[New Feature]]></category>
		<category><![CDATA[Tool]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=336</guid>
		<description><![CDATA[For quite a while now, we have made MathWorks product documentation available from the support page on our web site. Though we started with MATLAB documentation initially, we have added documentation for all the rest of our products and have hosted a complete set on the mathworks.com for quite some time now. As you likely [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p>For quite a while now, we have made MathWorks product documentation available from the <a href="http://www.mathworks.com/support/">support</a> page on our web site.  Though we started with MATLAB documentation initially, we have added documentation for all the rest
         of our products and have hosted a complete set on the mathworks.com for quite some time now.
      </p>
      <p>As you likely know, within the MathWorks family of products, you can often find more than one way, or even more than one product,
         to accomplish a particular goal.  However, it's been difficult to see the whole landscape of possibilities, including functions,
         examples, information in the User Guide, etc., for any given topic - until now, that is.  I want to show you our more integrated
         approach for seeking information in this post.
      </p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">Find Documentation On-Line</a></li>
         <li><a href="#2">Find New Beta Documentation Center</a></li>
         <li><a href="#3">Documentation Center Features</a></li>
         <li><a href="#4">Browse Documentation Topics by category</a></li>
         <li><a href="#5">Search Documentation Topics</a></li>
         <li><a href="#6">Learning About Curve Fitting</a></li>
         <li><a href="#7">Documentation Center</a></li>
      </ul>
   </div>
   <h3>Find Documentation On-Line<a name="1"></a></h3>
   <p>If you go to <a href="http://www.mathworks.com">MathWorks website</a>, you can search in the box in the upper right corner for the term "documention".  Here's a snippet of the screen you see
      next.
   </p>
   <p><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/336/searchDoc.png"> </p>
   <h3>Find New Beta Documentation Center<a name="2"></a></h3>
   <p>Choose the second link, 'Online Documentation for MathWorks Product Family', and you reach a page that looks like this one.</p>
   <p><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/336/mainDocLink.png"> </p>
   <p>See the prominent green box near the top 'Try the Documentation Center Beta' to see the new way to search for topics in our
      documentation and you will see a page that looks like this one.
   </p>
   <p><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/336/docCenterBeta.png"> </p>
   <h3>Documentation Center Features<a name="3"></a></h3>
   <p>In Release 2011b, the documentation already has many features that allow you to pinpoint relevant information quickly.</p>
   <h3>Browse Documentation Topics by category<a name="4"></a></h3>
   <div>
      <ul>
         <li>Help topics are organized by categories to supports your tasks</li>
         <li>Categories include function reference pages, examples, and other related content *	Consistent access to Getting Started, Troubleshooting,
            Featured Examples, and reference pages
         </li>
      </ul>
   </div>
   <h3>Search Documentation Topics<a name="5"></a></h3>
   <p>*	Get search suggestions as you type, organized by content type *	Refine search results by product, category, and content
      type *	Single-click access to Functions and Blocks in a product *	Expand search results to all Support content
   </p>
   <h3>Learning About Curve Fitting<a name="6"></a></h3>
   <p>Let's see what happens when we search for 'curve fitting' in the Doc Center.  Here you see a screen with several different
      areas of focus.
   </p>
   <p><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/336/cfDocBeta.png"> </p>
   <p>You can see relevant documentation in MathWorks products in the main, right-hand portion of the window.  In addition, the
      left side contains information clustered in other ways, allowing you to refine the search overall, by the type of information
      (functions, blocks, etc.), or by product.
   </p>
   <h3>Documentation Center<a name="7"></a></h3>
   <p>We hope you like the presentation of information in the Documentation Center and find it helpful. We have plans for more features
      for the Documentation Center. Please feel free to post your thoughts <a href="http://blogs.mathworks.com/loren/?p=336#respond">here</a>.
   </p><script language="JavaScript">
<!--

    function grabCode_a4e591c0561e4b8693e968f6bd46562d() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='a4e591c0561e4b8693e968f6bd46562d ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' a4e591c0561e4b8693e968f6bd46562d';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Loren Shure';
        copyright = 'Copyright 2012 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_a4e591c0561e4b8693e968f6bd46562d()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
a4e591c0561e4b8693e968f6bd46562d ##### SOURCE BEGIN #####
%% MATLAB Documentation 
% For quite a while now, we have made MathWorks product documentation
% available from the <http://www.mathworks.com/support/ support> page on
% our web site.  Though we started with MATLAB documentation initially, we
% have added documentation for all the rest of our products and have hosted
% a complete set on the mathworks.com for quite some time now.
%
% As you likely know, within the MathWorks family of products, you can
% often find more than one way, or even more than one product, to
% accomplish a particular goal.  However, it's been difficult to see the
% whole landscape of possibilities, including functions, examples,
% information in the User Guide, etc., for any given topic - until now,
% that is.  I want to show you our more integrated approach for seeking
% information in this post.
%% Find Documentation On-Line
% If you go to <http://www.mathworks.com MathWorks website>, you can search
% in the box in the upper right corner for the term "documention".  Here's
% a snippet of the screen you see next.
%
% <<searchDoc.png>>
%
%% Find New Beta Documentation Center
% Choose the second link, 'Online Documentation for MathWorks Product
% Family', and you reach a page that looks like this one.
%
% <<mainDocLink.png>>
%
% See the prominent green box near the top 'Try the Documentation Center
% Beta' to see the new way to search for topics in our documentation and
% you will see a page that looks like this one.
%
% <<docCenterBeta.png>>

%% Documentation Center Features
% In Release 2011b, the documentation already has many features that allow
% you to pinpoint relevant information quickly.
%

%% Browse Documentation Topics by category
%
% * Help topics are organized by categories to supports your tasks
% * Categories include function reference pages, examples, and other
% related content *	Consistent access to Getting Started, Troubleshooting,
% Featured Examples, and reference pages

%% Search Documentation Topics
%
% *	Get search suggestions as you type, organized by content type *	Refine
% search results by product, category, and content type *	Single-click
% access to Functions and Blocks in a product *	Expand search results to
% all Support content

%% Learning About Curve Fitting
% Let's see what happens when we search for 'curve fitting' in the Doc
% Center.  Here you see a screen with several different areas of focus.
%
% <<cfDocBeta.png>>
%
% You can see relevant documentation in MathWorks products in the main,
% right-hand portion of the window.  In addition, the left side
% contains information clustered in other ways, allowing you to refine the
% search overall, by the type of information (functions, blocks, etc.), or
% by product.
%%  Documentation Center
% We hope you like the presentation of information in the Documentation
% Center and find it helpful. We have plans for more features for the
% Documentation Center. Please feel free to post your thoughts
% <http://blogs.mathworks.com/loren/?p=336#respond here>.
##### SOURCE END ##### a4e591c0561e4b8693e968f6bd46562d
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/eYlnEEH9he0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2012/01/27/matlab-documentation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2012/01/27/matlab-documentation/</feedburner:origLink></item>
		<item>
		<title>Best Practices for Programming MATLAB</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/9mLFPLKFNqE/</link>
		<comments>http://blogs.mathworks.com/loren/2012/01/13/best-practices-for-programming-matlab/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 12:21:09 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[Best Practice]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=330</guid>
		<description><![CDATA[I thought I would share my top goto list of things I try to do when I write MATLAB code. And checking with other MathWorks folks whose code I admire, I found they basically used the same mental list that I use. You can find blog posts on all of these topics by selecting relevant [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p>I thought I would share my top goto list of things I try to do when I write MATLAB code. And checking with other MathWorks
         folks whose code I admire, I found they basically used the same mental list that I use.  You can find blog posts on all of
         these topics by selecting relevant categories from the right side of The Art of MATLAB blog site.
      </p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">My List of Best Practices</a></li>
         <li><a href="#2">Missing from Your List?  Additions to My List?</a></li>
      </ul>
   </div>
   <h3>My List of Best Practices<a name="1"></a></h3>
   <p>Clearly (at least to me), this is not everything you generally need to do.  You still need to comment the code, add good help
      information and examples, etc.  But these are the main coding practices and tools I always rely on.
   </p>
   <div>
      <ol>
         <li>Vectorize (but sensibly).</li>
         <li>Use <tt>bsxfun</tt> in lieu of <tt>repmat</tt> where possible.
         </li>
         <li>When looping through an array, loop down columns to access memory in the same order that MATLAB stores the data in.</li>
         <li>Profile the code.  I am often surprised about what is taking up the time.</li>
         <li>Pay attention to messages from the Code Analyzer.</li>
         <li>Use functions instead of scripts.</li>
         <li>Don't "poof" variables into any workspaces. Translation, don't use <tt>load</tt> without a left-hand side; avoid <tt>eval</tt>, <tt>evalin</tt>, and <tt>assignin</tt>.
         </li>
         <li>Use logical indexing instead of <tt>find</tt>.
         </li>
         <li>Avoid global variables.</li>
         <li>Don't use equality checks with floating point values.</li>
      </ol>
   </div>
   <h3>Missing from Your List?  Additions to My List?<a name="2"></a></h3>
   <p>What's on my list that you don't currently do?  Do you have a <b>major</b> addition to my list (there can't be too many, or I won't remember to do them all!)? Let me know <a href="http://blogs.mathworks.com/loren/?p=330#respond">here</a>.
   </p><script language="JavaScript">
<!--

    function grabCode_99ce53a4a3924039a56c831f8cd368d8() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='99ce53a4a3924039a56c831f8cd368d8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' 99ce53a4a3924039a56c831f8cd368d8';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Loren Shure';
        copyright = 'Copyright 2012 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_99ce53a4a3924039a56c831f8cd368d8()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
99ce53a4a3924039a56c831f8cd368d8 ##### SOURCE BEGIN #####
%% Best Practices for Programming MATLAB
% I thought I would share my top goto list of things I try to do when I
% write MATLAB code. And checking with other MathWorks folks whose code I
% admire, I found they basically used the same mental list that I use.  You
% can find blog posts on all of these topics by selecting relevant
% categories from the right side of The Art of MATLAB blog site.
%% My List of Best Practices
% Clearly (at least to me), this is not everything you generally need to
% do.  You still need to comment the code, add good help information and
% examples, etc.  But these are the main coding practices and tools I
% always rely on.
%
% # Vectorize (but sensibly).
% # Use |bsxfun| in lieu of |repmat| where possible.
% # When looping through an array, loop down columns to access memory in
% the same order that MATLAB stores the data in.
% # Profile the code.  I am often surprised about what is taking up the
% time.
% # Pay attention to messages from the Code Analyzer.
% # Use functions instead of scripts.
% # Don't "poof" variables into any workspaces. Translation, don't use
% |load| without a left-hand side; avoid |eval|, |evalin|, and |assignin|.
% # Use logical indexing instead of |find|.
% # Avoid global variables.
% # Don't use equality checks with floating point values.
%
%% Missing from Your List?  Additions to My List?
% What's on my list that you don't currently do?  Do you have a *major*
% addition to my list (there can't be too many, or I won't remember to do
% them all!)? Let me know <http://blogs.mathworks.com/loren/?p=330#respond
% here>.
##### SOURCE END ##### 99ce53a4a3924039a56c831f8cd368d8
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/9mLFPLKFNqE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2012/01/13/best-practices-for-programming-matlab/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2012/01/13/best-practices-for-programming-matlab/</feedburner:origLink></item>
		<item>
		<title>Liberating Deployed Application Output from the Console Window</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/evltHKgLAgI/</link>
		<comments>http://blogs.mathworks.com/loren/2012/01/02/liberating-deployed-application-output-from-the-console-window/#comments</comments>
		<pubDate>Mon, 02 Jan 2012 14:35:47 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[Deployment]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=320</guid>
		<description><![CDATA[Guest blogger Peter Webb returns with another in an occasional series of postings about application deployment. Contents Integrating Output with a Window System Creating a Shared Library Displaying Messages in a Visible Window Integrating Output with a Window System Applications developed with the MATLAB Compiler direct their output to the console window. While this may [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p>Guest blogger <a href="http://www.mathworks.com/matlabcentral/fileexchange/authors/4660">Peter Webb</a> returns with another in an <a href="http://blogs.mathworks.com/loren/category/deployment/">occasional series</a> of postings about application deployment.
      </p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">Integrating Output with a Window System</a></li>
         <li><a href="#2">Creating a Shared Library</a></li>
         <li><a href="#3">Displaying Messages in a Visible Window</a></li>
      </ul>
   </div>
   <h3>Integrating Output with a Window System<a name="1"></a></h3>
   <p>Applications developed with the MATLAB Compiler direct their output to the console window. While this may suffice for batch
      or background tasks, it is often not suitable for interactive or graphical environments. In an <a href="http://blogs.mathworks.com/loren/2011/11/04/managing-deployed-application-output-with-message-handlers">earlier post</a>, I demonstrated how to use <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f2-998954.html#f2-1008529"><i>message handlers</i></a> to redirect program output and error messages to a log file. For many batch or background applications, simply capturing
      output to a log file provides sufficient information for validation and debugging. However, if you need to more closely monitor
      the progress of your application while it is running, you might want to redirect output to a visible window.
   </p>
   <p>I've written a short C program in <tt>monitor.c</tt> to demonstrate a few applications of this idea: on UNIX, the program sends output to an <a href="http://en.wikipedia.org/wiki/X_terminal">X terminal</a>; on Windows, I created two programs. The simplest of the Windows programs displays the output in a Windows dialog box. The
      other, slightly more complex, sends the output as events to the <a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/snap_event_viewer.mspx?mfr=true">Windows Event Log</a>.
   </p>
   <p>In all three cases (UNIX and Windows), the <tt>monitor</tt> main program calls a <a href="http://www.mathworks.com/products/matlab">MATLAB</a> <a href="http://www.mathworks.com/help/techdoc/ref/function.html">function</a>, <tt>sendmessages</tt>, contained in a shared library generated by <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/compiler">MATLAB Compiler</a>.
   </p>
   <h3>Creating a Shared Library<a name="2"></a></h3>
   <p>The <tt>sendmessages</tt> function displays an <a href="http://www.mathworks.com/help/techdoc/ref/disp.html">informational message</a>, a <a href="http://www.mathworks.com/help/techdoc/ref/warning.html">warning</a> and then issues an <a href="http://www.mathworks.com/help/techdoc/ref/error.html">error</a>:
   </p><pre>function sendmessages(info, warn, err)
    disp(info); warning(warn); error(err);</pre><p>Create a <a href="http://www.mathworks.com/help/toolbox/compiler/f2-972343.html">C shared library</a> containing <tt>sendmessages</tt> with <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f0-979233.html">MATLAB Compiler</a>:
   </p><pre>mcc -W lib:libmsgfcn -T link:lib sendmessages.m</pre><p>To follow the rest of this posting, <a href="http://www.mathworks.com/matlabcentral/fileexchange/32598-liberating-deployed-application-output-from-the-console-window">download the source code</a> for the main program that calls <tt>sendmessages</tt> from MATLAB Central. See the <tt>monitorREADME</tt> in the ZIP file for a description of each of the downloaded files and complete directions for building the example application.
   </p>
   <p>Build the application using <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/mbuild.html#zmw57dd0e18448">mbuild</a>:
   </p>
   <p>UNIX:</p><pre>mbuild -g monitor.c monitorUNIX.c -L. -lmsgfcn</pre><p>Windows -- two commands because there are two programs:</p><pre>mbuild -g monitorWinDialog.c monitor.c WinLogger.rc libmsgfcn.lib</pre><pre>mbuild -g monitorWinEvents.c monitor.c libmsgfcn.lib</pre><p>The first command builds the dialog box version (<tt>monitorWinDialog.exe</tt>) and the second command builds the event log version (<tt>monitorWinEvents.exe</tt>).
   </p>
   <p>You can't run <tt>monitorWinEvents.exe</tt> on Microsoft Windows until you register the event source. (UNIX readers can skip this step.) To register the event source,
      follow the step-by-step instructions in the <tt>monitorREADME</tt> file in the source code distribution ZIP file.
   </p>
   <h3>Displaying Messages in a Visible Window<a name="3"></a></h3>
   <p>The main program in <tt>monitor.c</tt> relies on three external functions:
   </p><pre>void StartMonitoring(void) void StopMonitoring(void) int
MonitorHandler(const char *msg)</pre><p><tt>StartMonitoring</tt> initializes the monitoring system, <tt>MonitorHandler</tt> redirects the program's output to the monitoring window and <tt>StopMonitoring</tt> releases the resources required to capture the program's output. The implementation of these functions varies by platform.
   </p>
   <p>On UNIX, for example, the <tt>StartMonitor</tt> function starts an <tt>xterm</tt> which runs <tt>tail -f</tt> to monitor the log file:
   </p><pre>void StartMonitoring() {
    if (tmpLog == NULL) {
        char logFileName[80]; char cmd[128];</pre><pre>        sprintf(logFileName, "/tmp/monitorLog.%d", getpid()); tmpLog =
        fopen(logFileName, "w"); sprintf(cmd, "xterm -e tail -f %s&amp;",
        logFileName); system(cmd);
    }
}</pre><p>The UNIX message handler, <tt>MonitorHandler</tt>, is identical to the message handler from <a href="http://blogs.mathworks.com/loren/2011/11/04/managing-deployed-application-output-with-message-handlers/">the previous post's</a> example, except that it writes messages to a temporary log file instead of a persistent one.
   </p>
   <p>On Windows, the simplest version of the monitoring system sends messages to a dialog box. <tt>StartMonitoring</tt> creates the dialog box and starts it running in a separate thread. <tt>MonitorHandler</tt> calls <tt>LogMessage</tt> to add lines of text to the <a href="http://msdn.microsoft.com/en-us/library/bb775146%28VS.85%29.aspx"><tt>ListBox</tt></a> contained in the dialog box. <tt>StopMonitoring</tt> waits for the dialog box's thread to terminate. <tt>LogMessage</tt> sends the lines of text to the <tt>ListBox</tt> using the Windows messaging system. Since each item in a list box only displays a single line of text, <tt>LogMessage</tt> sends multi-line text as a sequence of separate messages, one message per line.
   </p><pre>int LogMessage(const char *s, int msgType) {
    char msg[1024]; char *pos = &amp;msg[0]; char *cr = NULL; msg[0] = '\0';
    strncat(msg, s, 1024);</pre><pre>    while (pos != NULL &amp;&amp; *pos != '\0') {
       cr = strchr(pos, '\n');
        if (cr != NULL)
            *cr = '\0';
        pos = CleanText(pos); if (strlen(pos) &gt; 0)
            SendMessage(loggerDialog, WM_COMMAND, MAKEWPARAM(msgType,
            0),
                        (LPARAM)pos);
        pos = cr+1;
    } return strlen(s);
}</pre><p>Now run it. The program takes three input arguments: the informational message, the warning and the error message.</p>
   <p>(UNIX):    <tt>./monitor information "look out!" oops</tt></p>
   <p>(Windows): <tt>monitorWinDialog.exe information "look out!" oops</tt></p>
   <p>On UNIX machines and when running the dialog box version (<tt>monitorWinDialog</tt>) on a Windows machine, you'll see a window pop up and display the messages. To view the output of the event log version on
      a Windows PC, you'll need to <a href="http://support.microsoft.com/kb/308427">open the Event Logger</a> and click on the <i>Application</i> category.
   </p>
   <p>Next time: handling errors differently than information and warning messages. Until then, <a href="http://blogs/mathworks.com/loren/?p=301#respond">let me know</a> how your application makes sure error messages get delivered to users. Do you need a permanent record, like the Windows event
      logger provides?
   </p><script language="JavaScript">
<!--

    function grabCode_9aa2fd68293244438880c24c79ee83fe() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='9aa2fd68293244438880c24c79ee83fe ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' 9aa2fd68293244438880c24c79ee83fe';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Peter Webb';
        copyright = 'Copyright 2011 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_9aa2fd68293244438880c24c79ee83fe()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
9aa2fd68293244438880c24c79ee83fe ##### SOURCE BEGIN #####
%% Liberating Deployed Application Output from the Console Window
% Guest blogger
% <http://www.mathworks.com/matlabcentral/fileexchange/authors/4660 Peter
% Webb> returns with another in an
% <http://blogs.mathworks.com/loren/category/deployment/ occasional series>
% of postings about application deployment.
%% Integrating Output with a Window System
% Applications developed with the MATLAB Compiler direct their output to
% the console window. While this may suffice for batch or background tasks,
% it is often not suitable for interactive or graphical environments. In an
% <http://blogs.mathworks.com/loren/2011/11/04/managing-deployed-application-output-with-message-handlers
% earlier post>, I demonstrated how to use
% <http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f2-998954.html#f2-1008529
% _message handlers_> to redirect program output and error messages to a
% log file. For many batch or background applications, simply capturing
% output to a log file provides sufficient information for validation and
% debugging. However, if you need to more closely monitor the progress of
% your application while it is running, you might want to redirect output
% to a visible window.
%
% I've written a short C program in |monitor.c| to demonstrate a few
% applications of this idea: on UNIX, the program sends output to an
% <http://en.wikipedia.org/wiki/X_terminal X terminal>; on Windows, I
% created two programs. The simplest of the Windows programs displays the
% output in a Windows dialog box. The other, slightly more complex, sends
% the output as events to the
% <http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/snap_event_viewer.mspx?mfr=true
% Windows Event Log>.
%
% In all three cases (UNIX and Windows), the |monitor| main program calls a
% <http://www.mathworks.com/products/matlab MATLAB>
% <http://www.mathworks.com/help/techdoc/ref/function.html function>,
% |sendmessages|, contained in a shared library generated by
% <http://www.mathworks.com/access/helpdesk/help/toolbox/compiler MATLAB
% Compiler>.
%
%% Creating a Shared Library
% The |sendmessages| function displays an
% <http://www.mathworks.com/help/techdoc/ref/disp.html informational
% message>, a <http://www.mathworks.com/help/techdoc/ref/warning.html
% warning> and then issues an
% <http://www.mathworks.com/help/techdoc/ref/error.html error>:
%
%  function sendmessages(info, warn, err)
%      disp(info); warning(warn); error(err);
%
% Create a <http://www.mathworks.com/help/toolbox/compiler/f2-972343.html C
% shared library> containing |sendmessages| with
% <http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f0-979233.html
% MATLAB Compiler>:
%
%  mcc -W lib:libmsgfcn -T link:lib sendmessages.m
%
% To follow the rest of this posting,
% <http://www.mathworks.com/matlabcentral/fileexchange/32598-liberating-deployed-application-output-from-the-console-window
% download the source code> for the main program that calls |sendmessages|
% from MATLAB Central. See the |monitorREADME| in the ZIP file for a
% description of each of the downloaded files and complete directions for
% building the example application.
%
% Build the application using
% <http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/mbuild.html#zmw57dd0e18448
% mbuild>:
%
% UNIX:
%
%  mbuild -g monitor.c monitorUNIX.c -L. -lmsgfcn
% 
% Windows REPLACE_WITH_DASH_DASH two commands because there are two programs:
%
%  mbuild -g monitorWinDialog.c monitor.c WinLogger.rc libmsgfcn.lib
%
%  mbuild -g monitorWinEvents.c monitor.c libmsgfcn.lib
%
% The first command builds the dialog box version (|monitorWinDialog.exe|)
% and the second command builds the event log version
% (|monitorWinEvents.exe|).
%
% You can't run |monitorWinEvents.exe| on Microsoft Windows until you
% register the event source. (UNIX readers can skip this step.) To register
% the event source, follow the step-by-step instructions in the
% |monitorREADME| file in the source code distribution ZIP file.
%% Displaying Messages in a Visible Window
% The main program in |monitor.c| relies on three external functions:
%
%  void StartMonitoring(void) void StopMonitoring(void) int
%  MonitorHandler(const char *msg)
%
% |StartMonitoring| initializes the monitoring system, |MonitorHandler|
% redirects the program's output to the monitoring window and
% |StopMonitoring| releases the resources required to capture the program's
% output. The implementation of these functions varies by platform.
%
% On UNIX, for example, the |StartMonitor| function starts an |xterm| which
% runs |tail -f| to monitor the log file:
%
%  void StartMonitoring() {
%      if (tmpLog == NULL) {
%          char logFileName[80]; char cmd[128];
%          
%          sprintf(logFileName, "/tmp/monitorLog.%d", getpid()); tmpLog =
%          fopen(logFileName, "w"); sprintf(cmd, "xterm -e tail -f %s&#038;",
%          logFileName); system(cmd);
%      }
%  }
%
% The UNIX message handler, |MonitorHandler|, is identical to the message
% handler from
% <http://blogs.mathworks.com/loren/2011/11/04/managing-deployed-application-output-with-message-handlers/
% the previous post's> example, except that it writes messages to a
% temporary log file instead of a persistent one.
%
% On Windows, the simplest version of the monitoring system sends messages
% to a dialog box. |StartMonitoring| creates the dialog box and starts it
% running in a separate thread. |MonitorHandler| calls |LogMessage| to add
% lines of text to the
% <http://msdn.microsoft.com/en-us/library/bb775146%28VS.85%29.aspx
% |ListBox|> contained in the dialog box. |StopMonitoring| waits for the
% dialog box's thread to terminate. |LogMessage| sends the lines of text to
% the |ListBox| using the Windows messaging system. Since each item in a
% list box only displays a single line of text, |LogMessage| sends
% multi-line text as a sequence of separate messages, one message per line.
%
%  int LogMessage(const char *s, int msgType) {
%      char msg[1024]; char *pos = &#038;msg[0]; char *cr = NULL; msg[0] = '\0';
%      strncat(msg, s, 1024);
%      
%      while (pos != NULL &#038;& *pos != '\0') {
%         cr = strchr(pos, '\n');
%          if (cr != NULL)
%              *cr = '\0';
%          pos = CleanText(pos); if (strlen(pos) > 0)
%              SendMessage(loggerDialog, WM_COMMAND, MAKEWPARAM(msgType,
%              0),
%                          (LPARAM)pos);
%          pos = cr+1;
%      } return strlen(s);
%  }
%
% Now run it. The program takes three input arguments: the informational
% message, the warning and the error message.
%
% (UNIX):    |./monitor information "look out!" oops|
%
% (Windows): |monitorWinDialog.exe information "look out!" oops|
% 
% On UNIX machines and when running the dialog box version
% (|monitorWinDialog|) on a Windows machine, you'll see a window pop up and
% display the messages. To view the output of the event log version on a
% Windows PC, you'll need to <http://support.microsoft.com/kb/308427 open
% the Event Logger> and click on the _Application_ category.
%
% Next time: handling errors differently than information and warning
% messages. Until then, <http://blogs/mathworks.com/loren/?p=301#respond
% let me know> how your application makes sure error messages get delivered
% to users. Do you need a permanent record, like the Windows event logger
% provides?
##### SOURCE END ##### 9aa2fd68293244438880c24c79ee83fe
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/evltHKgLAgI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2012/01/02/liberating-deployed-application-output-from-the-console-window/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2012/01/02/liberating-deployed-application-output-from-the-console-window/</feedburner:origLink></item>
		<item>
		<title>Ginger Plot Winner</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/a7CrViPL6dU/</link>
		<comments>http://blogs.mathworks.com/loren/2011/12/20/ginger-plot-winner/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 19:00:17 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=315</guid>
		<description><![CDATA[If you've been following the comments from my recent post on chaotic maps, you know that there were several entries to the challenge of coming up with an interesting visualization. The entries are each interesting, displaying schemes, including an animation, and extra information placed on the plots in various ways. I have conferred with a [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p>If you've been following the comments from my <a href="http://blogs.mathworks.com/loren/2011/12/07/pretty-2-dimensional-chaotic-maps/">recent post</a> on chaotic maps, you know that there were several entries to the challenge of coming up with an interesting visualization.
         The entries are each interesting, displaying schemes, including an animation, and extra information placed on the plots in
         various ways. I have conferred with a few colleagues here and we have chosen the contribution from Rafael Oliveira as the
         winning entry.  Why?  Because the code is interesting, he made interesting use interesting of some functions, the and the
         plot is cute (at least for <a href="http://www.mathworks.com/help/releases/R2011b/techdoc/ref/rng.html"><tt>rng(42)</tt></a>.
      </p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">Adorned Ginger Man</a></li>
         <li><a href="#3">The Code</a></li>
         <li><a href="#4">Thanks for Participating</a></li>
      </ul>
   </div>
   <h3>Adorned Ginger Man<a name="1"></a></h3>
   <p>Here's Rafael's adorned gingerman.</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">gingerR</pre><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/300/gingerWinner_01.png"> <p>Here's a comment from Rafael for this submission:</p>
   <p><i>I decided to make a pixelated gingerbread man with the output of the chaotic map, using a lemniscate of Gerono as his green
         bow tie :)</i></p>
   <h3>The Code<a name="3"></a></h3>
   <p>I took the liberty of adding a small number of comments to Rafael's code to highlight some parts of the plot.</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">type <span style="color: #A020F0">gingerR</span></pre><pre style="font-style:oblique">
function gingerR
%function pixelatedGBM from Rafael
    rng(42) % could comment this out to get other "men"
    [x,y] = gingerbreadman;
    % scale and rotate our gingerbread man
    r = [x y] * [cosd(135) -sind(135); sind(135) cosd(135)];
    minR = min(r); maxR = max(r);
    r = (r - repmat(minR,size(r,1),1))./repmat(maxR-minR,size(r,1),1);
    r(:,2) = r(:,2).^1.5;
    
    % create a pixel representation of it
    N = 25;
    b = linspace(0,1,N);
    dif = b(2)-b(1);
    [xb,yb] = meshgrid(b,b);
    
    C = zeros(N);
    x = r(:,1); y = r(:,2);
    for i = 1:numel(xb)
        C(i) = length(find(x &gt;= xb(i) &amp; x &lt; xb(i) +dif &amp; y &gt;= yb(i) ...
            &amp; y &lt; yb(i)+dif));
    end
    C = reshape(C,size(xb));
    % smooth a little for better results
    smooth = @(A,L) ((eye(size(A,1)) + ...
        L ^ 2 * diff(eye(size(A,1)),2)' * diff(eye(size(A,1)),2) + ...
        2 * L * diff(eye(size(A,1)))' * diff(eye(size(A,1)))) \ A);
    D = smooth(smooth(C,0.1)',0.1)';
    
    % let's draw it :)
    set(figure,'Position',[0 0 300 400],'Color',[1 1 1])
    movegui(gcf,'center')
    set(surf(xb,yb,zeros(size(D)),D),'ZData',xb.*0-0.01);
    view(2); shading flat; grid off; axis off equal;
    colormap([1 1 1; pink(19)])
    hold on
    t = linspace(0,2*pi,50);
    % bowtie
    set(fill((sin(t))/9+0.5,(sin(2*t))/18+0.65,[0 .8 0]),...
        'EdgeAlpha',0)
    set(fill((sin(t))/30+0.5,(cos(t))/30+0.65,[0 .9 0]),...
        'EdgeAlpha',0)
    t = linspace(0,2*pi,5);
    % buttons
    set(fill((sin(t))/20+0.5,(cos(t))/20+0.5,[.8 0 0]),'EdgeAlpha',0)
    set(fill((sin(t))/20+0.5,(cos(t))/20+0.3,[.8 0 0]),'EdgeAlpha',0)
    % sugar
    plot3(r(1:10:end,1),r(1:10:end,2),r(1:10:end,1)*0-0.005,'.',...
        'MarkerSize',3,'MarkerEdgeColor',[1 1 1]);
end

</pre><h3>Thanks for Participating<a name="4"></a></h3>
   <p>I really appreciate all the effort the contributors made.  The decision was tough.  I encourage you all to grab some of the
      contributions from the comments in the previous post and play with them yourselves.
   </p>
   <p>Let me also take this moment to wish all of you a happy, healthy holiday season and new year!</p><script language="JavaScript">
<!--

    function grabCode_1725a6a78fb442b5a429e4b2d6b90442() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='1725a6a78fb442b5a429e4b2d6b90442 ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' 1725a6a78fb442b5a429e4b2d6b90442';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Loren Shure';
        copyright = 'Copyright 2011 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_1725a6a78fb442b5a429e4b2d6b90442()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
1725a6a78fb442b5a429e4b2d6b90442 ##### SOURCE BEGIN #####
%% Ginger Plot Winner
% If you've been following the comments from my
% <http://blogs.mathworks.com/loren/2011/12/07/pretty-2-dimensional-chaotic-maps/
% recent post> on chaotic maps, you know that there were several entries to
% the challenge of coming up with an interesting visualization.  The
% entries are each interesting, displaying schemes, including an animation,
% and extra information placed on the plots in various ways. I have
% conferred with a few colleagues here and we have chosen the contribution
% from Rafael Oliveira as the winning entry.  Why?  Because the code is
% interesting, he made interesting use interesting of some functions, the
% and the plot is cute (at least for
% <http://www.mathworks.com/help/releases/R2011b/techdoc/ref/rng.html
% |rng(42)|>.
%% Adorned Ginger Man
% Here's Rafael's adorned gingerman.
gingerR
%%
% Here's a comment from Rafael for this submission: 
%
% _I decided to make a
% pixelated gingerbread man with the output of the chaotic map, using a
% lemniscate of Gerono as his green bow tie :)_
%% The Code
% I took the liberty of adding a small number of comments to Rafael's code
% to highlight some parts of the plot.
type gingerR
%% Thanks for Participating
% I really appreciate all the effort the contributors made.  The decision
% was tough.  I encourage you all to grab some of the contributions from
% the comments in the previous post and play with them yourselves.
%
% Let me also take this moment to wish all of you a happy, healthy holiday
% season and new year!



##### SOURCE END ##### 1725a6a78fb442b5a429e4b2d6b90442
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/a7CrViPL6dU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2011/12/20/ginger-plot-winner/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2011/12/20/ginger-plot-winner/</feedburner:origLink></item>
		<item>
		<title>Pretty 2-Dimensional Chaotic Maps</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/20D5mHsPxmg/</link>
		<comments>http://blogs.mathworks.com/loren/2011/12/07/pretty-2-dimensional-chaotic-maps/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 14:44:02 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2011/12/07/pretty-2-dimensional-chaotic-maps/</guid>
		<description><![CDATA[Chaos theory has found uses across a broad set of scientific fields to explain some complicated observed behavior. In geophysics, my background, it can help explain the reversals of Earth's magnetic field, for example. Today I thought I'd share a chaotic system, called Gingerbreadman maps, whose equations make the system seem simple. That is, until [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p><a href="http://en.wikipedia.org/wiki/Chaos_theory">Chaos theory</a> has found uses across a broad set of scientific fields to explain some complicated observed behavior.  In geophysics, my
         background, it can help explain the reversals of Earth's magnetic field, for example.  Today I thought I'd share a chaotic
         system, called Gingerbreadman maps, whose equations make the system seem simple.  That is, until you do some simulations.
      </p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">Gingerbreadman Map Equations</a></li>
         <li><a href="#2">Plot Results</a></li>
         <li><a href="#7">Code Listing</a></li>
         <li><a href="#8">References</a></li>
         <li><a href="#9">Make the Plot Prettier!</a></li>
      </ul>
   </div>
   <h3>Gingerbreadman Map Equations<a name="1"></a></h3>
   <p>The equations for the gingerbreadman map look simple enough.  For any given point in space: <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/299/ginger_eq74699.png"> , define the next point in the sequence by <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/299/ginger_eq01962.png"> .  I now show the same equations in this MATLAB code, accounting for some initial values <tt>x0,y0</tt> to seed the calculation.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">dbtype <span style="color: #A020F0">17:25</span> <span style="color: #A020F0">gingerbreadman</span></pre><pre style="font-style:oblique">
17    
18    % main calculation
19    for i = 1:n
20        if i == 1
21            x(i) = 1 - y0 + abs(x0);
22            y(i) = x0;
23        else
24            x(i) = 1 - y(i-1) + abs(x(i-1));
25            y(i) = x(i-1);

</pre><h3>Plot Results<a name="2"></a></h3>
   <p>I want to show several results from different starting values for <tt>x0,y0</tt>.  First I'll set the random number generator seed for repeatable results.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">rng(42)
gingerbreadman</pre><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/299/ginger_01.png"> <pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">gingerbreadman</pre><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/299/ginger_02.png"> <pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">gingerbreadman</pre><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/299/ginger_03.png"> <pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">gingerbreadman</pre><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/299/ginger_04.png"> <pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">gingerbreadman</pre><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/299/ginger_05.png"> <h3>Code Listing<a name="7"></a></h3>
   <p>Here's the full code listing for <tt>gingerbreadman</tt>.  You can see it allows you to specify the initial conditions, and return points and initial conditions should you choose.
       With no output arguments, <tt>gingerbreadman</tt> creates plots like you've been seeing.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">type <span style="color: #A020F0">gingerbreadman</span></pre><pre style="font-style:oblique">
function [xout,yout, x0, y0] = gingerbreadman(x0,y0)
%  Gingerbreadman map producing a chaotic 2-D map.

%  Copyright 2011 The MathWorks, Inc.

% if not enough inputs, assign random numbers
if nargin &lt; 2
    x0 = randn();
    y0 = randn();
end

% iteration counter
n = 10000;

x = zeros(n,1);
y = zeros(n,1);

% main calculation
for i = 1:n
    if i == 1
        x(i) = 1 - y0 + abs(x0);
        y(i) = x0;
    else
        x(i) = 1 - y(i-1) + abs(x(i-1));
        y(i) = x(i-1);
    end
end

% if output is requested, return gingerbread x,y values and
% x0, y0 initial conditions
%
% otherwise plot results
if nargout &gt; 0
    xout = x;
    yout = y;
else
    scatter(x, y, '.');
end
</pre><h3>References<a name="8"></a></h3>
   <div>
      <ul>
         <li><a href="http://en.wikipedia.org/wiki/Gingerbreadman_map">Wikipedia</a></li>
         <li><a href="http://mathworld.wolfram.com/GingerbreadmanMap.html">Weisstein, Eric W. "Gingerbreadman Map." From MathWorld--A Wolfram Web Resource.</a></li>
      </ul>
   </div>
   <h3>Make the Plot Prettier!<a name="9"></a></h3>
   <p>Instead of using the plotting function <tt>scatter</tt>, post your thoughts (in code) <a href="http://blogs.mathworks.com/loren/?p=299#respond">here</a> for a chance to win some MATLAB bling.  I will look at entries up through <del datetime="2011-12-07T20:23:03+00:00">(was: Sunday, November 27, 2011)</del> Wednesday December 14, 2011 and announce the winner
      (the one whose plot I find most interesting) shortly after that.
   </p><script language="JavaScript">
<!--

    function grabCode_5509e995bb114dbb93da9de1c568fa72() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='5509e995bb114dbb93da9de1c568fa72 ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' 5509e995bb114dbb93da9de1c568fa72';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Loren Shure';
        copyright = 'Copyright 2011 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_5509e995bb114dbb93da9de1c568fa72()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
5509e995bb114dbb93da9de1c568fa72 ##### SOURCE BEGIN #####
%% Pretty 2-Dimensional Chaotic Maps
% <http://en.wikipedia.org/wiki/Chaos_theory Chaos theory> has found uses
% across a broad set of scientific fields to explain some complicated
% observed behavior.  In geophysics, my background, it can help explain the
% reversals of Earth's magnetic field, for example.  Today I thought I'd
% share a chaotic system, called Gingerbreadman maps, whose equations make
% the system seem simple.  That is, until you do some simulations.
%% Gingerbreadman Map Equations
% The equations for the gingerbreadman map look simple enough.  For any
% given point in space: $x_{k},y_{k}$, define the next point in the
% sequence by $x_{k+1}=1-y_k+|x_k|,y_{k+1}=x_k$.  I now show the same
% equations in this MATLAB code, accounting for some initial values |x0,y0|
% to seed the calculation.
dbtype 17:25 gingerbreadman
%% Plot Results
% I want to show several results from different starting values for
% |x0,y0|.  First I'll set the random number generator seed for repeatable
% results.
rng(42)
gingerbreadman
%%
gingerbreadman
%%
gingerbreadman
%%
gingerbreadman
%%
gingerbreadman
%% Code Listing
% Here's the full code listing for |gingerbreadman|.  You can see it allows
% you to specify the initial conditions, and return points and initial
% conditions should you choose.  With no output arguments, |gingerbreadman|
% creates plots like you've been seeing.
type gingerbreadman
%% References
% * <http://en.wikipedia.org/wiki/Gingerbreadman_map Wikipedia>
% * <http://mathworld.wolfram.com/GingerbreadmanMap.html Weisstein, Eric W.
% "Gingerbreadman Map." From MathWorldREPLACE_WITH_DASH_DASHA Wolfram Web Resource.>
%% Make the Plot Prettier!
% Instead of using the plotting function |scatter|, post your thoughts (in
% code) <http://blogs.mathworks.com/loren/?p=299#respond here> for a chance
% to win some MATLAB bling.  I will look at entries up through Sunday,
% November 27, 2011 and announce the winner (the one whose plot I find most
% interesting) shortly after that.
##### SOURCE END ##### 5509e995bb114dbb93da9de1c568fa72
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/20D5mHsPxmg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2011/12/07/pretty-2-dimensional-chaotic-maps/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2011/12/07/pretty-2-dimensional-chaotic-maps/</feedburner:origLink></item>
		<item>
		<title>Subset Selection and Regularization (Part 2)</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/qbJ08EugGnc/</link>
		<comments>http://blogs.mathworks.com/loren/2011/11/29/subset-selection-and-regularization-part-2/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 14:41:05 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[Curve fitting]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2011/11/29/subset-selection-and-regularization-part-2/</guid>
		<description><![CDATA[This week Richard Willey from technical marketing will finish his two part presentation on subset selection and regularization. In a recent posting, we examined how to use sequential feature selection to improve predictive accuracy when modeling wide data sets with highly correlated variables. This week, we're going to solve the same problems using regularization algorithms [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p><i>This week Richard Willey from technical marketing will finish his two part presentation on subset selection and regularization.</i></p>
      <p>In a <a href="http://blogs.mathworks.com/loren/2011/11/21/subset-selection-and-regularization/">recent posting</a>, we examined how to use sequential feature selection to improve predictive accuracy when modeling wide data sets with highly
         correlated variables.  This week, we're going to solve the same problems using regularization algorithms such as lasso, the
         elastic net, and ridge regression.  Mathematically, these algorithms work by penalizing the size of the regression coefficients
         in the model.
      </p>
      <p>Standard linear regression works by estimating a set of coefficients that minimize the sum of the squared error between the
         observed values and the fitted values from the model.  Regularization techniques like ridge regression, lasso, and the elastic
         net introduce an additional term to this minimization problem.
      </p>
      <div>
         <ul>
            <li>Ridge regression identifies a set of regression coefficients that minimize the sum of the squared errors plus the sum of the
               squared regression coefficients multiplied by a weight parameter <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png"> . <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  can take any value between zero and one.  A <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  value of zero is equivalent to a standard linear regression.  As <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  increases in size, regression coefficients shrink towards zero.
            </li>
            <li>Lasso minimizes the sum of the squared errors plus the sum of the absolute value of the regression coefficients.</li>
            <li>The elastic net is a weighted average of the lasso and the ridge solutions.</li>
         </ul>
      </div>
      <p>The introduction of this additional term forces the regression coefficients towards zero generating a simpler model with greater
         predictive accuracy.
      </p>
      <p>Let's see regularization in action by using lasso to solve the same problem we looked at last week.</p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">Recreate Data Set 1 from the Previous Post</a></li>
         <li><a href="#2">Use Lasso to Fit the Model</a></li>
         <li><a href="#4">Create a Plot Showing Mean Square Error Versus Lambda</a></li>
         <li><a href="#5">Use the Stats Structure to Extract a Set of Model Coefficients.</a></li>
         <li><a href="#6">Run a Simulation</a></li>
         <li><a href="#8">Choosing the Best Technique</a></li>
         <li><a href="#9">Conclusion</a></li>
      </ul>
   </div>
   <h3>Recreate Data Set 1 from the Previous Post<a name="1"></a></h3><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">clear <span style="color: #A020F0">all</span>
clc
rng(1998);
mu = [0 0 0 0 0 0 0 0];
i = 1:8;
matrix = abs(bsxfun(@minus,i',i));
covariance = repmat(.5,8,8).^matrix;
X = mvnrnd(mu, covariance, 20);
Beta = [3; 1.5; 0; 0; 2; 0; 0; 0];
ds = dataset(Beta);
Y = X * Beta + 3 * randn(20,1);
b = regress(Y,X);
ds.Linear = b;</pre><h3>Use Lasso to Fit the Model<a name="2"></a></h3>
   <p>The syntax for the <a href="http://www.mathworks.com/help/toolbox/stats/lasso.html"><tt>lasso</tt></a> command is very similar to that used by linear regression. In this line of code, I am going estimate a set of coefficients
      <tt>B</tt> that models <tt>Y</tt> as a function of <tt>X</tt>.  To avoid over fitting, I'm going to apply five-fold cross validation.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">[B Stats] = lasso(X,Y, <span style="color: #A020F0">'CV'</span>, 5);</pre><p>When we perform a linear regression, we generate a single set of regression coefficients.  By default <tt>lasso</tt> will create 100 different models.  Each model was estimated using a slightly larger <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png"> . All of the model coefficients are stored in array <tt>B</tt>.  The rest of the information about the model is stored in a structure named <tt>Stats</tt>.
   </p>
   <p>Let's look at the first five sets of coefficients inside of <tt>B</tt>.  As you traverse the rows you can see that as <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  increases, the value model coefficients are usually shrinking towards zero.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">disp(B(:,1:5))
disp(Stats)</pre><pre style="font-style:oblique">       3.9147       3.9146       3.9145       3.9143       3.9142
      0.13502      0.13498      0.13494      0.13488      0.13482
      0.85283      0.85273      0.85262      0.85247      0.85232
     -0.92775     -0.92723      -0.9267       -0.926     -0.92525
       3.9415       3.9409       3.9404       3.9397       3.9389
      -2.2945       -2.294      -2.2936       -2.293      -2.2924
       1.3566       1.3567       1.3568       1.3569       1.3571
     -0.14796     -0.14803      -0.1481     -0.14821     -0.14833
         Intercept: [1x100 double]
            Lambda: [1x100 double]
             Alpha: 1
                DF: [1x100 double]
               MSE: [1x100 double]
    PredictorNames: {}
                SE: [1x100 double]
      LambdaMinMSE: 0.585
         Lambda1SE: 1.6278
       IndexMinMSE: 78
          Index1SE: 89
</pre><h3>Create a Plot Showing Mean Square Error Versus Lambda<a name="4"></a></h3>
   <p>The natural question to ask at this point in time is "OK, which of these 100 different models should I use?".  We can answer
      that question using <a href="http://www.mathworks.com/help/toolbox/stats/lassoplot.html"><tt>lassoPlot</tt></a>. <tt>lassoPlot</tt> generates a plot that displays the relationship between <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  and the cross validated mean square error (MSE) of the resulting model.  Each of the red dots shows the MSE for the resulting
      model.  The vertical line segments stretching out from each dot are error bars for each estimate.
   </p>
   <p>You can also see a pair of vertical dashed lines.  The line on the right identifies the <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  value that minimizes the cross validated MSE. The line on the left indicates the highest value of <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  whose MSE is within one standard error of the minimum MSE.  In general, people will chose the <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  that minimizes the MSE.  On occasion, if a more parsimonious model is considered particularly advantageous, a user might
      choose some other <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  value that falls between the two line segments.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">lassoPlot(B, Stats, <span style="color: #A020F0">'PlotType'</span>, <span style="color: #A020F0">'CV'</span>)</pre><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_01.png"> <h3>Use the Stats Structure to Extract a Set of Model Coefficients.<a name="5"></a></h3>
   <p>The <img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/298/Regularization_eq40602.png">  value that minimizes the MSE is stored in the <tt>Stats</tt> structure.  You can use this information to index into <tt>Beta</tt> and extract the set of coefficients that minimize the MSE.
   </p>
   <p>Much as in the feature selection example, we can see that the lasso algorithm has eliminated four of the five distractors
      from the resulting model.  This new, more parsimonious model will be significantly more accurate for prediction than a standard
      linear regression.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">ds.Lasso = B(:,Stats.IndexMinMSE);
disp(ds)</pre><pre style="font-style:oblique">    Beta    Linear      Lasso   
      3       3.5819      3.0591
    1.5      0.44611      0.3811
      0      0.92272    0.024131
      0     -0.84134           0
      2       4.7091      1.5654
      0      -2.5195           0
      0      0.17123      1.3499
      0     -0.42067           0
</pre><h3>Run a Simulation<a name="6"></a></h3>
   <p>Here, once again, it's very dangerous to base any kind of analysis on a single observation.  Let's use a simulation to compare
      the accuracy of a linear regression with the lasso.  We'll start by preallocating some variables.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">MSE = zeros(100,1);
mse = zeros(100,1);
Coeff_Num = zeros(100,1);
Betas = zeros(8,100);
cv_Reg_MSE = zeros(1,100);</pre><p>Next, we'll generate 100 different models and estimate the number of coefficients contained in the lasso model as well as
      the difference in the cross validated MSE between a standard linear regression and the lasso model.
   </p>
   <p>As you can see, on average, the lasso model only contains 4.5 terms (the standard linear regression model includes 8).  More
      importantly, the cross validated MSE for the linear regression model is about 30% larger than that generated from the <tt>lasso</tt>.  This is an incredibly powerful results.  The <tt>lasso</tt> algorithm is every bit as easy to apply as standard linear regression, however, it offers significant improvements in predictive
      accuracy compared to regression.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">rng(1998);

<span style="color: #0000FF">for</span> i = 1 : 100

    X = mvnrnd(mu, covariance, 20);
    Y = X * Beta + randn(20,1);

    [B Stats] = lasso(X,Y, <span style="color: #A020F0">'CV'</span>, 5);
    Betas(:,i) = B(:,Stats.IndexMinMSE) &gt; 0;
    Coeff_Num(i) = sum(B(:,Stats.IndexMinMSE) &gt; 0);
    MSE(i) = Stats.MSE(:, Stats.IndexMinMSE);

    regf = @(XTRAIN, ytrain, XTEST)(XTEST*regress(ytrain,XTRAIN));
    cv_Reg_MSE(i) = crossval(<span style="color: #A020F0">'mse'</span>,X,Y,<span style="color: #A020F0">'predfun'</span>,regf, <span style="color: #A020F0">'kfold'</span>, 5);

<span style="color: #0000FF">end</span>

Number_Lasso_Coefficients = mean(Coeff_Num);
disp(Number_Lasso_Coefficients)

MSE_Ratio = median(cv_Reg_MSE)/median(MSE);
disp(MSE_Ratio)</pre><pre style="font-style:oblique">         4.57
       1.2831
</pre><h3>Choosing the Best Technique<a name="8"></a></h3>
   <p>Regularization methods and feature selection techniques both have unique strengths and weaknesses.  Let's close this blog
      posting with some practical guidance regarding pros and cons for the various techniques.
   </p>
   <p>Regularization techniques have two major advantages compared to feature selection.</p>
   <div>
      <ul>
         <li>Regularization techniques are able to operate on much larger datasets than feature selection methods.  Lasso and ridge regression
            can be applied to datasets that contains thousands - even tens of thousands of variables.  Even sequential feature selection
            is usually too slow to cope with this many possible predictors.
         </li>
         <li>Regularization algorithms often generate more accurate predictive models than feature selection.  Regularization operates
            over a continuous space while feature selection operates over a discrete space. As a result, regularization is often able
            to fine tune the model and produce more accurate estimates.
         </li>
      </ul>
   </div>
   <p>However, feature selection methods also have their advantages</p>
   <div>
      <ul>
         <li>Regularization tehcniques are only available for a small number of model types.  Notably, regularization can be applied to
            linear regression and logistic regression.  However, if you're working some other modeling technique - say a boosted decision
            tree - you'll typically need to apply feature selection techiques.
         </li>
         <li>Feature selection is easier to understand and explain to third parties. Never underestimate the importance of being able to
            describe your methods when sharing your results.
         </li>
      </ul>
   </div>
   <p>With this said and done, each of the three regularization techniques also offers its own unique advantages and disadvantages.</p>
   <div>
      <ul>
         <li>Because lasso uses an L1 norm it tends to force individual coefficient values completely towards zero.  As a result, lasso
            works very well as a feature selection algorithm.  It quickly identifies a small number of key variables.
         </li>
         <li>In contrast, ridge regression uses an L2 norm for the coefficients (you're minimizing the sum of the squared errors).  Ridge
            regression tends to spread coefficient shrinkage across a larger number of coefficients.  If you think that your model should
            contain a large number of coefficients, ridge regression is probably a better choice than lasso.
         </li>
         <li>Last, but not least, we have the elastic net which is able to compensate for a very specific limitation of lasso.  Lasso is
            unable to identify more predictors than you have coefficients.
         </li>
      </ul>
   </div>
   <p>Let's assume that you are running a cancer research study.</p>
   <div>
      <ul>
         <li>You have genes sequences for 500 different cancer patients</li>
         <li>You're trying to determine which of 15,000 different genes have a signficant impact on the progression of the disease.</li>
      </ul>
   </div>
   <p>Sequential feature selection is completely impractical with this many different variables.  You can't use ridge regression
      because it won't force coefficients completely to zero quickly enough. At the same time, you can't use lasso since you might
      need to identify more than 500 different genes.  The elastic net is one possible solution.
   </p>
   <h3>Conclusion<a name="9"></a></h3>
   <p>If you'd like more information on this topic there is a MathWork's webinar titled <a href="http://www.mathworks.com/company/events/webinars/wbnr59911.html?id=59911&amp;p1=923401052&amp;p2=923401070">Computational Statistics:  Feature Selection, Regularization, and Shrinkage</a> which provides a more detailed treatment of these topics.
   </p>
   <p>In closing, I'd like to ask you whether any of you have practical examples applying feature selection or regularization algorithms
      in your work?
   </p>
   <div>
      <ul>
         <li>Have you ever used feature selection?</li>
         <li>Do you see an opportunity to apply lasso or ridge regression in your work?</li>
      </ul>
   </div>
   <p>If so, please post here <a href="http://blogs.mathworks.com/loren/?p=298#respond">here</a>.
   </p><script language="JavaScript">
<!--

    function grabCode_70800e67086e4d37aeee1ea03b81b69f() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='70800e67086e4d37aeee1ea03b81b69f ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' 70800e67086e4d37aeee1ea03b81b69f';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Richar Willey';
        copyright = 'Copyright 2011 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_70800e67086e4d37aeee1ea03b81b69f()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
70800e67086e4d37aeee1ea03b81b69f ##### SOURCE BEGIN #####
%% Subset Selection and Regularization (Part 2) 
% _This week Richard Willey from technical marketing will finish his two
% part presentation on subset selection and regularization._  
% 
% In a
% <http://blogs.mathworks.com/loren/2011/11/21/subset-selection-and-regularization/
% recent posting>, we examined how to use sequential feature selection to
% improve predictive accuracy when modeling wide data sets with highly
% correlated variables.  This week, we're going to solve the same problems
% using regularization algorithms such as lasso, the elastic net, and ridge
% regression.  Mathematically, these algorithms work by penalizing the size
% of the regression coefficients in the model.
%
% Standard linear regression works by estimating a set of coefficients
% that minimize the sum of the squared error between the observed 
% values and the fitted values from the model.  Regularization techniques 
% like ridge regression, lasso, and the elastic net introduce an 
% additional term to this minimization problem.  
%
% * Ridge regression identifies a set of regression coefficients that
% minimize the sum of the squared errors plus the sum of the squared
% regression coefficients multiplied by a weight parameter $$ \lambda $$.
% $$ \lambda $$ can take any value between zero and one.  A $$ \lambda $$
% value of zero is equivalent to a standard linear regression.  As $$
% \lambda $$ increases in size, regression coefficients shrink towards
% zero.
% * Lasso minimizes the sum of the squared errors plus the sum of the
% absolute value of the regression coefficients.
% * The elastic net is a weighted average of the lasso and the ridge
% solutions.
%
% The introduction of this additional term forces the regression
% coefficients towards zero generating a simpler model with greater
% predictive accuracy.  
%
% Let's see regularization in action by using lasso to solve the same
% problem we looked at last week.
 
%% Recreate Data Set 1 from the Previous Post  
 
clear all
clc
rng(1998);
mu = [0 0 0 0 0 0 0 0];
i = 1:8;
matrix = abs(bsxfun(@minus,i',i));
covariance = repmat(.5,8,8).^matrix;
X = mvnrnd(mu, covariance, 20);
Beta = [3; 1.5; 0; 0; 2; 0; 0; 0];
ds = dataset(Beta);
Y = X * Beta + 3 * randn(20,1);
b = regress(Y,X);
ds.Linear = b;
 
%% Use Lasso to Fit the Model
% The syntax for the
% <http://www.mathworks.com/help/toolbox/stats/lasso.html |lasso|> command
% is very similar to that used by linear regression. In this line of code,
% I am going estimate a set of coefficients |B| that models |Y| as a
% function of |X|.  To avoid over fitting, I'm going to apply five-fold
% cross validation.
[B Stats] = lasso(X,Y, 'CV', 5);
 
%%
% When we perform a linear regression, we generate a single set of
% regression coefficients.  By default |lasso| will create 100 different
% models.  Each model was estimated using a slightly larger $$ \lambda $$. 
% All of the model coefficients are stored in array |B|.  The rest of the
% information about the model is stored in a structure named |Stats|.
%
% Let's look at the first five sets of coefficients inside of |B|.  As you
% traverse the rows you can see that as $$ \lambda $$ increases, the value 
% model coefficients are usually shrinking towards zero.
disp(B(:,1:5))
disp(Stats)
 
%% Create a Plot Showing Mean Square Error Versus |Lambda|
% The natural question to ask at this point in time is "OK, which of these
% 100 different models should I use?".  We can answer that question using
% <http://www.mathworks.com/help/toolbox/stats/lassoplot.html |lassoPlot|>.
% |lassoPlot| generates a plot that displays the relationship between $$
% \lambda $$ and the cross validated mean square error (MSE) of the
% resulting model.  Each of the red dots shows the MSE for the resulting
% model.  The vertical line segments stretching out from each dot are error
% bars for each estimate.
%
% You can also see a pair of vertical dashed lines.  The line on the right
% identifies the $$ \lambda $$ value that minimizes the cross validated
% MSE. The line on the left indicates the highest value of $$ \lambda $$
% whose MSE is within one standard error of the minimum MSE.  In general,
% people will chose the $$ \lambda $$ that minimizes the MSE.  On occasion,
% if a more parsimonious model is considered particularly advantageous, a
% user might choose some other $$ \lambda $$ value that falls between the
% two line segments.
lassoPlot(B, Stats, 'PlotType', 'CV')
 
%% Use the Stats Structure to Extract a Set of Model Coefficients.
% The $$ \lambda $$ value that minimizes the MSE is stored in the
% |Stats| structure.  You can use this information to index into |Beta| and
% extract the set of coefficients that minimize the MSE.
%
% Much as in the feature selection example, we can see that the lasso
% algorithm has eliminated four of the five distractors from the resulting
% model.  This new, more parsimonious model will be significantly more
% accurate for prediction than a standard linear regression.
%
ds.Lasso = B(:,Stats.IndexMinMSE);
disp(ds)
 
%%  Run a Simulation
% Here, once again, it's very dangerous to base any kind of analysis on a
% single observation.  Let's use a simulation to compare the accuracy of a
% linear regression with the lasso.  We'll start by preallocating some
% variables.
MSE = zeros(100,1);
mse = zeros(100,1);
Coeff_Num = zeros(100,1);
Betas = zeros(8,100);
cv_Reg_MSE = zeros(1,100);
 
%%
% Next, we'll generate 100 different models and estimate the number of
% coefficients contained in the lasso model as well as the difference in
% the cross validated MSE between a standard linear regression and the
% lasso model.
%
% As you can see, on average, the lasso model only contains 4.5 terms (the
% standard linear regression model includes 8).  More importantly, the
% cross validated MSE for the linear regression model is about 30% larger
% than that generated from the |lasso|.  This is an incredibly powerful
% results.  The |lasso| algorithm is every bit as easy to apply as standard
% linear regression, however, it offers significant improvements in
% predictive accuracy compared to regression.
 
rng(1998);
 
for i = 1 : 100
    
    X = mvnrnd(mu, covariance, 20);
    Y = X * Beta + randn(20,1);
    
    [B Stats] = lasso(X,Y, 'CV', 5);
    Betas(:,i) = B(:,Stats.IndexMinMSE) > 0;
    Coeff_Num(i) = sum(B(:,Stats.IndexMinMSE) > 0);
    MSE(i) = Stats.MSE(:, Stats.IndexMinMSE);
    
    regf = @(XTRAIN, ytrain, XTEST)(XTEST*regress(ytrain,XTRAIN));
    cv_Reg_MSE(i) = crossval('mse',X,Y,'predfun',regf, 'kfold', 5);
        
end
 
Number_Lasso_Coefficients = mean(Coeff_Num);
disp(Number_Lasso_Coefficients)
 
MSE_Ratio = median(cv_Reg_MSE)/median(MSE);
disp(MSE_Ratio)

%% Choosing the Best Technique
% Regularization methods and feature selection techniques both have 
% unique strengths and weaknesses.  Let's close this blog posting with 
% some practical guidance regarding pros and cons for the various
% techniques.
%
% Regularization techniques have two major advantages compared to feature
% selection.
%
% * Regularization techniques are able to operate on much larger datasets 
% than feature selection methods.  Lasso and ridge regression can be
% applied to datasets that contains thousands - even tens of thousands of
% variables.  Even sequential feature selection is usually too slow to cope 
% with this many possible predictors.
% * Regularization algorithms often generate more accurate predictive
% models than feature selection.  Regularization operates over a
% continuous space while feature selection operates over a discrete space.
% As a result, regularization is often able to fine tune the model and
% produce more accurate estimates.
%
% However, feature selection methods also have their advantages
%
% * Regularization tehcniques are only available for a small number of model
% types.  Notably, regularization can be applied to linear regression and
% logistic regression.  However, if you're working some other modeling
% technique - say a boosted decision tree - you'll typically need to apply
% feature selection techiques.
% * Feature selection is easier to understand and explain to third parties.
% Never underestimate the importance of being able to describe your methods
% when sharing your results.
%
% With this said and done, each of the three regularization techniques also
% offers its own unique advantages and disadvantages.
%
% * Because lasso uses an L1 norm it tends to force individual coefficient
% values completely towards zero.  As a result, lasso works very well as a
% feature selection algorithm.  It quickly identifies a small number of key
% variables.
% * In contrast, ridge regression uses an L2 norm for the coefficients
% (you're minimizing the sum of the squared errors).  Ridge regression
% tends to spread coefficient shrinkage across a larger number of
% coefficients.  If you think that your model should contain a large number
% of coefficients, ridge regression is probably a better choice than lasso.
% * Last, but not least, we have the elastic net which is able to
% compensate for a very specific limitation of lasso.  Lasso is unable to
% identify more predictors than you have coefficients.  
% 
% Let's assume that you are running a cancer research study.  
%
% * You have genes sequences for 500 different cancer patients
% * You're trying to determine which of 15,000 different genes have a
% signficant impact on the progression of the disease.
%
% Sequential feature selection is completely impractical with this 
% many different variables.  You can't use ridge regression because 
% it won't force coefficients completely to zero quickly enough. 
% At the same time, you can't use lasso since you might need to identify 
% more than 500 different genes.  The elastic net is one possible solution.

%% Conclusion
% If you'd like more information on this topic there is a MathWork's
% webinar titled
% <http://www.mathworks.com/company/events/webinars/wbnr59911.html?id=59911&#038;p1=923401052&#038;p2=923401070
% Computational Statistics:  Feature Selection, Regularization, and
% Shrinkage> which provides a more detailed treatment of these topics.
%
% In closing, I'd like to ask you whether any of you have practical
% examples applying feature selection or regularization algorithms in your
% work?
%
% * Have you ever used feature selection?  
% * Do you see an opportunity to apply lasso or ridge regression in your
% work?
% 
% If so, please post here <http://blogs.mathworks.com/loren/?p=298#respond here>.




##### SOURCE END ##### 70800e67086e4d37aeee1ea03b81b69f
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/qbJ08EugGnc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2011/11/29/subset-selection-and-regularization-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2011/11/29/subset-selection-and-regularization-part-2/</feedburner:origLink></item>
		<item>
		<title>Subset Selection and Regularization</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/7k76ciQ82R4/</link>
		<comments>http://blogs.mathworks.com/loren/2011/11/21/subset-selection-and-regularization/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 15:55:34 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[Curve fitting]]></category>
		<category><![CDATA[Statistics]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2011/11/21/subset-selection-and-regularization/</guid>
		<description><![CDATA[This week Richard Willey from technical marketing will be guest blogging about subset selection and regularization. This week's blog posting is motivated by a pair of common challenges that occur in applied curve fitting. The first challenge is how best to create accurate predictive models when your independent variables exhibit strong correlation. In many cases [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p><i>This week Richard Willey from technical marketing will be guest blogging about subset selection and regularization.</i> This week's blog posting is motivated by a pair of common challenges that occur in applied curve fitting.
      </p>
      <div>
         <ul>
            <li>The first challenge is how best to create accurate predictive models when your independent variables exhibit strong correlation.
                In many cases you can improve upon the results of an ordinary least square regression if you reduce the number of predictors
               or, alternatively, shrink the coefficient values towards zero.
            </li>
            <li>The second challenge involves generating an accurate predictive model when you are constrained with respect to the number
               of predictors you are working with.  For example, assume that you need to embed your model onto a controller.  You have 20
               possible predictors to chose from, but you only have enough memory to allow for 8 independent variables.
            </li>
         </ul>
      </div>
      <p>This blog post will show two different sets of techniques to solve these related challenges.  The first set of techniques
         are based on a combination of feature selection and cross validation.  The second set of techniques are use regularization
         algorithms like ridge regression, lasso and the elastic net. All of these algorithms can be found in <a href="http://www.mathworks.com/products/statistics/">Statistics Toolbox</a>.
      </p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">Create a Dataset</a></li>
         <li><a href="#13">Compare the Results</a></li>
         <li><a href="#15">Perform a Simulation</a></li>
         <li><a href="#18">Introducing Sequential Feature Selection</a></li>
         <li><a href="#19">Generate a Data Set</a></li>
         <li><a href="#25">Run a Simulation</a></li>
      </ul>
   </div>
   <h3>Create a Dataset<a name="1"></a></h3>
   <p>To start with, I am going to generate a dataset that is deliberately engineered to be very difficult to fit using traditional
      linear regression.  The dataset has three characteristics that present challenges of linear regression
   </p>
   <div>
      <ol>
         <li>The dataset is very "wide".  There are relatively large numbers of (possible) predictors compared to the number of observations.</li>
         <li>The predictors are strongly correlated with one another</li>
         <li>There is a large amount of noise in the dataset</li>
      </ol>
   </div><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">clear <span style="color: #A020F0">all</span>
clc
rng(1998);</pre><p>Create eight <tt>X</tt> variables. The mean of each variable will be equal to zero.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">mu = [0 0 0 0 0 0 0 0];</pre><p>The variables are correlated with one another. The covariance matrix is specified as</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">i = 1:8;
matrix = abs(bsxfun(@minus,i',i));
covariance = repmat(.5,8,8).^matrix;</pre><p>Use these parameters to generate a set of multivariate normal random numbers.</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">X = mvnrnd(mu, covariance, 20);</pre><p>Create a hyperplane that describes <tt>Y = f(X)</tt> and add in a noise vector. Note that only three of the eight predictors have coefficient values &gt; 0. This means that five
      of the eight predictors have zero impact on the actual model.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">Beta = [3; 1.5; 0; 0; 2; 0; 0; 0];
ds = dataset(Beta);
Y = X * Beta + 3 * randn(20,1);</pre><p>For those who are interested, this example was motivated by Robert <a href="http://www-stat.stanford.edu/~tibs/lasso/lasso.pdf">Tibshirani's classic 1996 paper on the lasso</a>:
   </p>
   <p>Tibshirani, R. (1996). Regression shrinkage and selection via the lasso. J. Royal. Statist. Soc B., Vol. 58, No. 1, pages
      267-288).
   </p>
   <p>Let's model <tt>Y = f(X)</tt> using ordinary least squares regression.  We'll start by using traditional linear regression to model <tt>Y = f(X)</tt> and compare the coefficients that were estimated by the regression model to the known vector <tt>Beta</tt>.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">b = regress(Y,X);
ds.Linear = b;
disp(ds)</pre><pre style="font-style:oblique">    Beta    Linear  
      3       3.5819
    1.5      0.44611
      0      0.92272
      0     -0.84134
      2       4.7091
      0      -2.5195
      0      0.17123
      0     -0.42067
</pre><p>As expected, the regression model is performing very poorly.</p>
   <div>
      <ul>
         <li>The regression is estimating coefficient values for all eight predictors.  Objectively, we "know" that five of these coefficients
            should be zero.
         </li>
         <li>Furthermore, the coefficients that are being estimated are very far removed from the true value.</li>
      </ul>
   </div>
   <p>Let's see if we can improve on these results using a technique called "All possible subset regression". First, I'm going to
      generate 255 different regession models which encompass every possible combinate of the eight predictors. Next, I am going
      to measure the accuracy of the resulting regression using the cross validated mean squared error. Finally, I'm going to select
      the regression model that produced the most accurate results and compare this to our original regression model.
   </p>
   <p>Create an index for the regression subsets.</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">index = dec2bin(1:255);
index = index == <span style="color: #A020F0">'1'</span>;
results = double(index);
results(:,9) = zeros(length(results),1);</pre><p>Generate the regression models.</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)"><span style="color: #0000FF">for</span> i = 1:length(index)
    foo = index(i,:);
    rng(1981);
    regf = @(XTRAIN, YTRAIN, XTEST)(XTEST*regress(YTRAIN,XTRAIN));
    results(i,9) = crossval(<span style="color: #A020F0">'mse'</span>, X(:,foo), Y,<span style="color: #A020F0">'kfold'</span>, 5, <span style="color: #A020F0">'predfun'</span>,regf);
<span style="color: #0000FF">end</span></pre><p>Sort the outputs by MSE.  I'll show you the first few.</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">index = sortrows(results, 9);
index(1:10,:)</pre><pre style="font-style:oblique">ans =
  Columns 1 through 5
            1            1            0            0            1
            1            1            0            1            1
            1            0            1            0            1
            1            1            0            0            1
            1            0            0            0            1
            1            1            0            1            1
            1            0            0            1            1
            1            0            0            0            1
            1            0            1            0            1
            1            0            0            1            1
  Columns 6 through 9
            1            0            0       10.239
            1            0            0       10.533
            1            0            0       10.615
            0            0            0       10.976
            1            0            0       11.062
            0            0            0       11.257
            1            0            0       11.641
            0            0            0       11.938
            0            0            0       12.068
            0            0            0       12.249
</pre><h3>Compare the Results<a name="13"></a></h3><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">beta = regress(Y, X(:,logical(index(1,1:8))));
Subset = zeros(8,1);
ds.Subset = Subset;
ds.Subset(logical(index(1,1:8))) = beta;
disp(ds)</pre><pre style="font-style:oblique">    Beta    Linear      Subset 
      3       3.5819     3.4274
    1.5      0.44611     1.1328
      0      0.92272          0
      0     -0.84134          0
      2       4.7091     4.1048
      0      -2.5195    -2.0063
      0      0.17123          0
      0     -0.42067          0
</pre><p>The results are rather striking.</p>
   <div>
      <ul>
         <li>Our new model only includes 4 predictors.  Variables 3,4, 7 and 8 have been eliminated from the regression model.  Furthermore,
            the variables that were exluded are ones that we "know" should be zeroed out.
         </li>
         <li>Equally significant, the coefficients that are being estimated from the resulting regression model are much closer to the
            "true" values.
         </li>
      </ul>
   </div>
   <p>All possible subset regression appears to have generated a significantly better model.  This <tt>R^2</tt> value for this regression model isn't as good as the original linear regression; however, if we're trying to generate predictions
      from a new data set, we'd expect this model to perform significantly better.
   </p>
   <h3>Perform a Simulation<a name="15"></a></h3>
   <p>It's always dangerous to rely on the results of a single observation.  If we were to change either of the noise vectors that
      we used to generate our original dataset we might end up with radically different results. This next block of code will repeat
      our original experiment a hundred times, each time adding in different noise vectors.  Our output will be a bar chart that
      shows how frequently each of the different variables was included in the resulting model.  I'm using parallel computing to
      speed things up though you could perform this (more slowly) in serial.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">matlabpool <span style="color: #A020F0">open</span>

sumin = zeros(1,8);
<span style="color: #0000FF">parfor</span> j=1:100
    mu = [0 0 0 0 0 0 0 0];
    Beta = [3; 1.5; 0; 0; 2; 0; 0; 0];
    i = 1:8;
    matrix = abs(bsxfun(@minus,i',i));
    covariance = repmat(.5,8,8).^matrix;
    X = mvnrnd(mu, covariance, 20);
    Y = X * Beta + 3 * randn(size(X,1),1);

    index = dec2bin(1:255);
    index = index == <span style="color: #A020F0">'1'</span>;
    results = double(index);
    results(:,9) = zeros(length(results),1);

    <span style="color: #0000FF">for</span> k = 1:length(index)
        foo = index(k,:);
        regf = @(XTRAIN, YTRAIN, XTEST)(XTEST*regress(YTRAIN,XTRAIN));
        results(k,9) = crossval(<span style="color: #A020F0">'mse'</span>, X(:,foo), Y,<span style="color: #A020F0">'kfold'</span>, 5, <span style="color: #0000FF">...</span>
            <span style="color: #A020F0">'predfun'</span>,regf);
    <span style="color: #0000FF">end</span>

<span style="color: #228B22">% Sort the outputs by MSE</span>
index = sortrows(results, 9);
sumin = sumin + index(1,1:8);
<span style="color: #0000FF">end</span>  <span style="color: #228B22">% parfor</span>

matlabpool <span style="color: #A020F0">close</span></pre><pre style="font-style:oblique">Starting matlabpool using the 'local4' configuration ... connected to 3 labs.
Sending a stop signal to all the labs ... stopped.
</pre><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)"><span style="color: #228B22">%Plot results.</span>
bar(sumin)</pre><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/297/featureSelection_01.png"> <p>The results clearly show that "All possible subset selection" is doing a really good job identifying the key variables that
      drive our model. Variables 1, 2, and 5 show up in most of the models.  Individual distractors work their way into the models
      as well, however, their frequency is no where near as high as the real predictors.
   </p>
   <h3>Introducing Sequential Feature Selection<a name="18"></a></h3>
   <p>All possible subset selection suffers from one enormous limitation.  It's computationally infeasible to apply this technique
      to datasets that contain large numbers of (potential) predictors.  Our first example had eight possible predictors.  Testing
      all possible subsets required generate (2^8)-1 = 255 different models.  Suppose that we were evaluating a model with 20 possible
      predictors.  All possible subsets would require use to test 1,048,575 different models.  With 40 predictors we'd be looking
      at a billion different combinations.
   </p>
   <p>Sequential feature selection is a more modern approach that tries to define a smart path through the search space.  In turn,
      this should allow us to identify a good set of cofficients but ensure that the problem is still computationally feasible.
   </p>
   <p>Sequential feature selection works as follows:</p>
   <div>
      <ul>
         <li>Start by testing each possible predictor one at a time.  Identify the single predictor that generates the most accurate model.
             This predictor is automatically added to the model.
         </li>
         <li>Next, one at a time, add each of the remaining predictors to a model that includes the single best variable.  Identify the
            variable that improves the accuracy of the model the most.
         </li>
         <li>Test the two models for statistical significance.  If the new model is not significantly more accurate that the original model,
            stop the process.  If, however, the new model is statistically more significant, go and search for the third best variable.
         </li>
         <li>Repeat this process until you can't identify a new variable that has a statistically significant impact on the model.</li>
      </ul>
   </div><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)"><span style="color: #228B22">% This next bit of code will show you how to use sequential feature</span>
<span style="color: #228B22">% selection to solve a significantly larger problem.</span></pre><h3>Generate a Data Set<a name="19"></a></h3><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">clear <span style="color: #A020F0">all</span>
clc
rng(1981);

Z12 = randn(100,40);
Z1 = randn(100,1);
X = Z12 + repmat(Z1, 1,40);
B0 = zeros(10,1);
B1 = ones(10,1);
Beta = 2 * vertcat(B0,B1,B0,B1);
ds = dataset(Beta);

Y = X * Beta + 15*randn(100,1);</pre><p>Use linear regression to fit the model.</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">b = regress(Y,X);
ds.Linear = b;</pre><p>Use sequential feature selection to fit the model.</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">opts = statset(<span style="color: #A020F0">'display'</span>,<span style="color: #A020F0">'iter'</span>);

fun = @(x0,y0,x1,y1) norm(y1-x1*(x0\y0))^2;  <span style="color: #228B22">% residual sum of squares</span>
[in,history] = sequentialfs(fun,X,Y,<span style="color: #A020F0">'cv'</span>,5, <span style="color: #A020F0">'options'</span>,opts)</pre><pre style="font-style:oblique">Start forward sequential feature selection:
Initial columns included:  none
Columns that can not be included:  none
Step 1, added column 26, criterion value 868.909
Step 2, added column 31, criterion value 566.166
Step 3, added column 9, criterion value 425.096
Step 4, added column 35, criterion value 369.597
Step 5, added column 16, criterion value 314.445
Step 6, added column 5, criterion value 283.324
Step 7, added column 18, criterion value 269.421
Step 8, added column 15, criterion value 253.78
Step 9, added column 40, criterion value 242.04
Step 10, added column 27, criterion value 234.184
Step 11, added column 12, criterion value 223.271
Step 12, added column 17, criterion value 216.464
Step 13, added column 14, criterion value 212.635
Step 14, added column 22, criterion value 207.885
Step 15, added column 7, criterion value 205.253
Step 16, added column 33, criterion value 204.179
Final columns included:  5 7 9 12 14 15 16 17 18 22 26 27 31 33 35 40 
in =
  Columns 1 through 12
     0     0     0     0     1     0     1     0     1     0     0     1
  Columns 13 through 24
     0     1     1     1     1     1     0     0     0     1     0     0
  Columns 25 through 36
     0     1     1     0     0     0     1     0     1     0     1     0
  Columns 37 through 40
     0     0     0     1
history = 
      In: [16x40 logical]
    Crit: [1x16 double]
</pre><p>I've configured <tt>sequentialfs</tt> to show each iteration of the feature selection process.  You can see each of the variables as it gets added to the model,
      along with the change in the cross validated mean squared error.
   </p>
   <p>Generate a linear regression using the optimal set of features</p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">beta = regress(Y,X(:,in));

ds.Sequential = zeros(length(ds),1);
ds.Sequential(in) = beta</pre><pre style="font-style:oblique">ds = 
    Beta    Linear      Sequential
    0       -0.74167          0   
    0       -0.66373          0   
    0         1.2025          0   
    0        -1.7087          0   
    0         1.6493     2.5441   
    0         1.3721          0   
    0        -1.6411    -1.1617   
    0        -3.5482          0   
    0         4.1643     2.9544   
    0        -1.9067          0   
    2        0.28763          0   
    2         1.7454     2.9983   
    2         0.1005          0   
    2         2.2226     3.5806   
    2         4.4912     4.0527   
    2         4.7129     5.3879   
    2         0.9606     1.9982   
    2         5.9834     5.1789   
    2         3.9699          0   
    2        0.94454          0   
    0          2.861          0   
    0        -2.1236    -2.4748   
    0        -1.6324          0   
    0         1.8677          0   
    0       -0.51398          0   
    0         3.6856     4.0398   
    0        -3.3103    -4.1396   
    0       -0.98227          0   
    0         1.4756          0   
    0       0.026193          0   
    2         2.9373     3.8792   
    2        0.63056          0   
    2         2.3509     1.9472   
    2         0.8035          0   
    2         2.9242       4.35   
    2         0.4794          0   
    2        -0.6632          0   
    2         0.6066          0   
    2       -0.70758          0   
    2         4.4814     3.8164   
</pre><p>As you can see, <tt>sequentialfs</tt> has generated a much more parsimonious model than the ordinary least squares regression.  We also have a model that should
      generate significantly more accurate predictions.  Moreover, if we needed to (hypothetically) extract the five most important
      variables we could simple grab the output from Steps 1:5.
   </p>
   <h3>Run a Simulation<a name="25"></a></h3>
   <p>As a last step, we'll once again run a simulation to benchmark the technique.  Here, once again, we can see that the feature
      selection technique is doing a very good job identifying the true predictors while screening out the distractors.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">matlabpool <span style="color: #A020F0">open</span>

sumin = 0;
<span style="color: #0000FF">parfor</span> j=1:100
    Z12 = randn(100,40);
    Z1 = randn(100,1);
    X = Z12 + repmat(Z1, 1,40);
    B0 = zeros(10,1);
    B1 = ones(10,1);
    Beta = 2 * vertcat(B0,B1,B0,B1);
    Y = X * Beta + 15*randn(100,1);

    fun = @(x0,y0,x1,y1) norm(y1-x1*(x0\y0))^2;  <span style="color: #228B22">% residual sum of squares</span>
    [in,history] = sequentialfs(fun,X,Y,<span style="color: #A020F0">'cv'</span>,5);

    sumin = in + sumin;
<span style="color: #0000FF">end</span>

bar(sumin)

matlabpool <span style="color: #A020F0">close</span></pre><pre style="font-style:oblique">Starting matlabpool using the 'local4' configuration ... connected to 3 labs.
Sending a stop signal to all the labs ... stopped.
</pre><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/297/featureSelection_02.png"> <p>One last comment:  As you can see, neither <tt>sequentialfs</tt> nor all possible subset selection is perfect.  Neither of these techniques was able to capture all of the true variables
      while excluding the distractors.  It's important to recognize that these data sets were deliberately designed to be difficult
      to fit.  [We don't want to benchmark performance using "easy" problems where any old technique might succeed.]
   </p>
   <p>Next week's blog post will focus on a radically different way to solve these same types of problems.  Stay tuned for a look
      at regularization techniques such as ridge regression, lasso and the elastic net.
   </p>
   <p>Here are a couple questions that you might want to consider: # All possible subset methods are O(2^n).  What is the order
      of complexity of sequential feature selection? # Other than scaling issues, can you think of any possible problems with sequential
      feature selection?  Are there situations where the algorithm might miss important variables?
   </p><script language="JavaScript">
<!--

    function grabCode_e359acae97de460f9c0dbca9011fcebe() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='e359acae97de460f9c0dbca9011fcebe ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' e359acae97de460f9c0dbca9011fcebe';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Richard Willey';
        copyright = 'Copyright 2011 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_e359acae97de460f9c0dbca9011fcebe()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
e359acae97de460f9c0dbca9011fcebe ##### SOURCE BEGIN #####
%% Subset Selection and Regularization  
% _This week Richard Willey from technical marketing will be guest blogging
% about subset selection and regularization._ This week's blog posting is
% motivated by a pair of common challenges that occur in applied curve
% fitting.
%
% * The first challenge is how best to create accurate predictive models when
% your independent variables exhibit strong correlation.  In many cases you
% can improve upon the results of an ordinary least square regression if
% you reduce the number of predictors or, alternatively, shrink the
% coefficient values towards zero.
% * The second challenge involves generating an accurate predictive model
% when you are constrained with respect to the number of predictors you are
% working with.  For example, assume that you need to embed your model onto
% a controller.  You have 20 possible predictors to chose from, but you
% only have enough memory to allow for 8 independent variables.
%
% This blog post will show two different sets of techniques to solve 
% these related challenges.  The first set of techniques are based on a
% combination of feature selection and cross validation.  The second set of
% techniques are use regularization algorithms like ridge regression, lasso
% and the elastic net. All of these algorithms can be found in 
% <http://www.mathworks.com/products/statistics/ Statistics Toolbox>.

%% Create a Dataset
% To start with, I am going to generate a dataset that is deliberately
% engineered to be very difficult to fit using traditional linear
% regression.  The dataset has three characteristics that present
% challenges of linear regression
%
% # The dataset is very "wide".  There are relatively large numbers of
% (possible) predictors compared to the number of observations.
% # The predictors are strongly correlated with one another
% # There is a large amount of noise in the dataset

clear all
clc
rng(1998);

%%
% Create eight |X| variables. The mean of each variable will be equal to
% zero.
mu = [0 0 0 0 0 0 0 0];

%%
% The variables are correlated with one another. The covariance matrix is
% specified as
i = 1:8;
matrix = abs(bsxfun(@minus,i',i));
covariance = repmat(.5,8,8).^matrix;

%%
% Use these parameters to generate a set of multivariate normal 
% random numbers.
X = mvnrnd(mu, covariance, 20);

%%
% Create a hyperplane that describes |Y = f(X)| and add in a noise vector.
% Note that only three of the eight predictors have coefficient values > 0.
% This means that five of the eight predictors have zero impact on the
% actual model.
Beta = [3; 1.5; 0; 0; 2; 0; 0; 0];
ds = dataset(Beta);
Y = X * Beta + 3 * randn(20,1);

%%
% For those who are interested, this example was motivated by Robert
% <http://www-stat.stanford.edu/~tibs/lasso/lasso.pdf Tibshirani's classic
% 1996 paper on the lasso>: 
%   
% Tibshirani, R. (1996). Regression shrinkage and selection via the lasso. 
% J. Royal. Statist. Soc B., Vol. 58, No. 1, pages 267-288).

%% 
% Let's model |Y = f(X)| using ordinary least squares regression.  We'll
% start by using traditional linear regression to model |Y = f(X)| and
% compare the coefficients that were estimated by the regression model to
% the known vector |Beta|.

b = regress(Y,X);
ds.Linear = b;
disp(ds)
%%
% As expected, the regression model is performing very poorly.  
%
% * The regression is estimating coefficient values for all eight
% predictors.  Objectively, we "know" that five of these coefficients
% should be zero.
% * Furthermore, the coefficients that are being estimated are very far
% removed from the true value.
%%
% Let's see if we can improve on these results using a technique called
% "All possible subset regression". First, I'm going to generate 255
% different regession models which encompass every possible combinate of
% the eight predictors. Next, I am going to measure the accuracy of the
% resulting regression using the cross validated mean squared error.
% Finally, I'm going to select the regression model that produced the most
% accurate results and compare this to our original regression model.
%%
% Create an index for the regression subsets.
index = dec2bin(1:255);
index = index == '1';
results = double(index);
results(:,9) = zeros(length(results),1);
%%
% Generate the regression models.
for i = 1:length(index)
    foo = index(i,:);
    rng(1981);
    regf = @(XTRAIN, YTRAIN, XTEST)(XTEST*regress(YTRAIN,XTRAIN));
    results(i,9) = crossval('mse', X(:,foo), Y,'kfold', 5, 'predfun',regf);
end
%%
% Sort the outputs by MSE.  I'll show you the first few.
index = sortrows(results, 9);
index(1:10,:)

%% Compare the Results

beta = regress(Y, X(:,logical(index(1,1:8))));
Subset = zeros(8,1);
ds.Subset = Subset;
ds.Subset(logical(index(1,1:8))) = beta;
disp(ds)

%%
% The results are rather striking.
%
% * Our new model only includes 4 predictors.  Variables 3,4, 7 and 8 have
% been eliminated from the regression model.  Furthermore, the variables
% that were exluded are ones that we "know" should be zeroed out.
% * Equally significant, the coefficients that are being estimated from the
% resulting regression model are much closer to the "true" values.
%
% All possible subset regression appears to have generated a significantly
% better model.  This |R^2| value for this regression model isn't as good
% as the original linear regression; however, if we're trying to generate
% predictions from a new data set, we'd expect this model to perform
% significantly better.

%% Perform a Simulation
% It's always dangerous to rely on the results of a single observation.  If
% we were to change either of the noise vectors that we used to generate
% our original dataset we might end up with radically different results.
% This next block of code will repeat our original experiment a hundred
% times, each time adding in different noise vectors.  Our output will be a
% bar chart that shows how frequently each of the different variables was
% included in the resulting model.  I'm using parallel computing to speed
% things up though you could perform this (more slowly) in serial.

matlabpool open

sumin = zeros(1,8);
parfor j=1:100
    mu = [0 0 0 0 0 0 0 0];
    Beta = [3; 1.5; 0; 0; 2; 0; 0; 0];
    i = 1:8;
    matrix = abs(bsxfun(@minus,i',i));
    covariance = repmat(.5,8,8).^matrix;
    X = mvnrnd(mu, covariance, 20);    
    Y = X * Beta + 3 * randn(size(X,1),1);
    
    index = dec2bin(1:255);
    index = index == '1';
    results = double(index);
    results(:,9) = zeros(length(results),1);
    
    for k = 1:length(index)
        foo = index(k,:);
        regf = @(XTRAIN, YTRAIN, XTEST)(XTEST*regress(YTRAIN,XTRAIN));
        results(k,9) = crossval('mse', X(:,foo), Y,'kfold', 5, ...
            'predfun',regf);
    end
    
% Sort the outputs by MSE
index = sortrows(results, 9);    
sumin = sumin + index(1,1:8);
end  % parfor

matlabpool close
%%
%Plot results.
bar(sumin)
%%
% The results clearly show that "All possible subset selection" is doing a
% really good job identifying the key variables that drive our model.
% Variables 1, 2, and 5 show up in most of the models.  Individual
% distractors work their way into the models as well, however, their
% frequency is no where near as high as the real predictors.

%% Introducing Sequential Feature Selection
% All possible subset selection suffers from one enormous limitation.  It's
% computationally infeasible to apply this technique to datasets that
% contain large numbers of (potential) predictors.  Our first example had
% eight possible predictors.  Testing all possible subsets required
% generate (2^8)-1 = 255 different models.  Suppose that we were evaluating
% a model with 20 possible predictors.  All possible subsets would require
% use to test 1,048,575 different models.  With 40 predictors we'd be looking
% at a billion different combinations.
%
% Sequential feature selection is a more modern approach that tries to
% define a smart path through the search space.  In turn, this should allow
% us to identify a good set of cofficients but ensure that the problem is
% still computationally feasible.
%
% Sequential feature selection works as follows:
%
% * Start by testing each possible predictor one at a time.  Identify the
% single predictor that generates the most accurate model.  This predictor
% is automatically added to the model.
% * Next, one at a time, add each of the remaining predictors to a model
% that includes the single best variable.  Identify the variable that
% improves the accuracy of the model the most.
% * Test the two models for statistical significance.  If the new model is
% not significantly more accurate that the original model, stop the
% process.  If, however, the new model is statistically more significant,
% go and search for the third best variable.
% * Repeat this process until you can't identify a new variable that has a
% statistically significant impact on the model.

% This next bit of code will show you how to use sequential feature
% selection to solve a significantly larger problem.

%% Generate a Data Set

clear all
clc
rng(1981);

Z12 = randn(100,40);
Z1 = randn(100,1);
X = Z12 + repmat(Z1, 1,40);
B0 = zeros(10,1);
B1 = ones(10,1);
Beta = 2 * vertcat(B0,B1,B0,B1);
ds = dataset(Beta);

Y = X * Beta + 15*randn(100,1);
%%
% Use linear regression to fit the model.
b = regress(Y,X);
ds.Linear = b;

%%
% Use sequential feature selection to fit the model.
opts = statset('display','iter');

fun = @(x0,y0,x1,y1) norm(y1-x1*(x0\y0))^2;  % residual sum of squares
[in,history] = sequentialfs(fun,X,Y,'cv',5, 'options',opts)

%%
% I've configured |sequentialfs| to show each iteration of the feature
% selection process.  You can see each of the variables as it gets added to
% the model, along with the change in the cross validated mean squared
% error.

%%
% Generate a linear regression using the optimal set of features
beta = regress(Y,X(:,in));

ds.Sequential = zeros(length(ds),1);
ds.Sequential(in) = beta
%%
% As you can see, |sequentialfs| has generated a much more parsimonious
% model than the ordinary least squares regression.  We also have a model
% that should generate significantly more accurate predictions.  Moreover,
% if we needed to (hypothetically) extract the five most important
% variables we could simple grab the output from Steps 1:5.

%% Run a Simulation
% As a last step, we'll once again run a simulation to benchmark the
% technique.  Here, once again, we can see that the feature selection
% technique is doing a very good job identifying the true predictors while
% screening out the distractors.

matlabpool open

sumin = 0;
parfor j=1:100   
    Z12 = randn(100,40);
    Z1 = randn(100,1);
    X = Z12 + repmat(Z1, 1,40);
    B0 = zeros(10,1);
    B1 = ones(10,1);
    Beta = 2 * vertcat(B0,B1,B0,B1);
    Y = X * Beta + 15*randn(100,1);
    
    fun = @(x0,y0,x1,y1) norm(y1-x1*(x0\y0))^2;  % residual sum of squares
    [in,history] = sequentialfs(fun,X,Y,'cv',5);
    
    sumin = in + sumin;
end

bar(sumin)

matlabpool close
%%
% One last comment:  As you can see, neither |sequentialfs| nor all
% possible subset selection is perfect.  Neither of these techniques was
% able to capture all of the true variables while excluding the
% distractors.  It's important to recognize that these data sets were
% deliberately designed to be difficult to fit.  [We don't want to
% benchmark performance using "easy" problems where any old technique might
% succeed.]
%%
% Next week's blog post will focus on a radically different way to solve
% these same types of problems.  Stay tuned for a look at regularization
% techniques such as ridge regression, lasso and the elastic net.
%%
% Here are a couple questions that you might want to consider:
% # All possible subset methods are O(2^n).  What is the order of 
% complexity of sequential feature selection?
% # Other than scaling issues, can you think of any possible problems with
% sequential feature selection?  Are there situations where the algorithm
% might miss important variables?


##### SOURCE END ##### e359acae97de460f9c0dbca9011fcebe
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/7k76ciQ82R4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2011/11/21/subset-selection-and-regularization/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2011/11/21/subset-selection-and-regularization/</feedburner:origLink></item>
		<item>
		<title>Generating C Code from Your MATLAB Algorithms</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/goM3C7AfGZ0/</link>
		<comments>http://blogs.mathworks.com/loren/2011/11/14/generating-c-code-from-your-matlab-algorithms/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 21:48:00 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[Code Generation]]></category>
		<category><![CDATA[Deployment]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2011/11/14/generating-c-code-from-your-matlab-algorithms/</guid>
		<description><![CDATA[I am pleased to introduce guest blogger Arvind Ananthan. Arvind is the Product Marketing Manager for MATLAB Coder and Fixed-Point Toolbox. His main focus in this post is to introduce basics of MATLAB Coder, talk about the workflow, its use cases, and show examples of generated C code. Contents A Brief History of MATLAB to [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p>I am pleased to introduce guest blogger Arvind Ananthan. Arvind is the Product Marketing Manager for <a href="http://www.mathworks.com/products/matlab-coder/">MATLAB Coder</a> and <a href="http://www.mathworks.com/products/fixed/">Fixed-Point Toolbox</a>. His main focus in this post is to introduce basics of MATLAB Coder, talk about the workflow, its use cases, and show examples
         of generated C code.
      </p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">A Brief History of MATLAB to C</a></li>
         <li><a href="#2">A Simple Example</a></li>
         <li><a href="#7">Detailed MATLAB Coder Worfklow</a></li>
         <li><a href="#10">Support for Dynamic Sizing</a></li>
         <li><a href="#11">Use Cases of MATLAB Coder</a></li>
         <li><a href="#12">Generating MEX Functions for Simulation Acceleration</a></li>
         <li><a href="#16">Vectorization and Code Generation</a></li>
         <li><a href="#17">Customizing and Optimizing the Generated Code</a></li>
         <li><a href="#18">Learning More About MATLAB Coder</a></li>
      </ul>
   </div>
   <h3>A Brief History of MATLAB to C<a name="1"></a></h3>
   <p>In April, 2011, MathWorks introduced MATLAB Coder as a stand-alone product to generate C code from MATLAB code. This tool
      lets user generate readable, portable, and customizable C code from their MATLAB algorithms.
   </p>
   <p>Many astute readers will notice that C code generation from MATLAB isn't really brand new - and that we've had this capability
      to generate C code from MATLAB for quite some time now. Yes, that's true - we've been incubating this technology for quite
      some time till we felt it was ready to debut as a stand alone tool. Here's a timeline of this technology over the past few
      years:
   </p>
   <div>
      <ul>
         <li><b>2004</b> - Introduced the <i>Embedded MATLAB Function</i> block in Simulink
         </li>
         <li><b>2007</b> - Introduced the <tt>emlc</tt> function in Real-Time Workshop (now called Simulink Coder) to generate stand alone C from MATLAB
         </li>
         <li><b>2011</b> - Released MATLAB Coder, the first stand-alone product from MathWorks to generate portable and readable C code from MATLAB
            code.
         </li>
      </ul>
   </div>
   <h3>A Simple Example<a name="2"></a></h3>
   <p>Let me introduce the basics of using MATLAB Coder through a very simple example that multiplies two variables. Let's generate
      C code from the following MATLAB function that multiplies two inputs:
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">dbtype <span style="color: #A020F0">simpleProduct.m</span></pre><pre style="font-style:oblique">
1     function c = simpleProduct(a,b) %#codegen
2     c = a*b;
</pre><p>To generate C code from this function using MATLAB Coder, I first have to specify the size and data types of my inputs - I
      can do this through the MATLAB Coder UI (shown in the section below) where I specify my inputs as a [1x5] and [5x2] single
      precision matrices, and subsequently generate the following C code:
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">dbtype <span style="color: #A020F0">simpleProduct.c</span></pre><pre style="font-style:oblique">
1     #include "simpleProduct.h"
2     
3     void simpleProduct(const real32_T a[5], const real32_T b[10], real32_T c[2])
4     {
5       int32_T i0;
6       int32_T i1;
7       for (i0 = 0; i0 &lt; 2; i0++) {
8         c[i0] = 0.0F;
9         for (i1 = 0; i1 &lt; 5; i1++) {
10          c[i0] += a[i1] * b[i1 + 5 * i0];
11        }
12      }
13    }

</pre><p>MATLAB is a polymorphic language which means a single function, such as <tt>simpleProduct</tt>, can accept input arguments of different size and data types and output correct results. This function can behave as a simple
      scalar product, a dot product, or a matrix multiplier depending on what inputs you pass.
   </p>
   <p>Languages like C are said to be &#8220;stongly typed,&#8221; which requires you to create a different version of a function for every
      variation of input data size and types. Hence, when you write C code to implement <tt>simpleProduct</tt>, you have to know ahead of time the sizes and the data types of your inputs so you can implement the right variant.
   </p>
   <p>While MATLAB Coder helps you move from the highly flexible world of MATLAB to a strongly typed world of C, you still have
      to specify all of the constraints C expects. You do this in MATLAB Coder by creating a MATLAB Coder project file (<tt>simpleProduct.prj</tt> in this case) where you can specify the  various code generation parameters including the sizes and data types of your inputs
      as shown below.
   </p>
   <p><img vspace="5" hspace="5" src="http://blogs.mathworks.com/images/loren/296/simpleProductPRJ.png"> </p>
   <p>You can also edit the default configuration parameters by clicking on the <i>'More Settings'</i> link which appears on the 'Build' tab of this project UI .
   </p>
   <p>In the example above, I turned off a few default options such as including comments in the generated code just so you can
      see how compact the code is. You not only get the option to generate comments in the resulting C code, but also inlcude the
      original MATLAB code as comments in the corresponding sections of the generated code. This helps with traceability of your
      C code to your original algorithms.
   </p>
   <h3>Detailed MATLAB Coder Worfklow<a name="7"></a></h3>
   <p>The simple example above quickly illustrates the process of generating code with MATLAB coder and shows how the resulting
      C code looks.
   </p>
   <p>Naturally, your real-world functions are going to be much more involved and may run into hundreds or even thousands of lines
      of MATLAB Code. To help you handle that level of complexity, you would need an iterative process, like the 3-step workflow
      described here, that guides you through the task of code generation incrementally:
   </p>
   <div>
      <ol>
         <li><b>Prepare:</b> First, prepare your code to ensure that you can indeed generate code from your MATLAB algorithm. The convenience of MATLAB
            language doesn't map directly to the constrained behavior of C. You may have to re-write portions of your MATLAB code so it
            uses the MATLAB language features that support this mapping from MATLAB to C.
         </li>
         <li><b>Test &amp; Verify:</b> Next, you generate a MEX function to test that your preparation step is correct. If the tool is successful in generating
            a MEX function then you are ready to verify the results of this MEX function against the original MATLAB code. If not, you'd
            have to iterate on the previous step till you can successfully generate a MEX function.
         </li>
         <li><b>Generate:</b> Finally, you generate C source code and further iterate upon your MATLAB code in order to control the look and feel or the
            performance of your C code.You can also generate an optimized MEX function by turning off certain memory integrity checks
            and debug options that could slow down its execution at this stage.
         </li>
      </ol>
   </div>
   <p>I'll now demonstrate this concept using a slightly more involved example. The following MATLAB code implements the Newton-Raphson
      numerical technique for computing the n-th root of a real valued number.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">dbtype <span style="color: #A020F0">newtonSearchAlgorithm.m</span></pre><pre style="font-style:oblique">
1     function [x,h] = newtonSearchAlgorithm(b,n,tol) %#codegen
2     % Given, "a", this function finds the nth root of a
3     % number by finding where: x^n-a=0.
4     
5         notDone = 1;
6         aNew    = 0; %Refined Guess Initialization
7         a       = 1; %Initial Guess
8         count     = 0;
9         h(1)=a;
10        while notDone
11            count = count+1;
12            [curVal,slope] = fcnDerivative(a,b,n); %square
13            yint = curVal-slope*a;
14            aNew = -yint/slope; %The new guess
15            h(count)=aNew;
16            if (abs(aNew-a) &lt; tol) %Break if it's converged
17                notDone = 0;
18            elseif count&gt;49 %after 50 iterations, stop
19                notDone = 0;
20                aNew = 0;
21            else
22                a = aNew;
23            end
24        end
25        x = aNew;
26        
27    function [f,df] = fcnDerivative(a,b,n)
28    % Our function is f=a^n-b and it's derivative is n*a^(n-1).
29    
30        f  = a^n-b;
31        df = n*a^(n-1);

</pre><p>Our first step towards generating C code from this file is to prepare it for code generation. For code generation, each variable
      inside your MATLAB code must be intialized, which means specifying its size and data type. In this case, I'll choose to initialize
      the variable <tt>h</tt> to a static maximum size, <tt>h = zeros(1,50)</tt>, as it doesn't grow beyond a length of 50 inside the <tt>for</tt> loop.
   </p>
   <p>You can invoke the code generation function using the GUI or the command line (through the <tt>codegen</tt> command). Without worrying about the details of this approach, let's look at the generated C code:
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">dbtype <span style="color: #A020F0">newtonSearchAlgorithmMLC.c</span></pre><pre style="font-style:oblique">
1     #include "newtonSearchAlgorithmMLC.h"
2     
3     void newtonSearchAlgorithmMLC(real_T b, real_T n, real_T tol, real_T *x, real_T
4       h[50])
5     {
6       int32_T notDone;
7       real_T a;
8       int32_T count;
9       real_T u1;
10      real_T slope;
11      notDone = 1;
12      *x = 0.0;
13      a = 1.0;
14      count = -1;
15      memset((void *)&amp;h[0], 0, 50U * sizeof(real_T));
16      h[0] = 1.0;
17      while (notDone != 0) {
18        count++;
19        u1 = n - 1.0;
20        u1 = pow(a, u1);
21        slope = n * u1;
22        u1 = pow(a, n);
23        *x = -((u1 - b) - slope * a) / slope;
24        h[count] = *x;
25        if (fabs(*x - a) &lt; tol) {
26          notDone = 0;
27        } else if (count + 1 &gt; 49) {
28          notDone = 0;
29          *x = 0.0;
30        } else {
31          a = *x;
32        }
33      }
34    }

</pre><p>The subfunction <tt>fcnDerivative</tt> is inlined in the generated C code.  You can choose to not inline the code by putting this command <tt>coder.inline('never')</tt> in the MATLAB file.
   </p>
   <h3>Support for Dynamic Sizing<a name="10"></a></h3>
   <p>If you have variable in your MATLAB code that needs to vary its size during execution, you can choose to deal with this in
      three different ways in the generated C code:
   </p>
   <div>
      <ol>
         <li><b>Static allocation with fixed maximum size:</b> you can initialize variables to the maximum possible static size (like I did in the example above). In the generated code,
            memory is prealloacted to this size.
         </li>
         <li><b>Variable sizing with maximum size allocation:</b> this option will declare the memory for the variable in the generated code to its maximum possible size, but you can dynamically
            grow or shrink the variable size within this allocated maximum.
         </li>
         <li><b>Variable sizing with dynamic memory allocation:</b> this results in the generated code using <tt>malloc</tt> to allocate the memory for the variables that change in size during code execution.
         </li>
      </ol>
   </div>
   <p>The last two options can be enabled by turning on the <i><b>variable-sizing</b></i> feature in the configuration parameters. However, do remember that enabling this option also makes the resulting C code bigger
      in size - so if you can avoid it, you can get much more compact and possibly more efficient code.
   </p>
   <h3>Use Cases of MATLAB Coder<a name="11"></a></h3>
   <p>The primary use of MATLAB Coder is to enable algorithm developers and system engineers working in MATLAB to quickly generate
      readable and portable C code. The generated C code can be used in different ways supporting different workflows. Here are
      a few use cases of MATLAB Coder:
   </p>
   <div>
      <ol>
         <li><b>Create standalone executables</b> from your algorithms for prototyping on a PC
         </li>
         <li><b>Speed up</b> your MATLAB algorithm code (or portions of it) by generating and executing the MEX functions
         </li>
         <li><b>Integrate</b> your MATLAB algorithms as C source code or compiled library with your hand-written software
         </li>
      </ol>
   </div>
   <p>I won't be talking about creating standalone executables or creating libraries from MATLAB Coder in this blog; I'll, however,
      say a few words on generating MEX functions.
   </p>
   <h3>Generating MEX Functions for Simulation Acceleration<a name="12"></a></h3>
   <p>The MEX interface enables you to integrate C code into MATLAB. One common use of MEX is to speed up performance bottlenecks
      in MATLAB code by re-writing them in C and executing them back in MATLAB as MEX functions. MATLAB Coder can save you time
      and effort by automatically generating MEX functions from your MATLAB code (and allowing you to import legacy C code easily
      as well).
   </p>
   <p>The speed-up you get through automatic MEX generation can vary quite a bit depending on the application. In some cases, you
      may not get any speed up at all, or possibly even a slow-down, as MATLAB language has gotten quite smart and efficient in
      computing many built-in functions by automatically taking advantage of processor specific routines (such as Intel Performance
      Primitives) and multithreading to utilize multiple cores.
   </p>
   <p>A few guidelines that you can use to determine if your algorithm is a good candidate for speed-up with MEX are:</p>
   <div>
      <ul>
         <li>your algorithm contains multiply nested <tt>for</tt> loops, and possibly handles state calculations (making each iteration dependent on the previous one)
         </li>
         <li>your MATLAB code is hard/impossible to vectorize</li>
         <li>most of the processing cycles in your algorithm are <b>not</b> spent on already optimized built-in functions such as <tt>fft</tt> or <tt>inv</tt>, etc.
         </li>
         <li>you don't rely heavily on toolbox functions (especially those unsupported for code generation) in your algorithms</li>
      </ul>
   </div>
   <p>In these cases, you can expect to see a speed-up. Let's look at a realistic example that illustrates this concept.</p>
   <p>The example I chose here is one that my colleague <a href="http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/">Sarah Zaranek</a> had highlighted in her guest blog on vectorization.
   </p>
   <p>I modified the original MATLAB script from that article into a function as only functions are supported by MATLAB Coder. The
      other change that I made to the original script was to initialize the output variables <tt>subs</tt> and <tt>vals</tt> (as all variables in your code has to be defined/intialized once when used with MATLAB Coder). And by turning on the variable
      sizing feature (using dynamic memory allocation), I didn't have to preallocate it to a sufficiently large enough size to accomodate
      its growth, which would use more memory (often a lot) than needed.
   </p>
   <p>The following statements create and use a MATLAB Coder configuration object with support for variable size arrays and dynamic
      memory allocation to generate a MEX function.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)"><span style="color: #228B22">% Create a MEX configuration object</span>
cfg = coder.config(<span style="color: #A020F0">'mex'</span>);
<span style="color: #228B22">% Turn on dynamic memory allocation</span>
cfg.DynamicMemoryAllocation = <span style="color: #A020F0">'AllVariableSizeArrays'</span>;
<span style="color: #228B22">% Generate MEX function</span>
codegen <span style="color: #A020F0">-config</span> <span style="color: #A020F0">cfg</span> <span style="color: #A020F0">gridSpeedUpFcn</span> <span style="color: #A020F0">-args</span> <span style="color: #A020F0">{10}</span>
disp(<span style="color: #A020F0">'MEX generation complete!'</span>)</pre><pre style="font-style:oblique">Warning: There is no short path form of 'H:\Documents\LOREN\MyJob\Art of
MATLAB' available. Short path names do not include embedded spaces. The
short path name feature may be disabled in your operating system. Short
path names are required for successful code generation, therefore it may
not be possible to successfully complete the code generation process. To
avoid this problem, enable short path name support in your operating
system, or do not attempt code generation in paths containing spaces. 
MEX generation complete!
</pre><p>This generates a MEX function in the current folder. I'll use <tt>tic</tt>, and <tt>toc</tt> to estimate the execution times of the original MATLAB function and its MEX version.
   </p>
   <p>I run each function multiple times inside a <tt>for</tt> loop and use only the last few runs for our computation time calculation so we can minimize the effects of initialization
      and caching.
   </p><pre style="background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)">MToc = zeros(1,30);
MexToc = zeros(1,30);
<span style="color: #0000FF">for</span> j = 1:30
    tic; gridSpeedUpFcn(10);
    MToc(j) = toc;
<span style="color: #0000FF">end</span>

<span style="color: #0000FF">for</span> j = 1:30
    tic; gridSpeedUpFcn_mex(10);
    MexToc(j) = toc;
<span style="color: #0000FF">end</span>

eff_M_time = mean(MToc(1,21:30));
eff_Mex_time = mean(MexToc(1,21:30));

disp([<span style="color: #A020F0">'MATLAB code execution time (with nx=10): '</span>, num2str(eff_M_time)])
disp([<span style="color: #A020F0">'MEX code executing time (with nx=10): '</span>, num2str(eff_Mex_time)])

disp([<span style="color: #A020F0">' Speed up factor: '</span>, num2str(eff_M_time/eff_Mex_time)]);</pre><pre style="font-style:oblique">MATLAB code execution time (with nx=10): 0.31652
MEX code executing time (with nx=10): 0.0027057
 Speed up factor: 116.9806
</pre><p>The speed up we see in this example is more pronounced for larger values of <tt>nx</tt> (and would vary between different computers).
   </p>
   <h3>Vectorization and Code Generation<a name="16"></a></h3>
   <p>Sarah, in her blog <a href="http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/">article</a>, explains quite nicely on how to vectorize this code using <tt>meshgrid</tt> and <tt>ndgrid</tt> to explode the variables in support of vectorization, and get similar levels of speed up.
   </p>
   <p>Sarah paid a small price in code readability for her vectorization efforts, but a much larger price in memory consumption.
      This points to a common trade-off when vectorizing MATLAB code: execution time vs. memory consumption. The temporary variables
      which enable us to remove loops through matrix math are often very large, effectively limiting the number of iterations you
      can take before running out of memory. For instance, if we run her algorithm for a 1,000x1,000 grid instead of a 100x100 grid,
      we would create 9 double precision variables that are about 7.5 GB each!
   </p>
   <p>Code generation can effectively deliver similar speed-up benefits (and with less effort in some cases), as it's trying to
      explicitly achieve the same <tt>for</tt> loop operations in C code that a vectorized code achieves. However, the downside for most users with the code generation
      approach is that it's available with a separate product (higher cost), and only MATLAB code that supports code generation
      can automatically be converted to MEX as well.
   </p>
   <h3>Customizing and Optimizing the Generated Code<a name="17"></a></h3>
   <p>Now with most of the basics of code generation out of the way, a few questions might be on your mind:</p>
   <div>
      <ul>
         <li><b><i>"If I want the resulting code to look different, can I just edit the generated code?"</i></b> You can certainly edit the generated code; as you have just seen in the examples shown here, the code is very readable. However,
            once you modify it, you lose the connectivity to the original MATLAB code that generated it.
         </li>
         <li><b><i>"How can I optimize the code, or customize its look and feel?"</i></b> One easy way to modify the generated code is to change MATLAB Coder configuration parameters. Another way to customize the
            generated code is to directly edit the MATLAB code such as explicitly breaking up certain compact vector/matrix operations
            into <tt>for</tt> loops or other constructs you'd like to see in the generated code.
         </li>
         <li><b><i>"What about incorporating hand-optimized or processor specific intrinsics for certain portions of the code?"</i></b> <tt>coder.ceval</tt> is a command in MATLAB Coder that let's you integrate custom C code into your MATLAB code for both simulation and code generation.
            Using this you can use hand-optimized or legacy C code in your algorithms. <i>Embedded Coder</i> is another product that adds many code optimization and customization capabilities to MATLAB Coder such as using code replacement
            technology to incorporate optimized C code. It also lets you to customize the look-and-feel of the code.
         </li>
      </ul>
   </div>
   <h3>Learning More About MATLAB Coder<a name="18"></a></h3>
   <p>You can find more information about MATLAB Coder on the <a href="www-external-test.mathworks.com/products/matlab-coder/">product page</a>, which includes demos and recorded webinars. The <a href="http://www.mathworks.com/help/toolbox/coder/ug/ug_intropage.html">product documentation</a> is also an excellent resource to find answers to questions you might have when starting with this product.
   </p>
   <p>Please feel free to share your thoughts and comments on topics discussed here in this posting <a href="http://blogs.mathworks.com/loren/?p=296#respond">here</a>.
   </p><script language="JavaScript">
<!--

    function grabCode_118ae3094d7b413687ac64ea126cf023() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='118ae3094d7b413687ac64ea126cf023 ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' 118ae3094d7b413687ac64ea126cf023';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Arvind Ananthan';
        copyright = 'Copyright 2011 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_118ae3094d7b413687ac64ea126cf023()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
118ae3094d7b413687ac64ea126cf023 ##### SOURCE BEGIN #####
%% Generating C Code from Your MATLAB Algorithms
% I am pleased to introduce guest blogger Arvind Ananthan. Arvind is the
% Product Marketing Manager for
% <http://www.mathworks.com/products/matlab-coder/ MATLAB Coder> and
% <http://www.mathworks.com/products/fixed/ Fixed-Point Toolbox>. His main
% focus in this post is to introduce basics of MATLAB Coder, talk about the
% workflow, its use cases, and show examples of generated C code.
%% A Brief History of MATLAB to C
% In April, 2011, MathWorks introduced MATLAB Coder as a stand-alone
% product to generate C code from MATLAB code. This tool lets user generate
% readable, portable, and customizable C code from their MATLAB algorithms.
% 
% Many astute readers will notice that C code generation from MATLAB isn't
% really brand new - and that we've had this capability to generate C code
% from MATLAB for quite some time now. Yes, that's true - we've been
% incubating this technology for quite some time till we felt it was ready
% to debut as a stand alone tool. Here's a timeline of this technology over
% the past few years:
% 
% * *2004* - Introduced the _Embedded MATLAB Function_ block in Simulink
% * *2007* - Introduced the |emlc| function in Real-Time Workshop (now
% called Simulink Coder) to generate stand alone C from MATLAB
% * *2011* - Released MATLAB Coder, the first stand-alone product from
% MathWorks to generate portable and readable C code from MATLAB code.
% 
%% A Simple Example
% Let me introduce the basics of using MATLAB Coder through a very simple
% example that multiplies two variables. Let's generate C code from the
% following MATLAB function that multiplies two inputs:
dbtype simpleProduct.m
%%
%
%%
%
% To generate C code from this function using MATLAB Coder, I first have to
% specify the size and data types of my inputs - I can do this through the
% MATLAB Coder UI (shown in the section below) where I specify my inputs as
% a [1x5] and [5x2] single precision matrices, and subsequently generate
% the following C code:
dbtype simpleProduct.c
%%
% MATLAB is a polymorphic language which means a single function, such as
% |simpleProduct|, can accept input arguments of different size and data
% types and output correct results. This function can behave as a simple
% scalar product, a dot product, or a matrix multiplier depending on what
% inputs you pass.
%
% Languages like C are said to be â€œstongly typed,â€� which requires you to
% create a different version of a function for every variation of input
% data size and types. Hence, when you write C code to implement
% |simpleProduct|, you have to know ahead of time the sizes and the data
% types of your inputs so you can implement the right variant.
%
% While MATLAB Coder helps you move from the highly flexible world of
% MATLAB to a strongly typed world of C, you still have to specify all of
% the constraints C expects. You do this in MATLAB Coder by creating a
% MATLAB Coder project file (|simpleProduct.prj| in this case) where you
% can specify the  various code generation parameters including the sizes
% and data types of your inputs as shown below.
% 
% <<simpleProductPRJ.png>>
%
% You can also edit the default configuration parameters by clicking on the
% _'More Settings'_ link which appears on the 'Build' tab of this project
% UI .
%%
% In the example above, I turned off a few default options such as
% including comments in the generated code just so you can see how compact
% the code is. You not only get the option to generate comments in the
% resulting C code, but also inlcude the original MATLAB code as comments
% in the corresponding sections of the generated code. This helps with
% traceability of your C code to your original algorithms.
%
%% Detailed MATLAB Coder Worfklow
%
% The simple example above quickly illustrates the process of generating
% code with MATLAB coder and shows how the resulting C code looks.
%
% Naturally, your real-world functions are going to be much more involved
% and may run into hundreds or even thousands of lines of MATLAB Code. To
% help you handle that level of complexity, you would need an iterative
% process, like the 3-step workflow described here, that guides you through
% the task of code generation incrementally:
% 
% # *Prepare:* First, prepare your code to ensure that you can indeed
% generate code from your MATLAB algorithm. The convenience of MATLAB
% language doesn't map directly to the constrained behavior of C. You may
% have to re-write portions of your MATLAB code so it uses the MATLAB
% language features that support this mapping from MATLAB to C.
% # *Test &#038; Verify:* Next, you generate a MEX function to test that your
% preparation step is correct. If the tool is successful in generating a
% MEX function then you are ready to verify the results of this MEX
% function against the original MATLAB code. If not, you'd have to iterate
% on the previous step till you can successfully generate a MEX function.
% # *Generate:* Finally, you generate C source code and further iterate
% upon your MATLAB code in order to control the look and feel or the
% performance of your C code.You can also generate an optimized MEX
% function by turning off certain memory integrity checks and debug options
% that could slow down its execution at this stage.
%
% I'll now demonstrate this concept using a slightly more involved example.
% The following MATLAB code implements the Newton-Raphson numerical
% technique for computing the n-th root of a real valued number.
%
dbtype newtonSearchAlgorithm.m
%% 
% Our first step towards generating C code from this file is to prepare it
% for code generation. For code generation, each variable inside your
% MATLAB code must be intialized, which means specifying its size and
% data type. In this case, I'll choose to initialize the variable |h| to a
% static maximum size, |h = zeros(1,50)|, as it doesn't grow beyond a
% length of 50 inside the |for| loop.
% 
% You can invoke the code generation function using the GUI or the
% command line (through the |codegen| command). Without worrying about the
% details of this approach, let's look at the generated C code:
dbtype newtonSearchAlgorithmMLC.c
%%
% The subfunction |fcnDerivative| is inlined in the generated C code.  You
% can choose to not inline the code by putting this command
% |coder.inline('never')| in the MATLAB file.
%% Support for Dynamic Sizing 
% If you have variable in your MATLAB code that needs to vary its size
% during execution, you can choose to deal with this in three different
% ways in the generated C code:
% 
% # *Static allocation with fixed maximum size:* you can initialize
% variables to the maximum possible static size (like I did in the example
% above). In the generated code, memory is prealloacted to this size.
% # *Variable sizing with maximum size allocation:* this option will
% declare the memory for the variable in the generated code to its maximum
% possible size, but you can dynamically grow or shrink the variable size
% within this allocated maximum.
% # *Variable sizing with dynamic memory allocation:* this results in the
% generated code using |malloc| to allocate the memory for the variables
% that change in size during code execution.
%
% The last two options can be enabled by turning on the _*variable-sizing*_
% feature in the configuration parameters. However, do remember that
% enabling this option also makes the resulting C code bigger in size - so
% if you can avoid it, you can get much more compact and possibly more
% efficient code.
%
%% Use Cases of MATLAB Coder
%
% The primary use of MATLAB Coder is to enable algorithm developers and
% system engineers working in MATLAB to quickly generate readable and
% portable C code. The generated C code can be used in different ways
% supporting different workflows. Here are a few use cases of MATLAB Coder:
%
% # *Create standalone executables* from your algorithms for prototyping on
% a PC
% # *Speed up* your MATLAB algorithm code (or portions of it) by generating
% and executing the MEX functions
% # *Integrate* your MATLAB algorithms as C source code or compiled library
% with your hand-written software
%
% I won't be talking about creating standalone executables or creating
% libraries from MATLAB Coder in this blog; I'll, however, say a few words
% on generating MEX functions.
%
%% Generating MEX Functions for Simulation Acceleration
% The MEX interface enables you to integrate C code into MATLAB. One common
% use of MEX is to speed up performance bottlenecks in MATLAB code by
% re-writing them in C and executing them back in MATLAB as MEX functions.
% MATLAB Coder can save you time and effort by automatically generating MEX
% functions from your MATLAB code (and allowing you to import legacy C code
% easily as well).
% 
% The speed-up you get through automatic MEX generation can vary quite a
% bit depending on the application. In some cases, you may not get any
% speed up at all, or possibly even a slow-down, as MATLAB language has
% gotten quite smart and efficient in computing many built-in functions by
% automatically taking advantage of processor specific routines (such as
% Intel Performance Primitives) and multithreading to utilize multiple
% cores.
% 
% A few guidelines that you can use to determine if your algorithm is a
% good candidate for speed-up with MEX are:
%
% * your algorithm contains multiply nested |for| loops, and possibly
% handles state calculations (making each iteration
% dependent on the previous one)
% * your MATLAB code is hard/impossible to vectorize
% * most of the processing cycles in your algorithm are *not* spent on
% already optimized built-in functions such as |fft| or |inv|, etc.
% * you don't rely heavily on toolbox functions (especially those
% unsupported for code generation) in your algorithms
%
% In these cases, you can expect to see a speed-up. Let's look at a
% realistic example that illustrates this concept.
%
% The example I chose here is one that my colleague
% <http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/
% Sarah Zaranek> had highlighted in her guest blog on vectorization.
%
%%
% I modified the original MATLAB script from that article into a function
% as only functions are supported by MATLAB Coder. The other change that I
% made to the original script was to initialize the output variables |subs|
% and |vals| (as all variables in your code has to be defined/intialized
% once when used with MATLAB Coder). And by turning on the variable sizing
% feature (using dynamic memory allocation), I didn't have to preallocate
% it to a sufficiently large enough size to accomodate its growth, which
% would use more memory (often a lot) than needed.
% 
% The following statements create and use a MATLAB Coder configuration
% object with support for variable size arrays and dynamic memory
% allocation to generate a MEX function.

% Create a MEX configuration object
cfg = coder.config('mex');  
% Turn on dynamic memory allocation
cfg.DynamicMemoryAllocation = 'AllVariableSizeArrays'; 
% Generate MEX function
codegen -config cfg gridSpeedUpFcn -args {10}  
disp('MEX generation complete!')
%%
% This generates a MEX function in the current folder. I'll use
% |tic|, and |toc| to estimate the execution times of the original MATLAB
% function and its MEX version.
%
% I run each function multiple times inside a |for| loop and use only
% the last few runs for our computation time calculation so we can minimize
% the effects of initialization and caching.

MToc = zeros(1,30);
MexToc = zeros(1,30);
for j = 1:30
    tic; gridSpeedUpFcn(10); 
    MToc(j) = toc; 
end

for j = 1:30
    tic; gridSpeedUpFcn_mex(10); 
    MexToc(j) = toc; 
end

eff_M_time = mean(MToc(1,21:30));
eff_Mex_time = mean(MexToc(1,21:30));

disp(['MATLAB code execution time (with nx=10): ', num2str(eff_M_time)])
disp(['MEX code executing time (with nx=10): ', num2str(eff_Mex_time)])

disp([' Speed up factor: ', num2str(eff_M_time/eff_Mex_time)]);

%%
% The speed up we see in this example is more pronounced for larger values
% of |nx| (and would vary between different computers).

%% Vectorization and Code Generation
% Sarah, in her blog
% <http://blogs.mathworks.com/loren/2008/06/25/speeding-up-matlab-applications/
% article>, explains quite nicely on how to vectorize this code using
% |meshgrid| and |ndgrid| to explode the variables in support of
% vectorization, and get similar levels of speed up.
%
% Sarah paid a small price in code readability for her vectorization
% efforts, but a much larger price in memory consumption. This points to a
% common trade-off when vectorizing MATLAB code: execution time vs. memory
% consumption. The temporary variables which enable us to remove loops
% through matrix math are often very large, effectively limiting the number
% of iterations you can take before running out of memory. For instance, if
% we run her algorithm for a 1,000x1,000 grid instead of a 100x100 grid, we
% would create 9 double precision variables that are about 7.5 GB each!
%
% Code generation can effectively deliver similar speed-up benefits
% (and with less effort in some cases), as it's trying to explicitly
% achieve the same |for| loop operations in C code that a vectorized code
% achieves. However, the downside for most users with the code generation
% approach is that it's available with a separate product (higher cost),
% and only MATLAB code that supports code generation can automatically be
% converted to MEX as well.
%% Customizing and Optimizing the Generated Code
% Now with most of the basics of code generation out of the way, a few
% questions might be on your mind:
% 
% * *_"If I want the resulting code to look different, can I just edit the
% generated code?"_* You can certainly edit the generated code; as you have
% just seen in the examples shown here, the code is very readable. However,
% once you modify it, you lose the connectivity to the original MATLAB code
% that generated it.
% * *_"How can I optimize the code, or customize its look and feel?"_* One
% easy way to modify the generated code is to change MATLAB Coder
% configuration parameters. Another way to customize the generated code is
% to directly edit the MATLAB code such as explicitly breaking up certain
% compact vector/matrix operations into |for| loops or other constructs
% you'd like to see in the generated code.
% * *_"What about incorporating hand-optimized or processor specific
% intrinsics for certain portions of the code?"_* |coder.ceval| is a
% command in MATLAB Coder that let's you integrate custom C code into your
% MATLAB code for both simulation and code generation. Using this you can
% use hand-optimized or legacy C code in your algorithms. _Embedded Coder_
% is another product that adds many code optimization and customization
% capabilities to MATLAB Coder such as using code replacement technology to
% incorporate optimized C code. It also lets you to customize the
% look-and-feel of the code.
%% Learning More About MATLAB Coder
% You can find more information about MATLAB Coder on the
% <www-external-test.mathworks.com/products/matlab-coder/ product page>,
% which includes demos and recorded webinars. The
% <http://www.mathworks.com/help/toolbox/coder/ug/ug_intropage.html product
% documentation> is also an excellent resource to find answers to questions
% you might have when starting with this product.
%
% Please feel free to share your thoughts and comments on topics discussed
% here in this posting <http://blogs.mathworks.com/loren/?p=296#respond
% here>.

##### SOURCE END ##### 118ae3094d7b413687ac64ea126cf023
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/goM3C7AfGZ0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2011/11/14/generating-c-code-from-your-matlab-algorithms/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2011/11/14/generating-c-code-from-your-matlab-algorithms/</feedburner:origLink></item>
		<item>
		<title>Managing Deployed Application Output with Message Handlers</title>
		<link>http://feedproxy.google.com/~r/mathworks/loren/~3/BXL91sL4d-s/</link>
		<comments>http://blogs.mathworks.com/loren/2011/11/04/managing-deployed-application-output-with-message-handlers/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 13:59:08 +0000</pubDate>
		<dc:creator>Loren Shure</dc:creator>
				<category><![CDATA[Deployment]]></category>

		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2011/11/04/managing-deployed-application-output-with-message-handlers/</guid>
		<description><![CDATA[Guest blogger Peter Webb returns with another in an occasional series of postings about application deployment. Contents Two Output Streams A Function with Output Where are the Handlers? Logging Messages to a File Two Output Streams Programs running in interactive MATLAB send error, warning and informational text messages to the MATLAB command window. By default, [...]]]></description>
			<content:encoded><![CDATA[<div xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd" class="content">
   <introduction>
      <p>Guest blogger <a href="http://www.mathworks.com/matlabcentral/fileexchange/authors/4660">Peter Webb</a> returns with another in an <a href="http://blogs.mathworks.com/loren/category/deployment/">occasional series</a> of postings about application deployment.
      </p>
   </introduction>
   <h3>Contents</h3>
   <div>
      <ul>
         <li><a href="#1">Two Output Streams</a></li>
         <li><a href="#2">A Function with Output</a></li>
         <li><a href="#3">Where are the Handlers?</a></li>
         <li><a href="#4">Logging Messages to a File</a></li>
      </ul>
   </div>
   <h3>Two Output Streams<a name="1"></a></h3>
   <p>Programs running in interactive <a href="http://www.mathworks.com/products/matlab">MATLAB</a> send <a href="http://www.mathworks.com/help/techdoc/ref/error.html">error</a>, <a href="http://www.mathworks.com/help/techdoc/ref/warning.html">warning</a> and <a href="http://www.mathworks.com/help/techdoc/ref/disp.html">informational</a> text messages to the MATLAB command window. By default, deployed applications output text to the <a href="http://en.wikipedia.org/wiki/Standard_streams"><i>standard output</i> and <i>standard error</i></a> streams.
   </p>
   <p>In many cases, particularly for <a href="http://en.wikipedia.org/wiki/Console_application">console applications</a> on Windows and UNIX, this works well enough: the output appears in the DOS window or UNIX terminal. However, non-console
      applications (and any type of <a href="http://www.mathworks.com/help/toolbox/compiler/f2-995712.html">shared library</a> or <a href="http://www.mathworks.com/products/netbuilder/">builder component</a>) may be unable to access either of the standard output streams; messages sent to those streams will be lost. To avoid losing
      messages when the standard streams are unavailable, deployed applications register output and error <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f2-998954.html#f2-1008529"><i>message handlers</i></a>. A message handler directs formatted text messages to a persistent or visible location, typically a log file or an output
      window.
   </p>
   <p>In this post, I'll show you how to send both error and informational messages to a log file.</p>
   <h3>A Function with Output<a name="2"></a></h3>
   <p>To demonstrate output redirection, I wrote the <tt>sendmessages</tt> function, which displays an informational message and a warning, and then issues an error:
   </p><pre>function sendmessages(info, warn, err)
    disp(info);
    warning(warn);
    error(err);</pre><p>In order to follow the rest of this posting, you'll need the <a href="http://www.mathworks.com/matlabcentral/fileexchange/30567-managing-deployed-application-output-with-message-handlers">source code</a> from MATLAB Central, particularly the C main program, <tt>log.c</tt>. See <tt>logREADME</tt> in the ZIP file for a complete description of distributed files and step-by-step directions for building the application.
   </p>
   <h3>Where are the Handlers?<a name="3"></a></h3>
   <p>Create a C shared library from the example function with <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f0-985134.html">MATLAB Compiler</a>:
   </p><pre>mcc -Ngv -W lib:libmsgfcn -T link:lib sendmessages.m</pre><p>Open the generated wrapper file, <tt>libmsgfcn.c</tt>, in a text editor. The wrapper file contains two default handlers, <tt>mclDefaultErrorHandler</tt> and <tt>mclDefaultPrintHandler</tt>. The generated code contains two library initialization functions: <tt>libmsgfcnInitialize</tt> and <tt>libmsgfcnInitializeWithHandlers</tt>. You redirect program output by passing your custom message handlers to <tt>libmsgfcnInitializeWithHandlers</tt>. All message handlers conform to the same interface:
   </p><pre> static int MessageHandler(const char *message)</pre><p>You never need to call the message handler directly. The <a href="http://www.mathworks.com/help/toolbox/compiler/f12-999353.html">MATLAB Compiler Runtime</a> (MCR) calls your message handler automatically.
   </p>
   <h3>Logging Messages to a File<a name="4"></a></h3>
   <p>A common use for a message handler is to record messages and the time they occurred in a log file. This type of message handler
      requires only a few lines of C code:
   </p><pre>static FILE *logFile = NULL;</pre><pre>static int LogFileHandler(const char *s)
{
    if (logFile != NULL)
    {
        time_t now = time(NULL);
        fprintf(logFile, "%s", ctime(&amp;now));
        fprintf(logFile, "%s\n", s);
    }
}</pre><p>Your main program needs to open and close the log file, of course. Open the log file before calling the library initialization
      function and close it after calling <tt>mclTerminateApplication</tt>. See the full implementation in <tt>log.c</tt>.
   </p>
   <p>Build the application with <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/mbuild.html">mbuild</a>:
   </p>
   <p>(UNIX):    <tt>mbuild -g log.c -L. -lmsgfcn</tt></p>
   <p>(Windows): <tt>mbuild -g log.c libmsgfcn.lib</tt></p>
   <p>Run it from a DOS shell or UNIX terminal window:</p>
   <p>(UNIX):    <tt>./log "information" "look out!" "oops."</tt></p>
   <p>(Windows): <tt>log "information" "look out!" "oops."</tt></p>
   <p>The example program creates the log file <tt>MessageLog.txt</tt> to capture program output.
   </p><pre>Fri Aug 19 11:53:32 2011
information</pre><pre>Fri Aug 19 11:53:32 2011
Warning: lookout!
&gt; In sendmessages at 3</pre><pre>Fri Aug 19 11:53:32 2011
Error using sendmessages (line 4)
oops.</pre><p>Next time, I'll show you how to display log messages in a persistent window, and how to distinguish between errors and informational
      messages. In the meantime, <a href="http://blogs/mathworks.com/loren/?p=294#respond">let me know</a> how your applications manage output. Does your code need to distinguish between program output and error messages?
   </p><script language="JavaScript">
<!--

    function grabCode_f3ccb97df46c4a819de5bfa553b5d8ac() {
        // Remember the title so we can use it in the new page
        title = document.title;

        // Break up these strings so that their presence
        // in the Javascript doesn't mess up the search for
        // the MATLAB code.
        t1='f3ccb97df46c4a819de5bfa553b5d8ac ' + '##### ' + 'SOURCE BEGIN' + ' #####';
        t2='##### ' + 'SOURCE END' + ' #####' + ' f3ccb97df46c4a819de5bfa553b5d8ac';
    
        b=document.getElementsByTagName('body')[0];
        i1=b.innerHTML.indexOf(t1)+t1.length;
        i2=b.innerHTML.indexOf(t2);
 
        code_string = b.innerHTML.substring(i1, i2);
        code_string = code_string.replace(/REPLACE_WITH_DASH_DASH/g,'--');

        // Use /x3C/g instead of the less-than character to avoid errors 
        // in the XML parser.
        // Use '\x26#60;' instead of '<' so that the XML parser
        // doesn't go ahead and substitute the less-than character. 
        code_string = code_string.replace(/\x3C/g, '\x26#60;');

        author = 'Peter Webb';
        copyright = 'Copyright 2011 The MathWorks, Inc.';

        w = window.open();
        d = w.document;
        d.write('<pre>\n');
        d.write(code_string);

        // Add author and copyright lines at the bottom if specified.
        if ((author.length > 0) || (copyright.length > 0)) {
            d.writeln('');
            d.writeln('%%');
            if (author.length > 0) {
                d.writeln('% _' + author + '_');
            }
            if (copyright.length > 0) {
                d.writeln('% _' + copyright + '_');
            }
        }

        d.write('</pre>\n');
      
      d.title = title + ' (MATLAB code)';
      d.close();
      }   
      
-->
</script><p style="text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray"><br /><a href="javascript:grabCode_f3ccb97df46c4a819de5bfa553b5d8ac()"><span style="font-size: x-small;        font-style: italic;">Get 
            the MATLAB code 
            <noscript>(requires JavaScript)</noscript></span></a><br /><br />
      Published with MATLAB&reg; 7.13<br /></p>
</div>
<!--
f3ccb97df46c4a819de5bfa553b5d8ac ##### SOURCE BEGIN #####
%% Managing Deployed Application Output with Message Handlers
% Guest blogger 
% <http://www.mathworks.com/matlabcentral/fileexchange/authors/4660 Peter Webb>
% returns with another in an 
% <http://blogs.mathworks.com/loren/category/deployment/ occasional series>
% of postings about application deployment. 

%% Two Output Streams
% Programs running in interactive 
% <http://www.mathworks.com/products/matlab MATLAB>
% send 
% <http://www.mathworks.com/help/techdoc/ref/error.html error>,
% <http://www.mathworks.com/help/techdoc/ref/warning.html warning> and
% <http://www.mathworks.com/help/techdoc/ref/disp.html informational>
% text messages to the MATLAB command window. By default, deployed 
% applications output text to the 
% <http://en.wikipedia.org/wiki/Standard_streams _standard output_ and 
% _standard error_> streams. 
% 
% In many cases, particularly for 
% <http://en.wikipedia.org/wiki/Console_application console applications>
% on Windows
% and UNIX, this works well enough: the output appears in the DOS window or
% UNIX terminal. However, non-console applications (and any type of 
% <http://www.mathworks.com/help/toolbox/compiler/f2-995712.html 
% shared library> or 
% <http://www.mathworks.com/products/netbuilder/ builder component>) 
% may be unable to access either of the 
% standard output streams; messages sent to those streams will be lost. To
% avoid losing messages when the standard streams are unavailable, 
% deployed applications register output and error 
% <http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f2-998954.html#f2-1008529
% _message handlers_>. 
% A message handler directs formatted text messages to a persistent or
% visible location, typically a log file or an output window.
%
% In this post, I'll show you how to send both error and informational
% messages to a log file.
%
%% A Function with Output
% To demonstrate output redirection, I wrote the |sendmessages| function,
% which displays an informational message and a warning, and then issues an
% error:
%
%  function sendmessages(info, warn, err)
%      disp(info);
%      warning(warn);
%      error(err); 
%
% In order to follow the rest of this posting, you'll need the 
% <http://www.mathworks.com/matlabcentral/fileexchange/30567-managing-deployed-application-output-with-message-handlers 
% source code>
% from MATLAB Central, particularly the C main program, |log.c|. See
% |logREADME| in the ZIP file for a complete description of distributed
% files and step-by-step directions for building the application.
%
%% Where are the Handlers?
% Create a C shared library from the example function with 
% <http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/f0-985134.html
% MATLAB Compiler>:
%
%  mcc -Ngv -W lib:libmsgfcn -T link:lib sendmessages.m
%
% Open the generated wrapper file, |libmsgfcn.c|, in a text editor. The
% wrapper file contains two default handlers, |mclDefaultErrorHandler| and
% |mclDefaultPrintHandler|. The generated code contains two library initialization 
% functions: |libmsgfcnInitialize| and |libmsgfcnInitializeWithHandlers|.
% You redirect program output by passing your custom message handlers to
% |libmsgfcnInitializeWithHandlers|. All message handlers conform to the
% same interface:
%
%   static int MessageHandler(const char *message)
%
% You never need to call the message handler directly. The 
% <http://www.mathworks.com/help/toolbox/compiler/f12-999353.html 
% MATLAB Compiler Runtime> (MCR) calls your message handler automatically.
% 
%% Logging Messages to a File
% A common use for a message handler is to record messages and the time
% they occurred in a log file. This type of message handler requires only a
% few lines of C code:
%
%  static FILE *logFile = NULL;
%
%  static int LogFileHandler(const char *s)
%  {
%      if (logFile != NULL)
%      {
%          time_t now = time(NULL);
%          fprintf(logFile, "%s", ctime(&#038;now));
%          fprintf(logFile, "%s\n", s);
%      }
%  }
%
% Your main program needs to open and close the log file, of course. Open
% the log file before calling the library initialization function and close
% it after calling |mclTerminateApplication|. See the full implementation
% in |log.c|.
%
% Build the application with 
% <http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/mbuild.html 
% mbuild>:
%
% (UNIX):    |mbuild -g log.c -L. -lmsgfcn|
%
% (Windows): |mbuild -g log.c libmsgfcn.lib|
%
% Run it from a DOS shell or UNIX terminal window:
%
% (UNIX):    |./log "information" "look out!" "oops."|
%
% (Windows): |log "information" "look out!" "oops."|
%
% The example program creates the log file |MessageLog.txt| to capture
% program output.
%
%  Fri Aug 19 11:53:32 2011
%  information
%
%  Fri Aug 19 11:53:32 2011
%  Warning: lookout!
%  > In sendmessages at 3
%  
%  Fri Aug 19 11:53:32 2011
%  Error using sendmessages (line 4)
%  oops. 
% 
% Next time, I'll show you how to display log messages in a persistent
% window, and how to distinguish between errors and informational messages.
% In the meantime, <http://blogs/mathworks.com/loren/?p=294#respond 
% let me know> how your applications manage output. Does
% your code need to distinguish between program output and error messages?
##### SOURCE END ##### f3ccb97df46c4a819de5bfa553b5d8ac
--><img src="http://feeds.feedburner.com/~r/mathworks/loren/~4/BXL91sL4d-s" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.mathworks.com/loren/2011/11/04/managing-deployed-application-output-with-message-handlers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.mathworks.com/loren/2011/11/04/managing-deployed-application-output-with-message-handlers/</feedburner:origLink></item>
	</channel>
</rss>

