Often, certain data entries in a data set can be missing, corrupted, or invalid.
ID: 3747046 • Letter: O
Question
Often, certain data entries in a data set can be missing, corrupted, or invalid. Computers often assign such entries with value of NaN (which stands for not-a number). In MATLAB, the NaN functions similarly to numbers. For example, you could create a MATLAB array [2:5 NaN 7 8J. To see an important attribute of NaN that is different than a number, type NaN NaN into the command window and observe the result. The below steps require you to use MATLAB features that can be helpful when using data sets. Follow the steps below: Note: do not present any of the arrays in the command window unless requested. 5. Create a 2D data array using the lines below (write them exactly as written below): rng(2); % line 1 A-randi( 10, 1000, 1000); % line 2 A(randi( 1e6,20,1))-NaNi % line 3 Describe in words (in the command window) what line 3 in part a does. Create a vector, prob5c, containing the column indices of A which contain at least one NaN. Hint re-read the first paragraph of this problem Replace any element of A that is NaN with a 0. Certain elements of A have the following characteristics: an even row number, an odd column number, and a value that is less than or equal to 5. Copy matrix A into a matrix B and replace each element which has every listed attribute with -41 a. b. c. d. e. f. Calculate the sum of the values in the matrix B (1 number).Explanation / Answer
MATLAB CODE:
a)
>> rng(2);
A=randi(10,1000,1000);
>> A(randi(1e6,20,1))=NaN;
When executed, the array A will create an 2D array with 1000 rows and 1000 column.
b)
Line 2 A=randi(10,1000,1000); of the code will an 2D array with 1000 rows and 1000 column.
All the values of the array will be less than 10.
Line 3 A(randi(1e6,20,1))=NaN; of the code with will generate a number for all the vales of the array where the value is NaN.
c)
A = randi(15,4,4);
A(randi(3,4,1)) = NaN
for k = 1:4
for l = 1:4
if (isnan(A(k,l)) == true)
fprintf('%d %d ',k,l);
l = l + 1;
end
end
end
d.
A = randi(15,4,4);
A(randi(3,4,1)) = NaN
for k = 1:4
for l = 1:4
if (isnan(A(k,l)) == true)
fprintf('%d %d ',k,k);
l = l + 1;
end
end
end
m = 0;
for k = 1:4
for l = 1:4
if (isnan(A(k,l)) == true)
A(k,l) = 0
m = 1;
break;
end
end
if (m == 1)
break;
end
end
e.
A = randi(15,4,4);
A(randi(3,4,1)) = NaN
m = 0;
for k = 1:4
for l = 1:4
if (rem(k,2) == 0 && rem(l,2) == 1 && 5 >=A(k,l))
A(k,l) = -41
m =m+1;
end
end
end
f.
z = nansum(A)
m=sum(z)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.