Python - Simple Question: Numpy Matrix, Loop
up vote
0
down vote
favorite
I am fairly new to python and I am trying to get around understanding the following code:
import numpy as np
n=4
matrix=np.zeros((n,n))
for j in range (0,n):
for i in range (n-1,n-j-2,-1):
matrix[i,j]=2*n-i-j-1
print (matrix)
I would greatly appreciate if someone could please help me understand how each line executes and how the code is revaluated with the loop.
Thanks in advance!
python python-2.7 loops numpy matrix
add a comment |
up vote
0
down vote
favorite
I am fairly new to python and I am trying to get around understanding the following code:
import numpy as np
n=4
matrix=np.zeros((n,n))
for j in range (0,n):
for i in range (n-1,n-j-2,-1):
matrix[i,j]=2*n-i-j-1
print (matrix)
I would greatly appreciate if someone could please help me understand how each line executes and how the code is revaluated with the loop.
Thanks in advance!
python python-2.7 loops numpy matrix
In short: It creates a 4 by 4 array where each cell in the lower left triangle has the valuei - j + 1
wherei
is the row index andj
is the column index. You need to be more specific about what you don't understand if that does not solve the issue.
– timgeb
Nov 21 at 23:30
Thanks for your prompt reply! My question is how do I know that the lower left triangle is the one where the matrix formula executes. Sorry if it is way too obvious, but I am a complete begginner
– Aylín Pérez
Nov 21 at 23:33
The outerfor
loop iterates over the row indices. The innerfor
loop iterates over the columns, but only up to the column number that is equal to the current row number.
– timgeb
Nov 21 at 23:34
1
Thank you very much!!
– Aylín Pérez
Nov 21 at 23:47
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am fairly new to python and I am trying to get around understanding the following code:
import numpy as np
n=4
matrix=np.zeros((n,n))
for j in range (0,n):
for i in range (n-1,n-j-2,-1):
matrix[i,j]=2*n-i-j-1
print (matrix)
I would greatly appreciate if someone could please help me understand how each line executes and how the code is revaluated with the loop.
Thanks in advance!
python python-2.7 loops numpy matrix
I am fairly new to python and I am trying to get around understanding the following code:
import numpy as np
n=4
matrix=np.zeros((n,n))
for j in range (0,n):
for i in range (n-1,n-j-2,-1):
matrix[i,j]=2*n-i-j-1
print (matrix)
I would greatly appreciate if someone could please help me understand how each line executes and how the code is revaluated with the loop.
Thanks in advance!
python python-2.7 loops numpy matrix
python python-2.7 loops numpy matrix
edited Nov 22 at 0:41
asked Nov 21 at 23:28
Aylín Pérez
12
12
In short: It creates a 4 by 4 array where each cell in the lower left triangle has the valuei - j + 1
wherei
is the row index andj
is the column index. You need to be more specific about what you don't understand if that does not solve the issue.
– timgeb
Nov 21 at 23:30
Thanks for your prompt reply! My question is how do I know that the lower left triangle is the one where the matrix formula executes. Sorry if it is way too obvious, but I am a complete begginner
– Aylín Pérez
Nov 21 at 23:33
The outerfor
loop iterates over the row indices. The innerfor
loop iterates over the columns, but only up to the column number that is equal to the current row number.
– timgeb
Nov 21 at 23:34
1
Thank you very much!!
– Aylín Pérez
Nov 21 at 23:47
add a comment |
In short: It creates a 4 by 4 array where each cell in the lower left triangle has the valuei - j + 1
wherei
is the row index andj
is the column index. You need to be more specific about what you don't understand if that does not solve the issue.
– timgeb
Nov 21 at 23:30
Thanks for your prompt reply! My question is how do I know that the lower left triangle is the one where the matrix formula executes. Sorry if it is way too obvious, but I am a complete begginner
– Aylín Pérez
Nov 21 at 23:33
The outerfor
loop iterates over the row indices. The innerfor
loop iterates over the columns, but only up to the column number that is equal to the current row number.
– timgeb
Nov 21 at 23:34
1
Thank you very much!!
– Aylín Pérez
Nov 21 at 23:47
In short: It creates a 4 by 4 array where each cell in the lower left triangle has the value
i - j + 1
where i
is the row index and j
is the column index. You need to be more specific about what you don't understand if that does not solve the issue.– timgeb
Nov 21 at 23:30
In short: It creates a 4 by 4 array where each cell in the lower left triangle has the value
i - j + 1
where i
is the row index and j
is the column index. You need to be more specific about what you don't understand if that does not solve the issue.– timgeb
Nov 21 at 23:30
Thanks for your prompt reply! My question is how do I know that the lower left triangle is the one where the matrix formula executes. Sorry if it is way too obvious, but I am a complete begginner
– Aylín Pérez
Nov 21 at 23:33
Thanks for your prompt reply! My question is how do I know that the lower left triangle is the one where the matrix formula executes. Sorry if it is way too obvious, but I am a complete begginner
– Aylín Pérez
Nov 21 at 23:33
The outer
for
loop iterates over the row indices. The inner for
loop iterates over the columns, but only up to the column number that is equal to the current row number.– timgeb
Nov 21 at 23:34
The outer
for
loop iterates over the row indices. The inner for
loop iterates over the columns, but only up to the column number that is equal to the current row number.– timgeb
Nov 21 at 23:34
1
1
Thank you very much!!
– Aylín Pérez
Nov 21 at 23:47
Thank you very much!!
– Aylín Pérez
Nov 21 at 23:47
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
You can add the following print statement, and the loop will explain itself at each iteration:
n=4
matrix=np.zeros((n,n))
for i in range (0,n):
for j in range(0,i+1):
print(f'inserting {i-j+1} into the matrix at row index {i}, columns index {j}')
matrix[i,j]=i-j+1
When you run it, you get this output:
inserting 1 into the matrix at row index 0, columns index 0
inserting 2 into the matrix at row index 1, columns index 0
inserting 1 into the matrix at row index 1, columns index 1
...
inserting 3 into the matrix at row index 3, columns index 1
inserting 2 into the matrix at row index 3, columns index 2
inserting 1 into the matrix at row index 3, columns index 3
And your matrix is populated as before:
>>> matrix
array([[1., 0., 0., 0.],
[2., 1., 0., 0.],
[3., 2., 1., 0.],
[4., 3., 2., 1.]])
Just for reference:
>>> matrix
array([[1., 0., 0., 0.], #<- "row" index 0
[2., 1., 0., 0.], #<- "row" index 1
[3., 2., 1., 0.], #<- "row" index 2
[4., 3., 2., 1.]]) #<- "row" index 3
# ^ ... ^
# "col" 0 "col" 3
2
Thank you very much, this was really helpful!!
– Aylín Pérez
Nov 21 at 23:47
add a comment |
up vote
1
down vote
import numpy as np
n=4
We start by setting a 4x4 matrix with all coordinates set to null:
matrix=np.zeros((n,n))
We set new coordinate values by looping through rows and columns. First we loop through rows, from index 0 to n-1:
for i in range (0,n):
We next loop through columns. Now, notice that we only loop through those columns whose index is smaller than or equal to that of the current row (i.e., from 0 to i). This way we make sure that the values we set are on or below the diagonal of the matrix:
for j in range(0,i+1):
Finally, we set the desired value for the current coordinate:
matrix[i,j]=i-j+1
print(matrix)
Thank you, really helpful!
– Aylín Pérez
Nov 21 at 23:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421873%2fpython-simple-question-numpy-matrix-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can add the following print statement, and the loop will explain itself at each iteration:
n=4
matrix=np.zeros((n,n))
for i in range (0,n):
for j in range(0,i+1):
print(f'inserting {i-j+1} into the matrix at row index {i}, columns index {j}')
matrix[i,j]=i-j+1
When you run it, you get this output:
inserting 1 into the matrix at row index 0, columns index 0
inserting 2 into the matrix at row index 1, columns index 0
inserting 1 into the matrix at row index 1, columns index 1
...
inserting 3 into the matrix at row index 3, columns index 1
inserting 2 into the matrix at row index 3, columns index 2
inserting 1 into the matrix at row index 3, columns index 3
And your matrix is populated as before:
>>> matrix
array([[1., 0., 0., 0.],
[2., 1., 0., 0.],
[3., 2., 1., 0.],
[4., 3., 2., 1.]])
Just for reference:
>>> matrix
array([[1., 0., 0., 0.], #<- "row" index 0
[2., 1., 0., 0.], #<- "row" index 1
[3., 2., 1., 0.], #<- "row" index 2
[4., 3., 2., 1.]]) #<- "row" index 3
# ^ ... ^
# "col" 0 "col" 3
2
Thank you very much, this was really helpful!!
– Aylín Pérez
Nov 21 at 23:47
add a comment |
up vote
2
down vote
You can add the following print statement, and the loop will explain itself at each iteration:
n=4
matrix=np.zeros((n,n))
for i in range (0,n):
for j in range(0,i+1):
print(f'inserting {i-j+1} into the matrix at row index {i}, columns index {j}')
matrix[i,j]=i-j+1
When you run it, you get this output:
inserting 1 into the matrix at row index 0, columns index 0
inserting 2 into the matrix at row index 1, columns index 0
inserting 1 into the matrix at row index 1, columns index 1
...
inserting 3 into the matrix at row index 3, columns index 1
inserting 2 into the matrix at row index 3, columns index 2
inserting 1 into the matrix at row index 3, columns index 3
And your matrix is populated as before:
>>> matrix
array([[1., 0., 0., 0.],
[2., 1., 0., 0.],
[3., 2., 1., 0.],
[4., 3., 2., 1.]])
Just for reference:
>>> matrix
array([[1., 0., 0., 0.], #<- "row" index 0
[2., 1., 0., 0.], #<- "row" index 1
[3., 2., 1., 0.], #<- "row" index 2
[4., 3., 2., 1.]]) #<- "row" index 3
# ^ ... ^
# "col" 0 "col" 3
2
Thank you very much, this was really helpful!!
– Aylín Pérez
Nov 21 at 23:47
add a comment |
up vote
2
down vote
up vote
2
down vote
You can add the following print statement, and the loop will explain itself at each iteration:
n=4
matrix=np.zeros((n,n))
for i in range (0,n):
for j in range(0,i+1):
print(f'inserting {i-j+1} into the matrix at row index {i}, columns index {j}')
matrix[i,j]=i-j+1
When you run it, you get this output:
inserting 1 into the matrix at row index 0, columns index 0
inserting 2 into the matrix at row index 1, columns index 0
inserting 1 into the matrix at row index 1, columns index 1
...
inserting 3 into the matrix at row index 3, columns index 1
inserting 2 into the matrix at row index 3, columns index 2
inserting 1 into the matrix at row index 3, columns index 3
And your matrix is populated as before:
>>> matrix
array([[1., 0., 0., 0.],
[2., 1., 0., 0.],
[3., 2., 1., 0.],
[4., 3., 2., 1.]])
Just for reference:
>>> matrix
array([[1., 0., 0., 0.], #<- "row" index 0
[2., 1., 0., 0.], #<- "row" index 1
[3., 2., 1., 0.], #<- "row" index 2
[4., 3., 2., 1.]]) #<- "row" index 3
# ^ ... ^
# "col" 0 "col" 3
You can add the following print statement, and the loop will explain itself at each iteration:
n=4
matrix=np.zeros((n,n))
for i in range (0,n):
for j in range(0,i+1):
print(f'inserting {i-j+1} into the matrix at row index {i}, columns index {j}')
matrix[i,j]=i-j+1
When you run it, you get this output:
inserting 1 into the matrix at row index 0, columns index 0
inserting 2 into the matrix at row index 1, columns index 0
inserting 1 into the matrix at row index 1, columns index 1
...
inserting 3 into the matrix at row index 3, columns index 1
inserting 2 into the matrix at row index 3, columns index 2
inserting 1 into the matrix at row index 3, columns index 3
And your matrix is populated as before:
>>> matrix
array([[1., 0., 0., 0.],
[2., 1., 0., 0.],
[3., 2., 1., 0.],
[4., 3., 2., 1.]])
Just for reference:
>>> matrix
array([[1., 0., 0., 0.], #<- "row" index 0
[2., 1., 0., 0.], #<- "row" index 1
[3., 2., 1., 0.], #<- "row" index 2
[4., 3., 2., 1.]]) #<- "row" index 3
# ^ ... ^
# "col" 0 "col" 3
answered Nov 21 at 23:33
sacul
29.7k41640
29.7k41640
2
Thank you very much, this was really helpful!!
– Aylín Pérez
Nov 21 at 23:47
add a comment |
2
Thank you very much, this was really helpful!!
– Aylín Pérez
Nov 21 at 23:47
2
2
Thank you very much, this was really helpful!!
– Aylín Pérez
Nov 21 at 23:47
Thank you very much, this was really helpful!!
– Aylín Pérez
Nov 21 at 23:47
add a comment |
up vote
1
down vote
import numpy as np
n=4
We start by setting a 4x4 matrix with all coordinates set to null:
matrix=np.zeros((n,n))
We set new coordinate values by looping through rows and columns. First we loop through rows, from index 0 to n-1:
for i in range (0,n):
We next loop through columns. Now, notice that we only loop through those columns whose index is smaller than or equal to that of the current row (i.e., from 0 to i). This way we make sure that the values we set are on or below the diagonal of the matrix:
for j in range(0,i+1):
Finally, we set the desired value for the current coordinate:
matrix[i,j]=i-j+1
print(matrix)
Thank you, really helpful!
– Aylín Pérez
Nov 21 at 23:53
add a comment |
up vote
1
down vote
import numpy as np
n=4
We start by setting a 4x4 matrix with all coordinates set to null:
matrix=np.zeros((n,n))
We set new coordinate values by looping through rows and columns. First we loop through rows, from index 0 to n-1:
for i in range (0,n):
We next loop through columns. Now, notice that we only loop through those columns whose index is smaller than or equal to that of the current row (i.e., from 0 to i). This way we make sure that the values we set are on or below the diagonal of the matrix:
for j in range(0,i+1):
Finally, we set the desired value for the current coordinate:
matrix[i,j]=i-j+1
print(matrix)
Thank you, really helpful!
– Aylín Pérez
Nov 21 at 23:53
add a comment |
up vote
1
down vote
up vote
1
down vote
import numpy as np
n=4
We start by setting a 4x4 matrix with all coordinates set to null:
matrix=np.zeros((n,n))
We set new coordinate values by looping through rows and columns. First we loop through rows, from index 0 to n-1:
for i in range (0,n):
We next loop through columns. Now, notice that we only loop through those columns whose index is smaller than or equal to that of the current row (i.e., from 0 to i). This way we make sure that the values we set are on or below the diagonal of the matrix:
for j in range(0,i+1):
Finally, we set the desired value for the current coordinate:
matrix[i,j]=i-j+1
print(matrix)
import numpy as np
n=4
We start by setting a 4x4 matrix with all coordinates set to null:
matrix=np.zeros((n,n))
We set new coordinate values by looping through rows and columns. First we loop through rows, from index 0 to n-1:
for i in range (0,n):
We next loop through columns. Now, notice that we only loop through those columns whose index is smaller than or equal to that of the current row (i.e., from 0 to i). This way we make sure that the values we set are on or below the diagonal of the matrix:
for j in range(0,i+1):
Finally, we set the desired value for the current coordinate:
matrix[i,j]=i-j+1
print(matrix)
answered Nov 21 at 23:46
DavidPM
18117
18117
Thank you, really helpful!
– Aylín Pérez
Nov 21 at 23:53
add a comment |
Thank you, really helpful!
– Aylín Pérez
Nov 21 at 23:53
Thank you, really helpful!
– Aylín Pérez
Nov 21 at 23:53
Thank you, really helpful!
– Aylín Pérez
Nov 21 at 23:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421873%2fpython-simple-question-numpy-matrix-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In short: It creates a 4 by 4 array where each cell in the lower left triangle has the value
i - j + 1
wherei
is the row index andj
is the column index. You need to be more specific about what you don't understand if that does not solve the issue.– timgeb
Nov 21 at 23:30
Thanks for your prompt reply! My question is how do I know that the lower left triangle is the one where the matrix formula executes. Sorry if it is way too obvious, but I am a complete begginner
– Aylín Pérez
Nov 21 at 23:33
The outer
for
loop iterates over the row indices. The innerfor
loop iterates over the columns, but only up to the column number that is equal to the current row number.– timgeb
Nov 21 at 23:34
1
Thank you very much!!
– Aylín Pérez
Nov 21 at 23:47