What is contained in array arr1 after each of the following sets of state- ments
ID: 3691575 • Letter: W
Question
What is contained in array arr1 after each of the following sets of state- ments are executed?
(a) arr1 = [1 2 3 4; 5 6 7 8; 9 10 11 12]; mask = mod(arr1,2) == 0;
arr1(mask) = -arr1(mask);
(b) arr1 = [1 2 3 4; 5 6 7 8; 9 10 11 12]; arr2 = arr1 <= 5;
arr1(arr2) = 0;
arr1(~arr2) = arr1(~arr2).^2;
the problem is i am quite confused what to do after coding that into the scripit. What am i suppose to do?(do i put comment into the script with answer?) I am quite confused because my teacher assigned this problem and not sure what sort of .m he wants it in. Please explain and if sample coding is used, that will be appriciated.
Explanation / Answer
a.
arr1 = [1 2 3 4; 5 6 7 8; 9 10 11 12]; %Creates a 2-D array of size 3x4.
mask = mod(arr1,2) == 0; %Creates an array mask, with 0 in position of odd elements in arr1, and 1 in position of even elements in arr1.
%So, the mask will be:
%0 1 0 1
%0 1 0 1
%0 1 0 1
arr1(mask) = -arr1(mask) %Updates arr1, with negative values, only for even numbers, or for which there are 1's in mask.
%So, new arr1 values will be:
%1 -2 3 -4
%5 -6 7 -8
%9 -10 11 -12
b.
arr1 = [1 2 3 4; 5 6 7 8; 9 10 11 12]; %Creates the same array as previously.
arr2 = arr1 <= 5; %Creates arr2 with 1's in position for all values that in arr1, that are less than or equal to 5, and 0's otherwise.
arr1(arr2) = 0; %Updates all arr1, with 0's for which there are 1's in arr2.
arr1(~arr2) = arr1(~arr2).^2 %Updates arr1, with squares of the numbers.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.