Sean's pick this week is kml2struct by James Slegers. Import Your KML Files Earlier this week, my friend sent me a Google Maps link containing our hiking tracks... read more >>

]]>Sean's pick this week is kml2struct by James Slegers.

Earlier this week, my friend sent me a Google Maps link containing our hiking tracks recorded with the GPS on his smart phone. From Google Maps you can download the data as a KML (Keyhole Markup Language) file.

I wanted to plot it and experiment with the data in MATLAB. Once again, the File Exchange was there for me!

% Read the KML file into a struct: kmlS = kml2struct('2014-04-12PresidentialTraverse.kml'); % Convert to table (new datatype in R2013b) to make manipulations easier kmlT = struct2table(kmlS);

Now looking at the table, we can see the four important pieces:

- Geometry: What is it? A point, line, etc.
- Lon: Longitude coordinate of tracks
- Lat: Latitude coordinate of tracks
- Bounding Box: Bounding box if we want to draw it on a map

First, I'll get the bounding box of the whole trip. To do this, we'll stack each segment's bounding box into the third dimension and then pick the min and the max:

boxes = kmlT.BoundingBox; % Extract Bounding box from table boxes3d = cat(3,boxes{:}); % Stack along third dimension bbox = [min(boxes3d(1,:,:),[],3); max(boxes3d(2,:,:),[],3)].'; % Min and max along third dimension give limits latlim = bbox(2,:)+[-0.01 0.01]; % Buffer them lonlim = bbox(1,:)+[-0.01 0.01];

Next, I only want to work with the lines, i.e. the actual tracks. The points represent termini, which I don't need right now. Using the new categorical data type and logical indexing, we can extract the latitude and longitude from the table.

% Make Geometry categorical kmlT.Geometry = categorical(kmlT.Geometry); % Extract the latitude and longitude for the lines latlon = kmlT{kmlT.Geometry=='Line', {'Lat','Lon'}};

I'll get the elevation data from NASA using the Web Map Service in the Mapping Toolbox.

nasaLayers = wmsfind('nasa*elev', 'SearchField', 'serverurl'); ned = refine(nasaLayers, 'usgs_ned'); [Z, refmatZ] = wmsread(ned, 'Latlim', latlim, 'Lonlim', lonlim); Z = double(Z);

And finally, plot a contour map with the tracks overlaid on it.

figure ax = usamap(latlim, lonlim); geoshow(Z, refmatZ, 'DisplayType', 'texturemap') contourm(Z, refmatZ, 20, 'Color', 'k') demcmap(Z) title('Presidential Traverse 04/12:13/2014','FontSize',16) % Add each segment for ii = 1:length(latlon) geoshow(latlon{ii,:}, 'LineWidth', 2) end

Have you ever recorded a trip and then tried to analyze it in MATLAB? The tasks above would be more straight-forward if you had access to the original GPX files. These typically come with the elevation data and time stamps allowing you to get even more statistics with more accuracy.

Give it a try and let us know what you think here or leave a comment for James.

Get
the MATLAB code

Published with MATLAB® R2014a

Idin's pick this week is Communications System Toolbox Support Package for RTL-SDR Radio by MathWorks Communications System Toolbox Team. There has been a proliferation in the availability and usage of low-cost hardware platforms such as Raspberry Pi, BeagleBoard, and USRP boards. These... read more >>

]]>Idin's pick this week is Communications System Toolbox Support Package for RTL-SDR Radio by MathWorks Communications System Toolbox Team.

There has been a proliferation in the availability and usage of low-cost hardware platforms such as Raspberry Pi, BeagleBoard, and USRP boards. These boards can be used for quickly realizing ideas in the physical world, as well as great educational tools. MathWorks has invested heavily over the last few years to work with as many of these platforms as possible, which makes my life as a MathWorks application engineer much more exciting (see list of supported hardware here).

The USB-based software-defined radio (SDR) card from NooElec selling for under $25 is a nifty piece of hardware for any wireless communications enthusiast. There are a few different versions of this radio, but I will focus on the one pictured below (officially: NooElec NESDR Mini SDR and DVB-T USB Stick (R820T)).

What makes this radio more accessible is the MATLAB support package, which allows off-the-air signals to be captured directly into MATLAB and/or Simulink. This means you can make simple receivers to demodulate/decode signals that are present around us.

**What can you do with this radio?**

The radio front-end on this card supports frequencies from 25MHz to 1.7GHz, which means you can receive FM radio, some navigation signals, and most GSM and LTE bands in use in the United States. The bandwidth is limited to about 3MHz, so you can easily cover FM radio or GSM, but you can't do a full LTE receiver (most LTE deployments go up to 10MHz). But even on LTE, you can still capture and decode the LTE broadcast information (MIB), which is transmitted within the middle 1.4MHz of the band (the middle 72 OFDM subcarriers). If you have access to LTE System Toolbox, this should be a relatively simple exercise.

**What's already there?**

When you download the MathWorks support package, you will get a few example models including an FM receiver (for both MATLAB and Simulink), a spectral analysis app, and an FRS/GMRS walkie-talkie receiver. So you can listen to your favorite FM radio stations, or receive signals from an off-the-shelf walkie-talkie.

**How does it work?**

My favorite piece about this support package is its simplicity. With the support package, you simply get a "receiver block" in Simulink, and a corresponding System object in MATLAB. In Simulink, a simple spectrum analyzer looks like this:

where you set three parameters for the radio (as shown below): its center frequency, sampling rate, and tuner gain.

The MATLAB interface is just as straight forward.

% Create receiver System object hSDRrRx = comm.SDRRTLReceiver(... 'CenterFrequency',102.5e6, ... 'EnableTunerAGC',true, ... 'SampleRate',1e6, ... 'SamplesPerFrame',256*20, ... 'OutputDataType','double');

% View spectrum; assuming "hSpectrum" is already initialized for count = 1:5000 [data,~] = step(hSDRrRx); % no 'len' output needed for blocking operation data = data-mean(data); % remove DC component step(hSpectrum,data); end

For the curious, the spectrum analyzer object, hSpectrum, can be initialized as follows:

hSpectrum = dsp.SpectrumAnalyzer(... 'SpectrumType','Power density',... 'SampleRate',1e6, ... 'SpectralAverages',50);

**So What?**

I would argue that every electrical engineering student taking a digital communications course must own one of these devices. Even for the non-student, and the generally curious person, this will be the best $25 you will spend this year. And the MATLAB support package for the radio makes this one of the cheapest and easiest learning tools for wireless communications.

**Comments**

Let us know what you think here or leave a comment for MathWorks Communications System Toolbox Team.

Get
the MATLAB code

Published with MATLAB® R2014a

Jiro's pick this week is 2048 MATLAB. "2048" Need I say more? I'm certain that many of you know what I'm talking about. 2048 is an online and mobile game created by Gabriele Cirulli recently. All I can say is... read more >>

]]>Jiro's pick this week is 2048 MATLAB.

**"2048"**

Need I say more? I'm certain that many of you know what I'm talking about. 2048 is an online and mobile game created by Gabriele Cirulli recently. All I can say is that it is an addictive game that has already resulted in a few MATLAB implementations.

A couple of weekends ago, I also spent a day implementing a MATLAB version. I probably spent more time than I needed to on trying to make it look as close as possible to the original version, but it was a lot of fun.

But then, one of my colleagues, Seth (who was also just featured in this blog last week), suggested including a mechanism for feeding in different algorithms to solve the game. There must be an AI algorithm that would solve the game or produce a high score consistently.

In the app, I've included the ability to select a MATLAB function as the solving algorithm. Then you can see the algorithm in action as it tries to solve the game. The algorithm must be defined as a MATLAB function that takes in a 4x4 matrix representing the game board (NaN for empty spots) and returns a character array representing the direction to move ('up', 'down', 'right', or 'left'). Here's an example of a simple algorithm that randomly chooses a direction.

function direction = myAI(board) d = {'up', 'down', 'right', 'left'}; direction = d{randi(4)};

I've also included a command line simulator, which you can use to do a montecarlo simulation of a particular algorithm.

game = Game2048Simulator(@myAI); simulate(game, 1000) % 1000 simulations viewResult(game, 25) % 25 histogram bins

We can display the highest score and compute what percentages of the simulation resulted in each of the highest block.

% Display highest score disp(['Highest score: ', num2str(max(game.Result.Score))]) disp(' ') % Get the unique values for high blocks highblocks = game.Result.HighestBlock; blocks = unique(highblocks); % Compute the percentage of occurrance for each high block value nSims = length(highblocks); for id = 1:length(blocks) percentage = nnz(highblocks == blocks(id)) / nSims; disp([num2str(blocks(id)), ' ', num2str(percentage*100), '%']) end

Highest score: 3172 16 0.2% 32 6.8% 64 34.5% 128 49.7% 256 8.8%

**Submit your algorithm!**

So here's a challenge for you. Submit your algorithm and win some MathWorks swag! I will give prizes for each of the following criteria:

- Highest score - In the example above,
**3172**. - Highest block OR in the case of a tie for "Highest block", the one with the highest percentage for the highest block. In the
example above,
**8.8% for 256**.

*It was brought to my attention that since the simulator I've included with the File Exchange entry would stop the game once
you reach 2048, I am limiting the highest score you could get. For my next update, I will change it so that the game will
continue until no more blocks can move.*

If your algorithm is fairly short, please submit the algorithm through the comments for this blog post. Be sure to use code formatting. For lengthy algorithms or multi-function algorithms, feel free to email them to me (as ASCII text).

In the next blog post I write, I will go over some of the winning algorithms you have submitted.

Enjoy!!

Get
the MATLAB code

Published with MATLAB® R2014a

Now for something a little different. As some of you may know, most of the free time that I spend in front of a computer is absorbed by MATLAB Answers. So... read more >>

]]>Now for something a little different. As some of you may know, most of the free time that I spend in front of a computer is absorbed by MATLAB Answers. So this week I am going to highlight one of my favorite questions and some of the answers to it.

Sean's pick this week is Seth's Answer to Sven's Question about numerical packing.

I started working at MathWorks in late November, 2011. When Sven asked this question a few weeks later, my wife had not yet moved to Massachusetts. It was December in my temporary not-so-energy efficient house so I was saving costs by keeping the house at 50F (10C) and staying late at work.

This question came in at the end of the work day and immediately caught my attention. I spent the next five hours coming up with an answer.

A year or so later, Seth, our Optimization Toolbox Product Marketing Manager, was showcasing some new technology to the engineers where he debuted the Sudoku Solver that Alan wrote about a few weeks ago and *the* solution to the Traveling Sales Man problem. This actually interested me more since it brought back memories of Transportation Engineering
classes in college. This new technology was still a few releases away from being ready but the potential was already obvious.
I sent Seth this question to see if it would be a good candidate and he replied with the optimal answer in about an hour.

When R2014a was release earlier this Month, he agreed to post his answer.

So what is the question? The goal of the question is to optimally group some elements into bins to minimize both the number of bins and the size of the biggest bin. Each bin has a maximum size and the elements can not be rearranged from their original order. This is a type of optimization problem known as a knapsack problem (thanks Walter!).

The first answer to roll in came from Elige Grant. He posted a way to do this with brute force. For small problems, this will be quick enough and the optimal solution will obviously be found and guaranteed.

As the problem size grows, however, brute force starts to become cumbersome in terms of computational resources. My solution
was to use the only solver at the time that we had for mixed integer optimization, `ga`, the genetic algorithm solver. This solver has an option, *'IntCon'*, to constrain variables to integers, a requirement. I'd also only been able to have access to the Global Optimization Toolbox for the last few weeks, so this was a chance to play with something new. It did work too (!), for small problems. Once
again, as the problem size grew, this approach fell apart and there's no way to prove optimality (or lack thereof) from it.

Enter `intlinprog`.

In R2014a we introduced `intlinprog`, a mixed-integer linear programming solver. This solver will solve any mixed-integer linear programming problem, be it a
minimization or a feasibility study. If requested it will also prove that the solution is optimal for a minimization problem.

Let's examine how it's done:

```
% The call to |intlinprog| looks like this:
[xopt,fval,eflag,output] = intlinprog(f, intIdxs, A, b, Aeq, beq, lb, ub);
```

LP: Optimal objective value is 765.000000. Cut Generation: Applied 10 implication cuts. Lower bound is 765.000000. Heuristics: Found 1 solution using rss. Upper bound is 939.000000. Relative gap is 18.50%. Cut Generation: Applied 2 gomory cuts, and 7 implication cuts. Lower bound is 785.000000. Relative gap is 16.37%. Branch and Bound: nodes total num int integer relative explored time (s) solution fval gap (%) 9 0.01 2 9.220000e+02 1.044609e+01 12 0.01 2 9.220000e+02 0.000000e+00 Optimal solution found. Intlinprog stopped because the objective value is within a gap tolerance of the optimal value; options.TolGapAbs = 0 (the default value). The intcon variables are integer within tolerance, options.TolInteger = 1e-05 (the default value).

- f, objective function coefficients, zeros
- intIdxs, the integer constraints, are the integers between one and 52 meaning every variable is integer constrained
- A, contribution of each element to a bin, and that it must be in a bin greater than or equal to the previous element
- b, is the upper limit on the bin sizes
- Aeq, each element for each of the bins
- beq is all ones, each element is in only one bin
- lb, the lower bound, is all zeros
- ub, the upper bound, is all ones

We can see the sparsity patterns of A and Aeq the linear and equality constraints using `spy()`

subplot(2,1,1) spy(A) title('Linear Constraints') subplot(2,1,2) spy(Aeq) title('Equality Constraints')

Now let's look at output

output

output = relativegap: 0 absolutegap: 0 numfeaspoints: 2 numnodes: 12 constrviolation: 1.7764e-15 message: 'Optimal solution found.

Since *absolutegap* is 0, this is the best solution.

If we were curious on time, that took about 0.037s to run!

Do you have a problem that could be solved with mixed-integer linear programming, or one that you're not sure about and would like our thoughts on? Let us know about it here.

Also, if you liked this post, and you're still reading at this point (which probably biases the data): Would you like me to
write another out of model Pick Of The Week post that highlights an `intlinprog` practical example?

Get
the MATLAB code

Published with MATLAB® R2014a

Kristen's pick this week is Packing Santa's Sleigh by Alfonso Nieto-Castanon.Hi, my name is Kristen, and this is my first post for the Pick of the Week as a guest blogger. My background is in statistics, machine learning, and data science, which are the areas I support as an Education... read more >>

]]>Kristen's pick this week is Packing Santa's Sleigh by Alfonso Nieto-Castanon.

*Hi, my name is Kristen, and this is my first post for the Pick of the Week as a guest blogger. My background is in statistics, machine learning, and data science, which are the areas I support as an Education Technical Evangelist at MathWorks. I use MATLAB every day to support faculty and students at universities around the world as they use MATLAB to teach and conduct research. The most exciting part of my job is seeing the diverse problems that are solved with MATLAB! Today, I’m going to talk about one example...*

Over the holidays, MathWorks sponsored a Packing Santa's Sleigh competition on Kaggle, a platform for predictive modeling and data analytics competitions. Congratulations to Alfonso, who had the top MATLAB submission and scored second place overall! (Alfonso is no stranger to winning MATLAB contests – see his MATLAB Programming Contest wins here and number one rank on Cody here.)

Participants received the $x$-, $y$-, and $z$-dimensions of an ordered list of one million presents. To solve the problem, they were required to pack the presents onto a 1000 x 1000 sleigh, minimizing the total height of the sleigh and keeping the presents as close to the original order as possible. Presents could be rotated as long as they remained parallel to the $x$-, $y$-, and $z$-axes. (You might recognize this as a holiday version of the three-dimensional bin packing problem.)

The solutions were judged on the compactness of packing (minimized height) and present order (delta from original order). The actual evaluation metric $M$ was calculated by

$M = 2 \cdot max_{i}(z_{i}) + \sigma(\Gamma)$, where

$z_{i}$ = $z$-coordinate of the $i$th present,

$\sigma(\Gamma) = \sum_{i=1}^{N_{p}} |i-\Gamma_{i}|$,

$\Gamma$ = order the presents appear, and

$\Gamma_{i}$ = the Present ID in the $i$th position.

The ideal present order is $\Gamma_{ideal} = 1,2,3,\dots,N_{p}$, so that $\sigma(\Gamma_{ideal}) = 0$, where $N_p$ is the total number of presents.

A naïve solution might be to simply pack the presents in reverse order from the bottom to the top. Each layer would have a height $H$, the next layer would start at that height, and the process would be repeated until all of the presents are in the sleigh. This would optimize the present order, but leaves much room to reduce the height of the sleigh.

Alfonso's solution implements a similar layer-based approach, but he adds a creative twist to how individual layers are defined: He divided the presents in each layer into two sets based on their heights, $H_{1}$ (Set 1) and $H_{2}$ (Set 2). His algorithm keeps the original order of the presents by packing from the bottom up and reverses the solution at the end. Figure 1 shows a naïve layering approach (on the left) and a generalization of Alfonso's solution (on the right).

After the first layer, each layer starts partially occupied by presents from the layer below it. The process for each layer looks like this:

- Select random $H_{1}$ and $H_{2}$ values, where $H_{2}$ was constrained to be greater than or equal to $H_{1}$, and $H_{1}$ was constrained to be greater than or equal to $H_{2}$ - $H_{1}$ from the previous layer.
- Partition the presents into Set 1 and Set 2. Presents with all sides greater than H1 are put in Set 2. Of the remaining presents, ones that would fit at or below the maximum height are separated into the two sets based on the set for which they minimize the
*wasted volume*$^1$. - Estimate the expected
*volume density*$^2$, assuming a given*surface density*$^3$, based on the partitions of the presents. If the expected*volume density*is above the current best solution, implement a 2-d solver to place the presents in the layer. If successfully converged, adaptively increase the*surface density*value; otherwise, decrease the value. - This process is repeated until there are no longer any combinations of $H_{1}$ and $H_{2}$ that result in a higher
*volume density*.

After all of the layers are packed, the algorithm compacts them further by attempting to lower each present vertically, if there is room. Finally, the sleigh is flipped upside down to improve the optimal volume density and achieve the optimal order. This got Alfonso his winning score.

$^{1,2,3}$ Alfonso provides the definitions of these terms, as well as a more detailed description with additional tricks and nice visualization of his solution, on the competition Forum here. You can see other Kaggle competitions that he's won here.

Congratulations to Alfonso, the other competition winners, and all 362 competing teams!

Visit the competition Data page to download the data and sample MATLAB code to get you started. Good luck!

Give it a try and let us know about your solution here or leave a comment for Alfonso!

Get
the MATLAB code

Published with MATLAB® R2014a

Brett's Pick this week is more of a challenge than a "Pick".If I were to search the MATLAB Central File Exchange for "face detection" (with the quotation marks) I would get a dazzling--and somewhat overwhelming--array of 44 hits. Trying to detect faces (or anything else*) in images seems to me... read more >>

]]>Brett's Pick this week is more of a challenge than a "Pick".

If I were to search the MATLAB Central File Exchange for "face detection" (with the quotation marks) I would get a dazzling--and somewhat overwhelming--array of 44 hits. Trying to detect faces (or anything else*) in images seems to me a reasonable thing to want to do, and in my mind typifies the challenges that the Computer Vision System Toolbox was made to address. In fact, I shudder to think of what classical "image processing" code designed to detect faces might look like.

But how do we navigate the 44 files on the File Exchange that facilitate that detection process? Where do we start? I can sort the results by number of downloads, by rating, by relevancy, ....

Some of those downloads seem to have great potential. But where does one start?

For this post, I'm going to go "out-of-model" and focus not on a submitted File Exchange file, but on core capabilities of the Computer Vision System Toolbox. This Toolbox has a "Cascade Object Detector" that facilitates out-of-the-box detection of faces, eyes, noses, mouths, and upper bodies. In fact, there are two in-product face detectors, and "FrontalFaceCART" (frontal face detection using "Classification And Regression Tree" analysis) is the default tool behavior.

Using the detectors in the Computer Vision System Toolbox is stunningly simple; we simply instantiate an instance of the cascade object detector and, for visualization purposes, a vision.ShapeInserter:

faceDetector = vision.CascadeObjectDetector; shapeInserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[0 255 255]);

We then read input image:

```
I = imread('visionteam.jpg');
imshow(I);shg;
```

Then we simply "step" the instantiated objects on the image of interest. The CascadeObjectDetector returns the bounding boxes of the detected objects, and the ShapeInserter delimits them in the image:

bbox = step(faceDetector, I); % Draw boxes around detected faces and display results I_faces = step(shapeInserter, I, int32(bbox)); imshow(I_faces), title('Detected faces');

Detecting faces using MathWorks' functionality is trivially easy. It requires MATLAB, the Image Processing Toolbox, and the Computer Vision System Toolbox, and a few readily reproducible lines of code.

*Swag to the first person who shows me how to detect faces in "visionteam.jpg" with a File Exchange submission!*

By the way, you can readily train MATLAB to find objects that are not in the "pre-trained" list above. For that, you might find these tools useful:

Get
the MATLAB code

Published with MATLAB® R2014a

Sean's pick this week is Straightforward Copy & Paste by Yvan Lengwiler. When presenting seminars on MATLAB, I often ask customers how to get data into and out of Excel.... read more >>

]]>Sean's pick this week is Straightforward Copy & Paste by Yvan Lengwiler.

When presenting seminars on MATLAB, I often ask customers how to get data into and out of Excel. The answers are typically
commands such as `xlsread` and `xlswrite`. These are great, and are often the best way to do this for an application. However, they can be overkill if you just need
to move a single array to your active Excel sheet every now and then.

Let's see what Yvan has done:

```
% A simple magic square:
A = magic(5)
```

A = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

Because I left the semicolon off, it was echoed to the command line.

I'll copy it from there and paste it into Excel:

It pasted it in as a string into cells A1:A5. That's not what I wanted!

Instead, use Ivan's copy:

copy(A)

Now in Excel, when I paste, it'll paste the way I intended:

We could get that same functionality with just MATLAB by interactively opening the variable in the Variable Editor (or by
using `openvar`) and then copying from there.

So where does this really shine? What if we want to extract the middle part of some text or a table from a website and paste it into MATLAB?

Since Yvan's copy and paste allow linefeeds and separators, it's easy to bring in complicated data like this.

Let's get a list of all of MathWorks' products from the website into MATLAB. First, highlight and copy the table for all products:

% Pause so I can go copy it, then press a key when ready pause % Use Yvan's Paste and show first few results products = paste(); disp(products(1:10))

[1x0 char] [1x0 char] 'MATLAB® Product Family' [1x0 char] 'MATLAB ' [1x0 char] 'Parallel Computing' 'Parallel Computing Toolbox ' 'MATLAB Distributed Computing Server ' [1x0 char]

This gives us the whole table including headers and empty elements. We want only the products. The product strings have a space at the end of them that we can use to determine if the non-empty cells are products.

% Define an anonymous function to test if each cell is a product % I.e. non-empty and the last element a space isproduct = @(x)~isempty(x) && isspace(x(end)); % Keep only the cells which are products products = products(cellfun(isproduct,products)); % Deblank it to remove those precious spaces at the end. Keep only % unique products since some show up a few times under different categories. products = unique(deblank(products)); disp(products)

'Aerospace Blockset' 'Aerospace Toolbox' 'Bioinformatics Toolbox' 'Communications System Toolbox' 'Computer Vision System Toolbox' 'Control System Toolbox' 'Curve Fitting Toolbox' 'DO Qualification Kit (for DO-178)' 'DSP System Toolbox' 'Data Acquisition Toolbox' 'Database Toolbox' 'Datafeed Toolbox' 'Econometrics Toolbox' 'Embedded Coder' 'Filter Design HDL Coder' 'Financial Instruments Toolbox' 'Financial Toolbox' 'Fixed-Point Designer' 'Fuzzy Logic Toolbox' 'Gauges Blockset' 'Global Optimization Toolbox' 'HDL Coder' 'HDL Verifier' 'IEC Certification Kit (for ISO 26262 and IEC 61508)' 'Image Acquisition Toolbox' 'Image Processing Toolbox' 'Instrument Control Toolbox' 'LTE System Toolbox' 'MATLAB' 'MATLAB Builder EX (for Microsoft Excel)' 'MATLAB Builder JA (for Java language)' 'MATLAB Builder NE (for Microsoft .NET Framework)' 'MATLAB Coder' 'MATLAB Compiler' 'MATLAB Distributed Computing Server' 'MATLAB Production Server' 'MATLAB Report Generator' 'Mapping Toolbox' 'Model Predictive Control Toolbox' 'Model-Based Calibration Toolbox' 'Neural Network Toolbox' 'OPC Toolbox' 'Optimization Toolbox' 'Parallel Computing Toolbox' 'Partial Differential Equation Toolbox' 'Phased Array System Toolbox' 'Polyspace Bug Finder' 'Polyspace Code Prover' 'RF Toolbox' 'Real-Time Windows Target' 'Robust Control Toolbox' 'Signal Processing Toolbox' 'SimBiology' 'SimDriveline' 'SimElectronics' 'SimEvents' 'SimHydraulics' 'SimMechanics' 'SimPowerSystems' 'SimRF' 'Simscape' 'Simulink' 'Simulink 3D Animation' 'Simulink Code Inspector' 'Simulink Coder' 'Simulink Control Design' 'Simulink Design Optimization' 'Simulink Design Verifier' 'Simulink PLC Coder' 'Simulink Real-Time' 'Simulink Report Generator' 'Simulink Verification and Validation' 'Spreadsheet Link EX (for Microsoft Excel)' 'Stateflow' 'Statistics Toolbox' 'Symbolic Math Toolbox' 'System Identification Toolbox' 'SystemTest' 'Trading Toolbox' 'Vehicle Network Toolbox' 'Wavelet Toolbox'

And there we have it, MathWorks' 82 products!

How many of those products have you heard of (or not heard of)? I'm proud to say that I've only not *heard* of two of them; I'll send some MathWorks swag to the first person to guess each correctly :) *Limit: three guesses per day*

Do you have complicated data that you need to get into MATLAB? If so, what are your favorite tools or workflows to do this?

Give this a try and let us know what you think here or leave a comment for Yvan.

Get
the MATLAB code

Published with MATLAB® R2013b

Jiro's pick this week is seconds2human by Rody Oldenhuis.There was a time when I obsessed over waitbars. Why is that? I'm not sure, but Ned has a few guesses. But one of the features that I look for in these waitbar entries is the ability to show the remaining time,... read more >>

]]>Jiro's pick this week is `seconds2human` by Rody Oldenhuis.

There was a time when I obsessed over waitbars. Why is that? I'm not sure, but Ned has a few guesses. But one of the features that I look for in these waitbar entries is the ability to show the remaining time, like some of these. I have created my own waitbar as well to include the time remaining, and I remember thinking quite a bit about how I display the time. I can easily calculate the time using `tic` and `toc`. But what's the best way to display the seconds? It's probably better to say "3 hours 44 minutes 13 seconds remaining" than to say "13453 seconds remaining". So I had to do some manipulations to have an appropriate display, and many of the entries I highlighted above do that as well.

Rody's `seconds2human` does this nicely. You simply pass in the time in seconds, and it will convert it to something human understandable. There's an approximate display:

seconds2human(13453)

ans = About 3 hours and 44 minutes.

and an accurate display:

```
seconds2human(13453,'full')
```

ans = 3 hours, 44 minutes, 13 seconds.

Now, I can just make use of this and not have to worry about decomposing the seconds.

hWait = waitbar(0, 'Calculating...'); total = 1000; % Start timer tstart = tic; for iter = 1:total SomeComputation(iter); % Calculate remaining time remaining = toc(tstart)*(total-iter)/iter; % Update waitbar waitbar(iter/total,hWait, ... ['Time remaining: ',seconds2human(remaining)]) end delete(hWait);

**Comments**

I also want to point out that Rody's code is very well-written. It is well-documented, has error checking, and allows for a vectorized call, i.e. you can pass an array of numbers. Give this a try and let us know what you think here or leave a comment for Rody.

Get
the MATLAB code

Published with MATLAB® R2013b

Will's pick this week is Draw a Polar Dendrogram by Sam Roberts. If I hadn't chosen aerospace engineering for a career, genetics would have been a wise alternative. In recent years, it's become one of those peculiar hobbies of mine. I suppose that the combination of large data sets, statistical methods,... read more >>

]]>If I hadn't chosen aerospace engineering for a career, genetics would have been a wise alternative. In recent years, it's become one of those peculiar hobbies of mine. I suppose that the combination of large data sets, statistical methods, and a dash of human history appeal to me.

Years ago, I decided to have the genes in my Y chromosome examined. The specifics of my Campbell lineage become murky around 1800 such that I can't tell you how I relate to the vast majority of people named Campbell around the world. In lieu of a paper trail, I was curious to see if a DNA test would show any commonality with the Duke of Argyll, the head of the Campbell clan. Sure enough, our results were similar. The values vary slightly due to mutations amassed over the generations, but they were close enough to indicate that it's no fluke we have the same surname.

The service that conducted the analysis provided statistical estimates of the time in which the Duke and I shared a common ancestor. Rather than take the estimates at face value, the engineer/genetics enthusiast in me wanted to understand how they were calculated. While this is an interesting topic, the details aren't worth exploring here. Suffice it to say that I wrote MATLAB code that produces probability curves for when two men shared a common ancestor.

Months after having achieved this, it occurred to me that I could advance this notion one step further by applying it to groups instead of individuals. I extracted the peak of the probability distribution between every pair of individuals in the group. This enabled me to create a matrix of genetic distances, which I called MODE. Row 1 and Column 1 show the expected number of individuals separating him from all the other group members. Row 2 and Column 2 show the distance of member 2 to others, and so forth.

With a few lines of code, I was able to reshape this into a distance vector, supply that to the Statistics Toolbox function linkage, and voila, create a dendrogram. In effect, I have a family tree showing how and when different Scottish clans diverged. (I threw in some Irish names for good measure).

As I said, I'm no expert on this subject, thus I have no idea how accurate this diagram is. I can tell you that when I change the metric argument to linkage, I get radically different results. But even if the details aren't perfect, I appear to be on the right track. All the Campbells cluster together as you would expect. Five of us split around 1600 or so, while another broke off around 1000 AD. The common ancestor for everyone is about 60 generations ago, which works out to around the time the Romans abandoned Britain. However, this is probably misleading since my analysis doesn't account for the possibility of back mutations.

At long last, we come to the Pick of the Week. Diagrams like this are fun, but is there any way to make it even more exciting? This is precisely what Sam has figured out how to do. His submission displays a dendogram on a polar projection. The code he employs is simple and easy for me to understand, enough so that I felt comfortable making alterations within ten minutes (I updated the labeling to display the surnames). So even though it's been a long time coming (the code has been available for 5+ years), congratulations to Sam on his fine work!

Let us know what you think here or leave a comment for Sam.

]]>

Sean's pick this week is WorkerObjWrapper by MathWorks' Parallel Computing Team. Background MATLAB's Parallel Computing Toolbox provides you with the the ability to open a pool of MATLAB... read more >>

]]>Sean's pick this week is WorkerObjWrapper by MathWorks' Parallel Computing Team.

MATLAB's Parallel Computing Toolbox provides you with the the ability to open a pool of MATLAB workers that you can distribute work to with high level commands
like `parfor`.

When communicating with the workers in this pool, there will always be an overhead in data communication. The less data we
can transmit to the workers the better speed improvements we'll see. This can be difficult when working with large arrays
and can actually cause parallel computations to be slower than serial ones. *WorkerObjWrapper* has provided a convenient way to make data persist on a worker; this could be large arrays, connections to databases or other
things that we need on each iteration of a `parfor` loop.

We're going to pull some financial data from Yahoo! using the connection from the Datafeed Toolbox.

I have a list of securities and the corresponding fields I want from them:

% Securities and desired fields securities = {'MAR','PG','MSFT','SAM',... 'TSLA','YHOO','CMG','AAL'}; fields = {'High',{'low','High'},'High','High',... {'Low','high'},'Low',{'low','Volume'},'Low'};

I first want to make sure there is an open parallel pool ( parpool ) to distribute computations to. I have a two core laptop, so I'll open two local workers by selecting the icon at the bottom left hand side of the desktop.

I've written three equivalent functions to pull the prices from Yahoo!

`fetchFOR`- uses a regular for-loop to fetch the prices`fetchPARFOR`- uses a parallel for-loop`fetchWOWPARFOR`- uses a parallel for-loop and WorkerObjWrapper to make the connection on all workers.

First, a sanity check to make sure they all do the same thing:

```
ff = fetchFOR(securities,fields);
fp = fetchPARFOR(securities,fields);
fw = fetchWOWPARFOR(securities,fields);
assert(isequal(ff,fp,fw)); % Errors if they're not equal
```

Since the assertion passed, meaning the functions return the same result, we can now do the timings. I'll use `timeit`.

t = zeros(3,1); % Measure timings t(1) = timeit(@()fetchFOR(securities,fields),1); t(2) = timeit(@()fetchPARFOR(securities,fields),1); t(3) = timeit(@()fetchWOWPARFOR(securities,fields),1); % Show results fprintf('%.3fs %s\n',t(1),'for',t(2),'parfor',t(3),'parfor with WorkerObjWrapper')

8.631s for 5.991s parfor 4.255s parfor with WorkerObjWrapper

So we can see that creating the connection once on each worker in the parallel pool and then using `parfor` gives us the best computation time.

Do you have to work with large data or repeat a process multiple times where parallel computing might help? I'm curious to hear your experiences and the challenges that you've faced.

Give it a try and let us know what you think here or leave a comment for our Parallel Computing Team.

Get
the MATLAB code

Published with MATLAB® R2013b