Question

In: Computer Science

Mathlab Q1. Instruction Text % For loop code: vec = [45, -1, 7, 0, -37, 4,...

Mathlab

Q1. Instruction Text

% For loop code:

vec = [45, -1, 7, 0, -37, 4, -3];

newvec = zeros(1,numel(vec)); % pre-allocate newvec with zeros

for idx = 1:numel(vec)

if vec(idx) > 1 & vec(idx) < 0

numerator = 3*vec(idx)^3;

denominator = 9*vec(idx)^2 + 3;

else

numerator = 2*vec(idx)^3 - 2*vec(idx);

denominator = 2*vec(idx)^2 - 2/vec(idx);

end

newvec(idx) = numerator/denominator;

end

Solutions

Expert Solution

clc;

vec = [45, -1, 7, 0, -37, 4, -3]; %Initialize vector with values

newvec = zeros(1,numel(vec)); % pre-allocate newvec with zeros

% For loop code:

for idx = 1:numel(vec) %iterate through each value in vec

if vec(idx) > 1 & vec(idx) < 0

numerator = 3*vec(idx)^3;

denominator = 9*vec(idx)^2 + 3;

else

numerator = 2*vec(idx)^3 - 2*vec(idx);

denominator = 2*vec(idx)^2 - 2/vec(idx);

end

newvec(idx) = numerator/denominator;

end


Related Solutions

hat does the code below do? clear vec = randi(20,1,100)-10; iter=0; k=1; while k<= length(vec) if...
hat does the code below do? clear vec = randi(20,1,100)-10; iter=0; k=1; while k<= length(vec) if vec(k)<0 vec(k)= vec(k) * -1; end k = k+1; end a. This code generates a vector with 100 random integer elements between 20 and 100, and changes the sign of the elements coverting all poivitive ones to negative and all negative ones to positive. b. This code generates a vector with 20 random integer elements between 1 and 100, and changes the sign of...
Correct the code to prints the following: 0 1 2 3 4 5 6 7 8...
Correct the code to prints the following: 0 1 2 3 4 5 6 7 8 9 int[] numbers = new int[10]; for(int i=0; i < numbers.length; ++i)         numbers[i] = i * 3; for(int i=0; i < numbers.length; ++i)         System.out.print(numbers[i] / 2 + 1 + " "); System.out.println();
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1...
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1 0 2 11 5 11 27 4 0 6 Create Standard Deviation Chart (Normal Distribution Curve)
True and False 1. The instruction register stores machine code for the instruction being executed. 2....
True and False 1. The instruction register stores machine code for the instruction being executed. 2. Before a digital computer may execute an instruction, the instruction code must be fetched from memory. 3. A pointer is a binary code for data in the arithmetic logic unit. 4. Von Neumann computer architecture stores data and instruction codes in the same memory. 5. Complex instruction set computers have instructions with greater speed than those in reduced instruction set computers.
MIPS assembly code: procedure:                   addi $s0,$zero,0 loop:                   slt
MIPS assembly code: procedure:                   addi $s0,$zero,0 loop:                   slti $t1, $s0, 7                   beq $t1, $zero, exit                   addi $s0,$s0,1                   j loop exit:                   add $v0, $zero, $s0                   jr $ra       What is the corresponding high-level programming language code? Write a code in your preferred language. What is the result? (5 pts) Code: int add(int a, int b) {       return a+b; } int sub(int a, int b) {      ...
A= 1 0 -7 7 0 1 0 0 2 -2 10 -7 2 -2 2...
A= 1 0 -7 7 0 1 0 0 2 -2 10 -7 2 -2 2 1 Diagonalize the matrix above. That is, find matrix D and a nonsingular matrix P such that A = PDP-1 . Use the representation to find the entries of An as a function of n.
(9) Diagonalizing 4 0 1 -7 -5 5. -12 -6 7
(9) Diagonalizing 4 0 1 -7 -5 5. -12 -6 7
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8 −9 6) (1) Count the number of rows that contain negative components. (2) Obtain the inverse of A and count the number of columns that contain even number of positive components. (3) Assign column names (a,b,c,d) to the columns of A. (4) Transform the matrix A into a vector object a by stacking rows. (5) Replace the diagonal components of A with (0,0,2,3). Hint:...
Mother's age 18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,51 Female 1, 0, 2, 2, 3, 4, 7, 3, 2, 4, 7, 1,...
Mother's age 18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,51 Female 1, 0, 2, 2, 3, 4, 7, 3, 2, 4, 7, 1, 6, 4, 5, 3, 1, 4, 0, 1, 1, 1, 0, 1, 0 Use the stem and leaf plots that you previously created to help you draw and label histograms on your scratch paper with bin width of 2 for mothers's age at birth of female students and for mother's age at birth of male students. Make the lower bound of your first bin...
Consider the following reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0,...
Consider the following reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 Find the number of Page Faults with FIFO, Optimal Page Replacement, and LRU with four free frames that are initially empty. Which algorithm gives the minimum number of page faults?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT