function that takes as input an array and prints out the product of array in matlab

You can define a function that takes an array as input and returns the product of its elements. Here's an example:

main.m
function out = arrayProduct(arr)
% ARRAYPRODUCT computes the product of an array
% Inputs:
%   arr: a numeric array
% Output:
%   out: the product of the array elements
out = 1;
for i = 1:numel(arr)
    out = out * arr(i);
end
fprintf('The product is: %d\n', out);
end
269 chars
13 lines

You can then call this function by passing your array as an argument:

main.m
>> myArr = [2, 3, 4, 5];
>> arrayProduct(myArr)
The product is: 120
68 chars
4 lines

gistlibby LogSnag