How to Calculate Each Row of a Matrix in a For Loop

Asked 2 years ago, Updated 2 years ago, 48 views

There are 201 vectors of length 3575 and they are stored in the numpy.ndarray format. For ease of explanation, use x.
I'd like to calculate for each line of this x, a vector whose shape is (13575), but I don't know how to get each line. If anyone knows, please let me know.
Below is a reduced minimal example.

import numpy as np

x = np.array ([22, 44, 66], [90, 80, 70], [1, 3, 2]])
for_in range(x.shape[0]):
    Vector for each row = vector for each row /np.linalg.norm (vector for each row)
    print (vector of each row)

python python3 numpy

2022-09-30 10:31

2 Answers

If you want to take out each line, you can do the following

import numpy as np

x = np.array ([22, 44, 66], [90, 80, 70], [1, 3, 2]])
For vec in x:
    v=vec/np.linalg.norm(vec)
    print(v)

The comment explains how to do it all at once.


2022-09-30 10:31

If you want to take out each line,

import numpy as np
k
x = np.array ([22, 44, 66], [90, 80, 70], [1, 3, 2]])
for i in x:
    norm=vec/np.linalg.norm(vec)
    print(norm)

can be calculated in .

Also, if you want to specify only certain lines,

#Create a list of empty np.arrays
list_x=np.array([])
for i in x:
    list_x = np.append(list_x, i )
print(list_x)

Then, the list that was in x is broken down for each list and inserted into list_x, so you can only take out the list you want to calculate and calculate it.


2022-09-30 10:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.