MATLAB Programming
MATLAB Programming MATLAB Programming
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
- Page 20 and 21: 12 Programming Tips Command and Fun
- Page 22 and 23: Converting Between Strings and Cell
- Page 24 and 25: A External Interfaces Finding the D
- Page 26 and 27: 1 Data Structures Empty Matrices, S
- Page 28 and 29: 1 Data Structures (shown as E below
- Page 30 and 31: 1 Data Structures 0 0 0 0 0 0 0 0 0
- Page 32 and 33: 1 Data Structures 0.6068 0.7621 0.8
- Page 34 and 35: 1 Data Structures 3 5 7 3 5 7 3 5 7
- Page 36 and 37: 1 Data Structures A = 3:0.2:3.8 A =
- Page 38 and 39: 1 Data Structures 5000 50 B = [int8
- Page 40 and 41: 1 Data Structures class(x) ans = si
- Page 42 and 43: 1 Data Structures Matrix Indexing T
- Page 44 and 45: 1 Data Structures A used in the pre
- Page 46 and 47: 1 Data Structures 16 5 9 4 . . . 12
- Page 48 and 49: 1 Data Structures Getting Informati
- Page 50 and 51: 1 Data Structures Function islogica
- Page 52 and 53: 1 Data Structures This example runs
- Page 54 and 55: 1 Data Structures A = 10 20 30 40 5
- Page 56 and 57: 1 Data Structures This changes matr
- Page 58 and 59: 1 Data Structures A = [1+9i 2-8i 3+
- Page 60 and 61: 1 Data Structures rp = rp + blocksi
- Page 62 and 63: 1 Data Structures A = [1:8; 11:18;
- Page 64 and 65: 1 Data Structures rand('state', 0);
- Page 66 and 67: 1 Data Structures OperatingonDiagon
- Page 68 and 69: 1 Data Structures Empty Matrices, S
- Page 72 and 73: 1 Data Structures Use the isscalar
- Page 74 and 75: 1 Data Structures Full and Sparse M
- Page 76 and 77: 1 Data Structures Multidimensional
- Page 78 and 79: 1 Data Structures As you add dimens
- Page 80 and 81: 1 Data Structures Generating Arrays
- Page 82 and 83: 1 Data Structures • size — Retu
- Page 84 and 85: 1 Data Structures For example, cons
- Page 86 and 87: 1 Data Structures Avoiding Ambiguit
- Page 88 and 89: 1 Data Structures Permuting Array D
- Page 90 and 91: 1 Data Structures Computing with Mu
- Page 92 and 93: 1 Data Structures • As multivaria
- Page 94 and 95: 1 Data Structures A{2,2} = 7; B{1,1
- Page 96 and 97: 1 Data Structures Summary of Matrix
- Page 98 and 99: 1 Data Structures Functions to Dete
- Page 100 and 101: 1 Data Structures 1-76
- Page 102 and 103: 2 Data Types Cell Arrays (p. 2-94)
- Page 104 and 105: 2 Data Types Data Type Summary The
- Page 106 and 107: 2 Data Types Numeric Types Numeric
- Page 108 and 109: 2 Data Types the two equally nearby
- Page 110 and 111: 2 Data Types x = x = 127 -128 Also,
- Page 112 and 113: 2 Data Types Set the warning state
- Page 114 and 115: 2 Data Types Floating-Point Numbers
- Page 116 and 117: 2 Data Types isfloat(x) ans = 1 You
- Page 118 and 119: 2 Data Types In this example, 7.5 d
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