Question

In: Computer Science

[In MATLAB] We have an array of seven values for a number x : [ 1.92,...

[In MATLAB] We have an array of seven values for a number x : [ 1.92, 0.05, -2.43, -0.02, 0.09, 0.85 . 0.06] . We consider any data value in the range -0.1 <x<0.1 as erroneous value. We want to remove all such values and replace them with zeros at the end of the array.

  1. Write a script using a for loop with conditional statements
  2. Write a script using the find function

Solutions

Expert Solution

MATLAB Code:

% Code for part a
x = [ 1.92, 0.05, -2.43, -0.02, 0.09, 0.85 , 0.06];
result = [];

% err variable stores the count of erroneous value
err = 0;

% If erroneous value is found then increment by 1 else append the value to
% the result array
for i = 1:7
    if (x(i) < 0.1) && (x(i) > -0.1)
        err = err + 1;
    else
        result(end+1) = x(i);
    end 
end

% Fill rest with zeros
for i = 1:err
    result(end+1) = 0;
end

% Display the result
fprintf("The modified array for part a is:")
disp(result);






% Code for part b
x = [ 1.92, 0.05, -2.43, -0.02, 0.09, 0.85 , 0.06];

% Find the indexes of erroneous values
k = find( (-0.1 < x) & (x < 0.1) );

% err variable stores the count of erroneous value
err = length(k);

% Remove those erroneous values
x(k) = [];

% Append rest with zeros
x = [x zeros(1,err)];

% Display the output
fprintf("The modified array for part b is:")
disp(x);

Please refer to the following picture for sample execution of the above code:


Related Solutions

Suppose we have the following values on the number of customers (X) and the average profits...
Suppose we have the following values on the number of customers (X) and the average profits (Y) for fifteen stores: Store                   Customers (X)          Average Profits (Y) A                               161                              157 B                               99                              93 C                               135                              136 D                               120                              123 E                                164                              153 F                                221                              241 G                               179                              201 H                               204                              206 I                                 214                              229 J                                 101                              135 K                               231                              224 L                                206                              195 M                               248                              242 N      ...
Suppose we have the following values on the number of customers (X) and the average profits...
Suppose we have the following values on the number of customers (X) and the average profits (Y) for fifteen stores: Store                   Customers (X)            Average Profits (Y) A                               161                              157 B                               99                              93 C                               135                              136 D                               120                              123 E                                164                              153 F                                221                              241 G                               179                              201 H                               204                              206 I                                 214                              229 J                                101                              135 K                               231     ...
Suppose that we have a single input variable X and all possible values of X is...
Suppose that we have a single input variable X and all possible values of X is {0, 1, 2}. Also, suppose that we have two groups for outcomes (i.e., Y = 1, 2). It is known that the X|Y = 1 has Binomial(2, 0.7) and X|Y = 2 has the discrete uniform. In addition, P(Y = 1) = 0.3. (1) Predict Y for X = 0, 1, 2, respectively, using the Bayes classifier. (2) Compute the overall Bayes error rate.
Write a class DataSet that stores a number of values of type double in an array...
Write a class DataSet that stores a number of values of type double in an array list. Provide methods to insert a value to the array list and to compute the sum, average, and maximum value. Please put a note on what you did is for what. So I can understand this question better. Code provided: import java.util.*; public class DataSet {    private ArrayList<Double> data; /** Constructs an empty data set. @param maximumNumberOfValues the maximum this data set can...
We are given a non sorted array so that the values are not pairwise disjoint (the...
We are given a non sorted array so that the values are not pairwise disjoint (the same values may appear in many entries). Say that we can find the k-th smallest element number in the array in time O(n) for any 1 <= k <= n. Give an algorithm that finds if there is a value that appears at least n/3 times. Please explain the algorithm in words and analyze the run time.
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
Matlab Given an array of monthly rainfall that covers some number of years (where each column...
Matlab Given an array of monthly rainfall that covers some number of years (where each column is a month and each row is a year) create a function YEARLY that prints the average rainfall for each year For example, if the information below were to be stored in a 5x12 matrix RAIN... J F M A M J J A S O N D 2003 1 2 1 2 1 2 1 2 1 2 1 2 2004 1 1...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to sort it in ascending order
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT