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


댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.