matlab基本操作1
-
Scalar Expansion
-
B = A - 8.5; B(1:2,2:3) = 0; //意思都很清楚,举2个例子
Logical Subscripting
- Suppose you have the following set of data:
x = [2.1 1.7 1.6 1.5 NaN 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8];
-
The NaN is a marker for a missing observation, such as a failure to respond to an item on
a questionnaire. To remove the missing data with logical indexing, use
isfinite(x)
,which is true for all finite numerical values and false for NaN and Inf:x = x(isfinite(x)) x = 2.1 1.7 1.6 1.5 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8
-
Now there is one observation, 5.1, which seems to be very different from the others. It is
an outlier. The following statement removes outliers, in this case those elements more
than three standard deviations from the mean:x = x(abs(x-mean(x)) <= 3*std(x)) x = 2.1 1.7 1.6 1.5 1.9 1.8 1.5 1.8 1.4 2.2 1.6 1.8
-
For another example, highlight the location of the prime numbers in Dürer's magic square
by using logical indexing and scalar expansion to set the nonprimes to 0. (See .)A(~isprime(A)) = 0 A = 0 3 2 13 5 0 11 0 0 0 7 0 0 0 0 0
The find Function
-
The find function determines the indices of array elements that meet a given logical
condition. In its simplest form, find returns a column vector of indices. Transpose that
vector to obtain a row vector of indices. For example, start again with Dürer's magic
square. -
The find function determines the indices of array elements that meet a given logical condition. In its simplest form, find returns a column vector of indices. Transpose that vector to obtain a row vector of indices. For example, start again with Dürer's magic square. (See .) k = find(isprime(A))' picks out the locations, using one-dimensional indexing, of the primes in the magic square: k = 2 5 9 10 11 13 Display those primes, as a row vector in the order determined by k, with A(k) ans = 5 3 2 11 7 13 When you use k as a left-side index in an assignment statement, the matrix structure is preserved: A(k) = NaN A = 16 NaN NaN NaN NaN 10 NaN 8 9 6 NaN 12 4 15 14 1
-