using matlab For this problem you will be asked to modify the function below as
ID: 3801413 • Letter: U
Question
using matlab
Explanation / Answer
Matlab function function_example.m
function [result,user_sel_out] = function_example(array1,array2,user_sel_in)
% INPUTS: array1 = is an array of length N
% array2 = is an array of length N
% user_sel_in = An integer either 1 or 2
% OUTPUTS: result = an array of length N obtained after performing
% the operation specified by the user through user_sel_in
% user_sel_out = is an string indicating the operation performed
% among the first two arguments
N = length(array1);
if(N~=length(array2)) % if the length of the array1 and array2 are not same
error('First two arguments must be of same length'); % operation cannot be performed
end
if(user_sel_in == 1) % if user_sel_in is one
user_sel_out = 'addition'; % user_sel_out will be addition
result = array1+array2; % and perform addition of array1 and array2
elseif(user_sel_in == 2) % if user_sel_in is two
user_sel_out = 'subtraction';% user_sel_out will be subtraction
result = array2-array1;% and perform subtraction of array2 from array1
else % if the user_sel_out not 1 and 2
error('Third argument must be either 1 or 2'); % print an error message
end
end
Testing the function
>> array1 = [1 2 3 4 5 6];
>> array2 = [1 1 1 1 1 1];
>> user_sel_in = 1;
>> [result,user_sel_out] = function_example(array1,array2,user_sel_in)
result =
2 3 4 5 6 7
user_sel_out =
addition
>> user_sel_in = 2;
>> [result,user_sel_out] = function_example(array1,array2,user_sel_in)
result =
0 -1 -2 -3 -4 -5
user_sel_out =
subtraction
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.