MATLAB Answers

What programming challenges would you pose to new users?

52 views (last 30 days)
Doug Hull
Doug Hull on 15 Feb 2011
What projects would you give to take a person from no MATLAB knowledge to a greatly improve their MATLAB knowledge?
I am looking for challenges that would take a well versed MATLAB user about 30 minutes to program or less, but would expose a new MATLAB user to many new concepts.
Ideally, I will take several of these problems and turn them into videos.
See my answer below for a typical challenge.

Answers (8)

Doug Hull
Doug Hull on 15 Feb 2011

Given vectors X, Y of the same length and scalar lev,

Make a plot like this


  • data in Y above the value of lev is red,
  • data in Y below the value of lev is blue,
  • a line is drawn across the entire axis in green at y = lev.
  • Bonus challenge: make the crossing of the green line clean, i.e. it must touch the green line without a gap.
  • Make the green line such that you can click and drag it vertically and the data will update underneath it smoothly.

Skills likely to be employed:

  • Logical indexing
  • Use of NaN
  • Currentpoint
  • Callbacks
  • Updating Handle Graphics objects with set.
  • Good architecture
  • Incremental testing of code.

Kenneth Eaton
Kenneth Eaton on 15 Feb 2011
Designing simple GUIs can introduce newer users to a number of important concepts. For example...
Create a stopwatch GUI with a display for the elapsed time and a single push button to start and stop the counter.
  • You can't use global variables.
  • You can't use GUIDE.
  • All the code must be in a single m-file.
  • The program must not modify or depend on variables in the command workspace.
Concepts covered:
  • The nuts and bolts of designing GUIs and working with uicontrols
  • Callback functions
  • Nested functions
  • Working with timers
Admittedly, this isn't the sort of challenge I would ask of a completely green MATLAB user, more like someone who is a couple weeks or months into learning it and has a good deal of the basics down.
  1 Comment
Doug Hull
Doug Hull on 15 Feb 2011
Nineteen minutes coding, not counting interruptions. This is a worthy problem. Covers many of the same callback skills as mine because you are turning a mode on and off in callbacks.

Sign in to comment.

John D'Errico
John D'Errico on 15 Feb 2011
I would give a challenge of (accurately) computing the function sine(x), for given scalar values of x using the standard Taylor series approximation. This drives them to learn the use of loops, tests for convergence. It also teaches them about floating point numbers in MATLAB, and a bit about interval reduction methods to make the series converge rapidly enough.

Matt Tearle
Matt Tearle on 15 Feb 2011
Several of the problems at Project Euler have made me think about new ways of doing things in MATLAB (e.g. writing my own long integer class to do things like adding 50-digit numbers).
  1 Comment
John D'Errico
John D'Errico on 15 Feb 2011
I would add that PE was one of the incentives for writing my vpi class.

Sign in to comment.

John D'Errico
John D'Errico on 15 Feb 2011
How about the "game" of life? This is easily enough written in MATLAB using loops. But writing the updates in a vectorized form can teach the student about techniques for vectorization - an important skill in MATLAB.
Kenneth Eaton
Kenneth Eaton on 16 Feb 2011
@Sean: It actually ends up being a couple of characters longer. CONV2 has a different default 'shape' option than FILTER2, so you would have to add an additional argument 'same' (or perhaps just 's') to get just the central part of the convolution so M stays the same size on each iteration.

Sign in to comment.

John D'Errico
John D'Errico on 15 Feb 2011
MATLAB is a matrix laboratory. But many new users don't appreciate the capabilities of linear algebra in MATLAB. So as a way to teach them something about matrices, getting them to visualize how to set up a matrix for such a problem, I would show how to write a convolution in the form of a matrix multiply. You can build that convolution matrix in several ways. First, I might use a loop. More fun is to use toeplitz. Finally, you can teach about sparse matrices using this teaching tool, creating the matrix using spdiags. This teaches the student that sparse matrices can indeed yield significant speed improvements.
A nice thing is that some students who come to MATLAB from a signal processing background will find this example interesting, since they will understand the concept of a convolution.

Walter Roberson
Walter Roberson on 15 Feb 2011
Movement planning. A common assignment in robotics these days, that can help teach fundamental graph algorithms such as shortest-path.

John D'Errico
John D'Errico on 15 Feb 2011
There are many useful programming capabilities in matlab. One of them is recursion. A problem is that perhaps the most common problem that is taught as a recursion is perhaps one of the worst - the Fibonacci sequence. If this sequence is generated using the brute force recursion, it takes massive amounts of computation to generate even a relatively small Fibonacci number.
However, there are also many very nice problems that can be solved using recursion. For example, computing all of the partitions of an integer is one such nice problem. It can be introduced nicely as the "making change" problem, thus list all distinct ways that one can make change for a $2.00 bill, using only "coins" in denominations of {1, 5, 10, 25, 50, 100}.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!