Loren on the Art of MATLAB

Turn ideas into MATLAB

size Does Matter Sometimes

Recently at work I was on an email thread where people were discussing the function size and how to use it reliably in the context of arrays that might be two dimensional but could have higher dimensions as well. The original solutions resorted to calling size multiple times.

There is a better way, however, and I pointed it out. The idea is to use ~ to capture all the remaining dimensions after the first two. Like so:

  [rows, cols, ~] = size(A);

Steve has a much more in depth exposé of the problem and solution, and a follow-on post.

Instead of using ~, you could choose to use a junk variable and ignore it, but I (and Steve) prefer to use this relatively new feature and not clutter the code or workspace with something that is a distraction.

Have you used ~ to ignore outputs from a function, or in a function definition to ignore an input? Let me know some details here.

Published with MATLAB® 7.11

  • print
  • send email


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.