comparison doc/interpreter/expr.txi @ 5016:bdbee5282954

[project @ 2004-09-22 02:50:35 by jwe]
author jwe
date Wed, 22 Sep 2004 02:50:36 +0000
parents af308ca1a354
children 297b82335c7b
comparison
equal deleted inserted replaced
5015:6d481b6e349e 5016:bdbee5282954
65 @end example 65 @end example
66 66
67 @noindent 67 @noindent
68 and select the first row of the matrix. 68 and select the first row of the matrix.
69 69
70 A special form of indexing may be used to select elements of a matrix or 70 @c FIXED -- sections on variable prefer_zero_one_indexing were removed
71 vector. If the indices are vectors made up of only ones and zeros, the 71
72 result is a new matrix whose elements correspond to the elements of the 72 Indexing a scalar with a vector of ones can be used to create a
73 index vector that are equal to one. For example,
74
75 @example
76 @group
77 a = [1, 2; 3, 4];
78 a ([1, 0], :)
79 @end group
80 @end example
81
82 @noindent
83 selects the first row of the matrix @code{a}.
84
85 This operation can be useful for selecting elements of a matrix based on
86 some condition, since the comparison operators return matrices of ones
87 and zeros.
88
89 This special zero-one form of indexing leads to a conflict with the
90 standard indexing operation. For example, should the following
91 statements
92
93 @example
94 @group
95 a = [1, 2; 3, 4];
96 a ([1, 1], :)
97 @end group
98 @end example
99
100 @noindent
101 return the original matrix, or the matrix formed by selecting the first
102 row twice? Although this conflict is not likely to arise very often in
103 practice, you may select the behavior you prefer by setting the built-in
104 variable @code{prefer_zero_one_indexing}.
105
106 @c XXX FIXME XXX -- this variable no longer exists!
107
108 @defvr {Built-in Variable} prefer_zero_one_indexing
109 If the value of @code{prefer_zero_one_indexing} is nonzero, Octave
110 will perform zero-one style indexing when there is a conflict with the
111 normal indexing rules. @xref{Index Expressions}. For example, given a
112 matrix
113
114 @example
115 a = [1, 2, 3, 4]
116 @end example
117
118 @noindent
119 with @code{prefer_zero_one_indexing} is set to nonzero, the
120 expression
121
122 @example
123 a ([1, 1, 1, 1])
124 @end example
125
126 @noindent
127 results in the matrix @code{[ 1, 2, 3, 4 ]}. If the value of
128 @code{prefer_zero_one_indexing} set to 0, the result would be
129 the matrix @code{[ 1, 1, 1, 1 ]}.
130
131 In the first case, Octave is selecting each element corresponding to a
132 @samp{1} in the index vector. In the second, Octave is selecting the
133 first element multiple times.
134
135 The default value for @code{prefer_zero_one_indexing} is 0.
136 @end defvr
137
138 Finally, indexing a scalar with a vector of ones can be used to create a
139 vector the same size as the index vector, with each element equal to 73 vector the same size as the index vector, with each element equal to
140 the value of the original scalar. For example, the following statements 74 the value of the original scalar. For example, the following statements
141 75
142 @example 76 @example
143 @group 77 @group
888 @example 822 @example
889 A (:, 1:2:5) = [] 823 A (:, 1:2:5) = []
890 @end example 824 @end example
891 825
892 @noindent 826 @noindent
893 deletes the first, third, and fifth columns. 827 deletes the first, second, and fifth columns.
894 828
895 An assignment is an expression, so it has a value. Thus, @code{z = 1} 829 An assignment is an expression, so it has a value. Thus, @code{z = 1}
896 as an expression has the value 1. One consequence of this is that you 830 as an expression has the value 1. One consequence of this is that you
897 can write multiple assignments together: 831 can write multiple assignments together:
898 832