Colin Nguyen
Colin Nguyen

Reputation: 1

Logical to numerical (matlab)

I'm almost done with the problem, but no matter what I try I can't convert the values from logical to numerical.

Detecting unusual numbers or outliers in a data set is important in many disciplines, because the outliers identify interesting phenomena, extreme events, or invalid experimental results. A simple method to check if a data value is an outlier is to see if the value is a significant number of standard deviations away from the mean of the data set. For example, is an outlier if where is the data set mean, is the data set standard deviation, and is the number of standard deviations deemed significant.
Assign outlierData with all values in userData that are numberStdDevs standard deviations from userData's mean. Hint: use logical indexing to return the outlier data values.
Example: If userData is [9, 50, 51, 49, 100 ] and numberStdDevs is 1, then outlierData is [9, 100].

function outlierData = getOutliers(userData, numberStdDevs)
% getOutliers: Return all elements of input array data that are more than
% numStdDevs standard deviations away from the mean.
%
%   Inputs: userData - array of input data values
%           numberStdDevs - threshold number of standard deviations to
%                           determine whether a particular data value is an outlier
%
%   Outputs: outlierData - array of outlier data values


    % Assign dataMean with the mean of userData
    dataMean = mean(userData);

    % Assign dataStdDev with userData's standard deviation
    dataStdDev = std(userData);

    % Assign outlierData with Return outliers
    outlierData = (abs(userData - dataMean)) > (numberStdDevs * dataStdDev);

end

Check if getOutliers([9, 50, 51, 49, 100 ], 1) returns [9, 100]
I get 1 0 0 0 1
Check if getOutliers([76, 79, 84, 68, 85, 23, 105, 47, 97, 96, 39], 1) returns [23, 105, 39]
I get 0 0 0 0 0 1 1 0 0 0 1
Check if getOutliers([76, 79, 84, 68, 85, 23, 105, 47, 97, 96, 39], 0.5) returns [23, 105, 47, 97, 96, 39]
I get 0 0 0 0 0 1 1 1 1 1 1

Upvotes: 0

Views: 1806

Answers (1)

Y. Chang
Y. Chang

Reputation: 595

You are super close!!! To use the logical indexing in MATLAB, you simply use mydata(logicalIndexVector) to get your new data. In your case, you can modify the last line of your code.

outlierData = userData((abs(userData - dataMean)) > (numberStdDevs * dataStdDev));

I suggest reading this article and follow the practice in the article.

Upvotes: 1

Related Questions