Borrar filtros
Borrar filtros

How to enter multiple values for a single input in a function

36 visualizaciones (últimos 30 días)
Brett Baxter
Brett Baxter el 5 de Sept. de 2020
Comentada: Nasir el 11 de Sept. de 2023
So what I'm trying to do is have a function take in a vector of inputs, and then use them seperately in a function. So the function would be something simple, like adding 1 to every input value, but I want the limit of input values to be practically infinite. So I want to assign the variable x to every component of a vector and add 1 to each component, then at the end I want to obtain the sum of every component in the vector. I don't quite know how else to ask this question, something like
x = [1, 2, 3 . . . .]
answer = sum(x+1)
  5 comentarios
Brett Baxter
Brett Baxter el 5 de Sept. de 2020
I figured it out, I had misplaced some of the '.' to specify that I was multiplying vectors. As well, I just needed to specify that I wanted the sum of the output. So sum(answer) to get it to all add up.
Nasir
Nasir el 11 de Sept. de 2023
can someone tell me about how to enter multiple values in single string
x = [1 2 3 4 5 6 ...............................................1000]

Iniciar sesión para comentar.

Respuestas (1)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam el 5 de Sept. de 2020
Most Matlab functions are vector based. you should use .^ for vector based ^ and .* for vector based *
So you function line looks like this:
answer = sum((x+1).^2 .* mod(x, 2));
and ./ instead of /

Categorías

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

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by