• Matlab TutorialsNo Comments

    default thumbnail

    The logical vectors created from logical and relational operations can be used to subarrays. Suppose X is an ordinary and L is a of the same size that is the result of some logical operation. Then X(L) specifies the elements of X the elements of L are nonzero.

    This kind of subscripting can be done in one step by specifying the logical operation as the subscripting expression. Suppose you have the following 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 a missing observation, such as a failure to respond to an item on a questionnaire. To remove the missing data with logical indexing, use finite(x), which is true all finite numerical values and false NaN and Inf.

    x = x(finite(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 .

    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 , highlight the location of the prime numbers in Dürer’s square by using logical indexing and scalar expansion to set the nonprimes to 0.

    A(~isprime(A)) = 0
    A =
         0     3     2    13
         5     0    11     0
         0     0     7     0
         0     0     0     0

     

    You might also be interested in :  diary

    Comment closed!