Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of listing examples of weird MATLAB syntax, I'll address some of the examples in the question that I think make sense or are expected/documented/desired behavior.</p> <ul> <li><p><strong>How <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/any.html" rel="noreferrer">ANY</a> and <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/all.html" rel="noreferrer">ALL</a> handle empty arguments:</strong></p> <p>The result of <code>any([])</code> makes sense: there are no non-zero elements in the input vector (since it's empty), so it returns false.</p> <p>The result of <code>all([])</code> can be better understood by thinking about how you might implement your own version of this function:</p> <pre><code>function allAreTrue = my_all(inArray) allAreTrue = true; N = numel(inArray); index = 1; while allAreTrue &amp;&amp; (index &lt;= N) allAreTrue = (inArray(index) ~= 0); index = index + 1; end end </code></pre> <p>This function loops over the elements of <code>inArray</code> until it encounters one that is zero. If <code>inArray</code> is empty, the loop is never entered and the default value of <code>allAreTrue</code> is returned.</p></li> <li><p><strong>Concatenating unlike classes:</strong></p> <p>When concatenating different types into one array, MATLAB follows a <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html" rel="noreferrer">preset precedence of classes</a> and converts values accordingly. The general precedence order (from highest to lowest) is: <code>char</code>, <code>integer</code> (of any sign or number of bits), <code>single</code>, <code>double</code>, and <code>logical</code>. This is why <code>[double(1.8), uint8(123)]</code> gives you a result of type <code>uint8</code>. When combining unlike integer types (<code>uint8</code>, <code>int32</code>, etc.), the <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/br04cfa-1.html#br04cfa-2" rel="noreferrer">left-most matrix element determines the type of the result</a>.</p></li> <li><p><strong>Multiple lines without using the <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-41244.html#f0-46995" rel="noreferrer">line continuation operator (<code>...</code>)</a>:</strong></p> <p>When constructing a matrix with multiple rows, you can simply hit return after entering one row and enter the next row on the next line, without having to use a <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f0-41244.html#f0-45379" rel="noreferrer">semicolon</a> to define a new row or <code>...</code> to continue the line. The following declarations are therefore equivalent:</p> <pre><code>a = {'aa', 'bb' 'cc', 'dd'}; a = {'aa', 'bb'; ... 'cc', 'dd'}; a = {'aa', 'bb'; 'cc', 'dd'}; </code></pre> <p>Why would you want MATLAB to behave like this? One reason I've noticed is that it makes it easy to cut and paste data from, for example, an Excel document into a variable in the MATLAB command window. Try the following:</p> <ul> <li>Select a region in an Excel file and copy it.</li> <li>Type <code>a = [</code> into MATLAB <em>without hitting return</em>.</li> <li>Right-click on the MATLAB command window and select "Paste".</li> <li>Type <code>];</code> and hit return. Now you have a variable <code>a</code> that contains the data from the rows and columns you selected in the Excel file, and which maintains the "shape" of the data.</li> </ul></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload