Problem-4: Functions Write a function called vectorzip (x,y) that will take as i
ID: 2248062 • Letter: P
Question
Problem-4: Functions Write a function called vectorzip (x,y) that will take as inputs two vectors and return another vector as follows: x(1, 2, 31 >>> zipped >zipped vectorzip(x, y: >>>a -[7, 5, 9,/201 >>> b= [4, 81; >>> zipped = vectorzip (a, b); >> zipped Note that normally, / /you would need to have an error check in your code to check if your input is a vector or not. For this question, we wili/ assume that the user will always enter the proper inputs.Explanation / Answer
Functions may return scalar or matrix values. The first line of such functions are of the form:
function variable = function-name(argument1, argument2,...)
where the variable will be set equal to the output value somewhere in the function definition. Here is an example where y is set to equal the function fliplr.
function y = fliplr(x)
%FLIPLR(X) returns X with row preserved and columns flipped
%in the left/right direction.
% X = 1 2 3 becomes 3 2 1
% 4 5 6 6 5 4
[m,n] = size(x);
y = x(:,n:-1:1);
If you want to return more than one argument, you can do it by having the function return a vector of values. For example, the following function returns two vectors. The first vector is the prime factors less than 10 of the argument. The second indicates how many times each of these factors is used.
function [factors, times] = primefact(n)
%[FACTORS TIMES] = PRIMEFACT(N) find prime factors of n
primes = [2 3 5 7];
for i = 1:4
temp = n;
if ( rem(temp,primes(i)) == 0)
factors = [factors primes(i)];
times = [times 0];
while (rem(temp,primes(i)) == 0)
temp = temp/primes(i);
times(length(times)) = times(length(times))+1;
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.