I am trying to run a for loop in Julia using bounds for integration where fI
and r
are arrays of the same length. I know this is incorrect, but this is the gist of what I want to do.
a = zeros(1:length(fI))
for i = 1:length(fI)
a[i] = (fI[i+1] - fI[i])/(r[i+1] - r[i])
end
How can I set increments of n+1 in Julia? Haven't had any luck finding the answer elsewhere.
Just let me know if I can clarify anything. I'm still pretty new to the language.
A for loop doesn't increment anything. Your code used in the for statement does. It's entirely up to you how/if/where/when you want to modify i or any other variable for that matter.
Incrementing two variables is bug prone, especially if you only test for one of them. For loops are meant for cases where your loop runs on one increasing/decreasing variable. For any other variable, change it in the loop.
Also for loops have an option of initializing the variable. Thus no need to write any incrementing/decrementing step inside the loop body like we do in while and do… while loops.
Python increment operator In python, if you want to increment a variable we can use “+=” or we can simply reassign it “x=x+1” to increment a variable value by 1.
Ranges are specified by start:stepsize:end
. Thus the answer is for i = 1:(n+1):length(fI)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With