How would you automate this code?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Armando MAROZZI
el 9 de Feb. de 2021
Comentada: Armando MAROZZI
el 9 de Feb. de 2021
I have the following situation:
d = [3 39 54 53 6];
n = 103;
What I would like to do is to have a code where: when the sum of the elements in d is bigger than n, then I subtract one from the first column in d that is equal or bigger than n.
In the example above, I would have:
d = [3 39 54 *52* 6];
% since the first column where sum(d) is bigger than n is the fourth column, therefore I subtract 1 from 52
How can I automate this?
Thanks!
0 comentarios
Respuesta aceptada
Rik
el 9 de Feb. de 2021
Th cumsum and find functions should be useful. Let me know if you have trouble implementing it.
3 comentarios
Rik
el 9 de Feb. de 2021
You're welcome.
Loops are an excellent first step. Matlab is good at optimizing code that is easy to interpret. Loops are the best choice if there isn't a direct function available. Using cellfun (except for the legacy syntax) or arrayfun only hides the loop and tends to have worse performance.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!