How to get the multiplication of elements in a row vector by using for loop

23 visualizaciones (últimos 30 días)
% create a vector
A = [-2,4,9,-5,1];
Mult = 0;
% create a for loop
for i = 1:length(A)
Mult = Mult * A(i) ;
end
% display the result
disp(Mult);

Respuesta aceptada

KSSV
KSSV el 15 de Mzo. de 2022
% create a vector
A = [-2,4,9,-5,1];
Mult = 1; % <----- this should be 1
% create a for loop
for i = 1:length(A)
Mult = Mult * A(i) ;
end
% display the result
disp(Mult);
360
prod(A) % inbuit function
ans = 360
  4 comentarios
Shuoze Xu
Shuoze Xu el 15 de Mzo. de 2022
Ok,bro.
That my first time to use that, i do not know i need to touch the accepting and voting.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by