solve this using matlab. you can use these functions (if), (switch-case), while
ID: 2249230 • Letter: S
Question
solve this using matlab. you can use these functions (if), (switch-case), while loop, for loop, nested loop, infinite loop, continue, break, do not get too deep please! answer must be short
Problem 5) Write a function to perform binary shift right operation. Your function will accept a binary vector (a sequence of Os and 1s), and will shift the digits in your binary vector one digit to the right. The function will replace the first digit with one 0. Example: For input x [ 1 0 1 1 0 0 0 1], the output y is [ 0 1 0 1 1000] Note: Your function should check for the input vector to be a binary vector. That is all the elements in your input vector should be either 0 or 1.If your vector is not a binary vector, the function will return y = 'Error'.Explanation / Answer
Matlab Script:
function y = bin_right_sft_op(x)
n = x~=1 & x~=0;%return 1 for each element if it is not equal 1 or 0 for ex x=[1 2 1 1] n will be [0 1 0 0]
sum_n = sum(n);%if any value of x not equal to 1 or 0 then sum will be greater than zero
if sum_n==0
y = [0 x(1:length(x)-1)];%x(1:length(x)-1) gives all the elements except last one
else
y='Error';
end
end
command window output:
>> y = bin_right_sft_op([1 0 2 -1 -5])
y =
Error
>> y = bin_right_sft_op([1 0 1 1 0 0 0 1])
y =
0 1 0 1 1 0 0 0
>>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.