What does the variable i in for loops stand for? - loops

Having trouble understanding what this variable is and where it gets defined:
for i in range (0, 5):
print i
Prints out number 0 - 4 like expected, but I don't understand what i means.

It's just a variable that assumes the values of the elements of an iterable object.

i is just a name chosen for the variable that holds the current array index in each loop iteration.
This is not hard coded, you can choose any name you want:
for someOtherName in range (0, 5):
print someOtherName
i is very traditional, probably comes from "index".
If you are iterating over something other than an integer index, or think your program could benefit from a more context-bearing name (such as in nested or very complex loops), you should probably give it a different name.

i is just a variable which takes values in the range. It could have been named anything within the rule defined by programming language-grammar.
This variable i is supposed to take values in the range 0 to 5 in the given code.
So, i will iterate from 0 to 4.
5 is exclusive from i as we're talking about range function which excludes the right-bound(right hand side limit).

Nothing, its just a local variable. You could call it just about anything you wanted and use that name in your loop instead.

Because when you have nested for loops the second one usually uses j as its variable and for loops, in general, can be visualized to be iterating through vectors or matrices (if nested), it could be a reference to linear algebra, where there are I and J to represent the two axes as the basis vectors.

For me, i = iteration. That's how I grab it.

From my Knowledge, It comes from FORTRAN, where variables starting with "i"
through "n" were integers.
This is how our developers started using "i" as a standard loop counter over 60 years ago

Related

How do I generate a string array in MatLab 2016b? [duplicate]

When trying to run my code, for example
for ii= 1:10
output(ii)=rand(3);
end
I get the error
In an assignment A(:) = B, the number of elements in A and B must be the same
or
In an assignment A(I) = B, the number of elements in B and I must be the same.
What does this error mean? What is the approach to get rid of it?
This error comes because you are trying to fill a variable chunk with more (or less) values than its size. In other words, you have a statement A(:)=B on where size(A(:)) is different to size(B).
In the example in the question, rand(3) returns a 3x3 matrix, however, output(ii) is just a single value (even if output may be bigger, output(ii) is just a single value of output), thus the value returned by rand(3) does not fit inside output.
In order to solve this problem, you need to change the the size of the output variable, so you have space to fit all the result.
There are 2 ways of doing this. One of them is by creating a Matrix that fits the return, e.g. output=zeros(3,3,10).
Then we can change the code to
for ii= 1:10
output(:,:,ii)=rand(3);
end
Alternatively, you can fill the output as a cell array. This is particularly useful when the return of the function changes sizes each time, e.g. rand(ii);
In that case, the following would work
for ii= 1:10
output{ii}=rand(ii);
end
It is probable that unlike in the example in the question, in the real case you do not know the size of what the output returns, thus you do not know which of the two options to use to fix your code.
On possible way of learning that, is activating debugging help when the code errors, by typing dbstop if error in your command line. This will trigger a debugging stop when MATLAB throws an error, and you can type size(rand(ii)) and size(output(ii)) to see the sizes of both.
Often, reading the documentation of the function being used also helps, to see if different sizes are possible.
That said, the second option, cell arrays, will always ensure everything will fit. However matrices are generally a faster and easier to use in MATLAB, thus you should aim for the matrix based solution if you can.

How can I write 32 std_logic_vetor words to an array in VHDL

I need to write 32 std_logic_vetor-s to an array
kind of like
data_array <= {0x0000, 0xA0F1, 0xFFF1, 0xBFB1, 0x1F9A, 0x03CD,...}
I have looked through some examples and everyone is doing it word-by-word.
Do I need a loop for that?
What if they are constants, do I have to write those dozens of strings?
Correct syntax is:
data_array <= (x"0000", x"A0F1", x"FFF1", x"BFB1", x"1F9A", x"03CD",...)
Whether you can use a for loop or not depends on how your std_logic_vectors are provided. If you take them from another array or a concatenated std_logic_vector, you can use a loop.
If they are constants, you have to define them somewhere, so yes, you have to write them down (or calculate them using a function, if that is appropriate).

In an assignment A(:) = B, the number of elements in A and B must be the same

When trying to run my code, for example
for ii= 1:10
output(ii)=rand(3);
end
I get the error
In an assignment A(:) = B, the number of elements in A and B must be the same
or
In an assignment A(I) = B, the number of elements in B and I must be the same.
What does this error mean? What is the approach to get rid of it?
This error comes because you are trying to fill a variable chunk with more (or less) values than its size. In other words, you have a statement A(:)=B on where size(A(:)) is different to size(B).
In the example in the question, rand(3) returns a 3x3 matrix, however, output(ii) is just a single value (even if output may be bigger, output(ii) is just a single value of output), thus the value returned by rand(3) does not fit inside output.
In order to solve this problem, you need to change the the size of the output variable, so you have space to fit all the result.
There are 2 ways of doing this. One of them is by creating a Matrix that fits the return, e.g. output=zeros(3,3,10).
Then we can change the code to
for ii= 1:10
output(:,:,ii)=rand(3);
end
Alternatively, you can fill the output as a cell array. This is particularly useful when the return of the function changes sizes each time, e.g. rand(ii);
In that case, the following would work
for ii= 1:10
output{ii}=rand(ii);
end
It is probable that unlike in the example in the question, in the real case you do not know the size of what the output returns, thus you do not know which of the two options to use to fix your code.
On possible way of learning that, is activating debugging help when the code errors, by typing dbstop if error in your command line. This will trigger a debugging stop when MATLAB throws an error, and you can type size(rand(ii)) and size(output(ii)) to see the sizes of both.
Often, reading the documentation of the function being used also helps, to see if different sizes are possible.
That said, the second option, cell arrays, will always ensure everything will fit. However matrices are generally a faster and easier to use in MATLAB, thus you should aim for the matrix based solution if you can.

What is the name for the iterative paramater in a for loop in context of graphs?

Suppose you have a function that iterates x value over some kind of a range or an interval, like so:
for (x = MIN_CONST; x <= MAX_CONST; $x += STEP_CONST)
{
//x is a ____ (what)?
y = library_function(x);
print y
}
An x value (out of sequential x values assigned inside the loop) can be seen as a "probe" or an "iterant", or "something".
I am looking for a defined, (or a better) name for this value.
If you like you can imagine a teacher explaining Newton's Method to students, and each x value gotten via the method would be called _____ what?
The general word for this is loop control variable. See: loop control variable on Wikipedia
However, note that the two examples you are giving are not quite the same.
In the first case (i.e. in the for-loop) I would say that your x is basically a loop counter that you are using as an input argument for a function. (Although loop counter is not quite precise, since you are not requiring it to be an integer.). If you call it a loop control variable or a function argument/parameter/input depends merely from how you look at it - the way you are using it it is both.
OTOH in the example with the newton method where the x variable at step i, a.k.a. x_i, is called the i-th approximation or i-th iteration of x. At least it is what they call it in the wikipedia article about iterative methods. I think our professor usually just said "the x_i".
Personally, I often call them the running variable (probably just a literal translation from my german-speaking background 'Laufvariable') or simply the loop variable - but I don't think these are official terms or anything. Still people usually understand what I mean.

Arrays and derived types

For my new project, I have to use an array instead of a scratch file to store information from users. To do this, I need to create derived types, too.
However, I haven't understood what an array is and what a derived type is, how to use them, what they can do, and some other basic ideas.
Can anyone give me some information about array and derived types?
I wrote code for them, but I don't know it is written correctly.
If anyone can check this for me, I would appreciate it.
Here are my array and derived types:
! derived type
TYPE Bank
INTEGER :: acNumber, acChecks
REAL :: acBlance, acRate
CHARACTER :: acType*1, acLName*15, acFName*15
END TYPE
! array
INTEGER, PARAMETER :: MaxRow, MaxColum = 7
INTEGER, DIMENSION(MaxRow:MaxColum) :: AccountData
If you are a fortran programmer you have probably seen a subroutine accepting 10/15 arguments. If you think about it, it's insane (they are too many, you run the risk of swapping them) and you quickly realize that some arguments always travel together.
It would make sense to pack them under a single entity that carries everything around as a whole, non as independent entities. This would reduce the number of arguments considerably, giving you only the burden to find proper association. This single entity is the type.
In your code, you say that a Bank is an aggregate of those informations. You can now declare a concrete variable of that type, which will represent and provide access to the single variables acNumber, acChecks and so on. In order to do so, you have to use the % symbol. so if your bank variable is called b, you can say for example
b%acNumber = 5
You can imagine b as a closet, containing different shelves. You move the closed, all the shelves and their content move together.
An array is a group of entities of the same type (say, integer, or Character(len=1024), or Bank) and they are one after another so you can access each of them with a numeric index. Remember that, unless specified differently, arrays indexes in fortran start at 1 (in all the other major languages, the first index is zero instead)
As for your code, I suggest you to:
write
INTEGER, DIMENSION(MaxRow:MaxColum) :: AccountData
as
INTEGER :: AccountData(MaxRow,MaxColum)
it is the same, but you write less. Please also note that there is a difference between using the : and the ,. If you want to define a matrix (your case), which is a two-dimension array, you have to use the comma. What you wrote is wrong.
for the strings, it's better if you write
CHARACTER :: acType*1, acLName*15, acFName*15
as
CHARACTER(LEN=1) :: acType
CHARACTER(LEN=15) :: acLName
CHARACTER(LEN=15) :: acFName
in this case, you write more, but your syntax is deprecated (I could be wrong, though)
Also, remember that it's better if you write one member variable per line in the types. It's a matter of taste, but I prefer to see the full size of a type by having one line per member variable.
For MaxRows and MaxColumns, I would write them as MAX_ROWS and MAX_COLUMNS. Parameters and stuff that is highly constant by tradition is identified with an all capital, underscore separated name in any major language.
Edit: to answer your comment, here is an example of the use of an array
$ more foo.f90
program test
integer :: myarray(10)
myarray = 0 ! equivalent to zeroing the single elements one by one
myarray(2) = 5
myarray(7) = 10
print *, myarray
end program
$ g95 foo.f90 -o foo
$ ./foo
0 5 0 0 0 0 10 0 0 0
an array is just like multiple variables with the same name, identified by an index. Very useful to express vectors, or matrices.
You can of course do an array of an aggregated type you define, instead of a predefined type (eg. integer).
An array is an ordered list of variables, all of the same type, indexed by integers. See Array in Wikipedia Note that in Fortran array indexing is more flexible than most other low level languages, in that instead of a single index per dimension, you can have an index triplet consisting of lower bound, upper bound, and stride. In that case the lvalue of the expression is a subarray rather than a single element of the array type.
A derived type is a composite type defined by the users, which is made up of multiple components which can be of different types. In some other languages these are knows as structs, structure types, or record types. See Record in Wikipedia
You can also make an array of a derived type, or you can have a derived type where one or more components are themselves arrays, or for that matter, other derived types. It's up to you!
The easiest way to check your code is to try to compile it. Making it past the compiler is of course no guarantee that the program works as expected, but it certainly is a required step.

Resources