Borrar filtros
Borrar filtros

How to find the largest product?

3 visualizaciones (últimos 30 días)
Abedeera Jayasuriya Seena Ptabedige Nileema
Editada: Jan el 30 de Jul. de 2018
Hi, I am trying to solve this homework. could anyone please help me to solve this?
* Write a function max_product that takes v a vector and n, a positive integer, as inputs and computes the largest product of n consecutive elements of v. It returns the product and the index of the element of v that is the first term of the product. If there are multiple such products in v, the function must return the one with the smallest starting index. As an example, the following call >> [product, ind] = max_product([1 2 2 1 3 1],3); will assign 6 to product and 3 to ind since the max 3-term product in the input vector is 2*1*3. If v has fewer than n elements, the function returns 0 and -1, respectively.
  5 comentarios
Stephen23
Stephen23 el 21 de Nov. de 2017
@Andrei Bobrov: please show examples of using movsum and conv.
Andrei Bobrov
Andrei Bobrov el 21 de Nov. de 2017
Hi Stephen! See my answer.

Iniciar sesión para comentar.

Respuestas (3)

Stephen23
Stephen23 el 21 de Nov. de 2017
Editada: Stephen23 el 21 de Nov. de 2017
function [product,ind] = max_product(a,n)
product = 0;
ind = -1;
num = numel(a);
for k = 1:1+num-n
tmp = prod(a(k:k+n-1));
if tmp>product
product = tmp;
ind = k;
end
end
end
And tested:
>> [product, ind] = max_product([1 2 2 1 3 1],3)
product = 6
ind = 3
>> [product, ind] = max_product([1 2 2 1 3 9],3)
product = 27
ind = 4
>> [product, ind] = max_product([1 2 2 1 3 9],9)
product = 0
ind = -1
This could be vectorized, or you could use movprod, but using a loop makes the algorithm clear.

Jan
Jan el 21 de Nov. de 2017
Editada: Jan el 21 de Nov. de 2017
And a vectorized version:
function [P, Ind] = max_product(v, n)
X = v((1:numel(v) - n + 1) + (0:n-1).');
[P, Ind] = max(prod(X, 1));
end
  6 comentarios
silvia a
silvia a el 30 de Jul. de 2018
Editada: silvia a el 30 de Jul. de 2018
thank you very much for your answer; I have solved it;
I do not know if you guys read these reviews. But I wanted to tell you that your answers and code examples help very much. I am very new to this but trying out your examples really helped me to get started and have a better grasp of things. Thank you!
Jan
Jan el 30 de Jul. de 2018
Editada: Jan el 30 de Jul. de 2018
Dear silvia a, the "guys and girls" here read the comments usually. I'm happy, that I could help you, because this my reason to post answers. You are welcome. I appreciate your feedback, thanks.

Iniciar sesión para comentar.


Andrei Bobrov
Andrei Bobrov el 21 de Nov. de 2017
Editada: Andrei Bobrov el 21 de Nov. de 2017
Op! Everyone solves someone else's homework ... I also want! :)
[product1, ind] = max(prod(hankel(a(1:end-b+1),a(end-b+1:end)),2))
as function:
function [product1,ind]=max_product(a,b)
try
[product1, ind] = max(prod(hankel(a(1:end-b+1),a(end-b+1:end)),2));
catch
product1 = 0;
ind = -1;
end
end
with conv2:
function [product1,ind]=max_product(a,b)
try
[product1, ind] = max(exp(conv2(log(a(:)),ones(b,1),'valid')));
catch
product1 = 0;
ind = -1;
end
end
with movsum
[product1, ind] = max(exp(movsum(log(a),[0, b-1])));

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by