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.
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...
We have an array of numbers, and we start at index 0. At every point, we're...
We have an array of numbers, and we start at index 0. At every point, we're allowed to jump from index i to i+3, i+4, or stop where we are. We'd like to find the maximum possible sum of the numbers we visit. For example, for the array [14, 28, 79, -87, 29, 34, -7, 65, -11, 91, 32, 27, -5], the answer is 140. (starting at the 14, we jump 4 spots to 29, then 3 spots to 65,...
Using an array and a function, print the values of an array backwards. Please follow these...
Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT