MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

1 Data Structures These operations on an empty scalar array return a nonzero value: A = []; ndims(A), isnumeric(A), isreal(A), isfloat(A), isempty(A), all(A), prod(A Using Empty Matrices in Relational Operations You can use empty matrices in relational operations such as “equal to” (==) or “greater than” (>) as long as both operands have the same dimensions, or the nonempty operand is scalar. The result of any relational operation involving anemptymatrixistheemptymatrix.Evencomparinganemptymatrixfor equality to itself does not return true, but instead yields an empty matrix: x = ones(0,3); y = x; y == x ans = Empty matrix: 0-by-3 Using Empty Matrices in Logical Operations MATLAB has two distinct types of logical operators: • Short-circuit (&&, ||) — Used in testing multiple logical conditions (e.g., x >= 50 && x < 100) where each condition evaluates to a scalar true or false. • Element-wise (&, |) — Performs a logical AND, OR, or NOT on each element of a matrix or array. Short-circuit Operations. The rule for operands used in short-circuit operations is that each operand must be convertible to a logical scalar value. Because of this rule, empty matrices cannot be used in short-circuit logical operations. Such operations return an error. The only exception is in the case where MATLAB can determine the result of a logical statement without having to evaluate the entire expression. This is true for the following two statements because the result of the entire statements are known by considering just the first term: 1-46

Empty Matrices, Scalars, and Vectors true || [] ans = 1 false && [] ans = 0 Elementwise Operations. Unlike the short-circuit operators, all elementwise operations on empty matrices are considered valid as long as the dimensions of the operands agree, or the nonempty operand is scalar. Element-wise operations on empty matrices always return an empty matrix: true | [] ans = [] Note This behavior is consistent with the way MATLAB does scalar expansion with binary operators, wherein the nonscalar operand determines thesizeoftheresult. . Scalars Any individual real or complex number is represented in MATLAB as a 1-by-1 matrix called a scalar value: A = 5; ndims(A) ans = 2 size(A) ans = 1 1 % Check number of dimensions in A % Check value of row and column dimensions 1-47

1 Data Structures<br />

These operations on an empty scalar array return a nonzero value:<br />

A = [];<br />

ndims(A), isnumeric(A), isreal(A), isfloat(A), isempty(A), all(A), prod(A<br />

Using Empty Matrices in Relational Operations<br />

You can use empty matrices in relational operations such as “equal to” (==) or<br />

“greater than” (>) as long as both operands have the same dimensions, or the<br />

nonempty operand is scalar. The result of any relational operation involving<br />

anemptymatrixistheemptymatrix.Evencomparinganemptymatrixfor<br />

equality to itself does not return true, but instead yields an empty matrix:<br />

x = ones(0,3);<br />

y = x;<br />

y == x<br />

ans =<br />

Empty matrix: 0-by-3<br />

Using Empty Matrices in Logical Operations<br />

<strong>MATLAB</strong> has two distinct types of logical operators:<br />

• Short-circuit (&&, ||) — Used in testing multiple logical conditions (e.g.,<br />

x >= 50 && x < 100) where each condition evaluates to a scalar true<br />

or false.<br />

• Element-wise (&, |) — Performs a logical AND, OR, or NOT on each<br />

element of a matrix or array.<br />

Short-circuit Operations. The rule for operands used in short-circuit<br />

operations is that each operand must be convertible to a logical scalar value.<br />

Because of this rule, empty matrices cannot be used in short-circuit logical<br />

operations. Such operations return an error.<br />

The only exception is in the case where <strong>MATLAB</strong> can determine the result of<br />

a logical statement without having to evaluate the entire expression. This<br />

is true for the following two statements because the result of the entire<br />

statements are known by considering just the first term:<br />

1-46

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!