Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab logical indexing Generate a vector hav ing linearly spaced values between

ID: 3764932 • Letter: M

Question

Matlab logical indexing

Generate a vector hav ing linearly spaced values between -2 and 2. There should he at least 11 values. Keep a copy of this original vector. With a second copy of this vector, clip its values to lie between -1 and 1. This means that any value that is (algebraically) smaller than -1 should be replaced with -1 and that any value larger than 1 should be replaced with 1. If this is done correctly, the resulting vector should have no elements with an absolute value larger than 1. Accomplish this task with logical indexing. Use formatted printing to print a table that compares the values of the original vector with those of the modified vector. This table should have two columns: the first column should contain values from the original vector, and the second column should contain values from the modified vector.

Explanation / Answer

count = 11
origvec = linspace(-2,2,count)
clipvec = origvec
clipvec(origvec > 1) = 1
clipvec(origvec < -1) = -1
disp("Original Vector : Clipped Vector")
for i = 1 : count
    fprintf("%f : %f ",origvec(i), clipvec(i))
end

-------------output-----------------