MATLAB: How to use a vector as an input in a function function vectorMATLAB dear all i wanna creat a function which use a vector and a digit as inputs. it is as below: function [z]=(x,[m,n,o,p]) z=x+m+n+o+p; end but it doesn't work! how can i use a vector as inupt? thanks alot Best Answer See the documentation on Function Basics (link). Your code has a number of errors. This corrected version works: function [z]=f(x,v) % Define & Code Function ‘f’z = x+v(1)+v(2)+v(3)+v(4);endz = f(42,[5 12 3 7]) % Call Function ‘f’I will let you explore the reasons it does. Note that if you are simply summing the elements of vector ‘v’, use the sum function. However, since I believe this is not your actual problem, I am summing the elements individually. Related SolutionsMATLAB: Move element to the end of the vector. Your loop index ii is not being used. There is no need to use a loop anyway. It can be done in one line:function w=moveme(v,a) w=[v(v~=a), v(v==a)]; MATLAB: For loop within a function A function is like any other script file, except it is saved as a function.For example, to get the sum of the elements of a vector, this is one option using a for loop inside a function:function p = vector_sum(x)p = 0;for k1 = 1:length(x) p = p + x(k1);endendThen call it as:z = 1:10;sum_from_1_to_10 = vector_sum(z)You can adapt this idea to calculate the factorial for your assignment.See the documentation for Function Basics for details. Related QuestionHowwould you in put this in to math lab with functionsHow to 3D plot functionHow to insert a function into anonymous functionFunctionsSum question in Matlab
Best Answer