Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using increments in a for loop

Tags:

julia

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.

like image 619
ellielinc Avatar asked Aug 03 '17 20:08

ellielinc


People also ask

How do you increment a for loop?

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.

Can you increment 2 variables in a for loop?

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.

Does a for loop need an increment?

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.

How do you increment a number in a for loop in Python?

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.


1 Answers

Ranges are specified by start:stepsize:end. Thus the answer is for i = 1:(n+1):length(fI).

like image 99
Chris Rackauckas Avatar answered Oct 04 '22 21:10

Chris Rackauckas