This question already has answers here:
Creation of numpy array from two arrays, such that alternate indices contain elements from different arrays [duplicate]
(2 answers)
Pythonic way to combine (interleave, interlace, intertwine) two lists in an alternating fashion?
(26 answers)
Closed 10 days ago.
With Matlab, I can run this easily :
a = [0 0 0 0 0]
b = [1 1 1 1]
a(1:1:end) = b
To obtain : a = [0 1 0 1 0 1 0 1 0]
How can I implement this using Python ?
Related
This question already has answers here:
How to convert a column or row matrix to a diagonal matrix in Python?
(4 answers)
Closed 1 year ago.
How can I build such a matrix using a loop?
You can get this, even without a loop via numpy.
import numpy as np
np.eye(10, k=0, dtype=int) *-1
You want to create a symmetrical 2D array using for loops. The classical approach would be:
import numpy as np
ax0 = 5
ax1 = ax0
l = []
for i in range(ax0):
l.append([])
for j in range(ax1):
if i != j:
l[i].append(0)
else:
l[i].append(-1)
print (np.array(l))
Output:
[[-1 0 0 0 0]
[ 0 -1 0 0 0]
[ 0 0 -1 0 0]
[ 0 0 0 -1 0]
[ 0 0 0 0 -1]]
You can make a much more compact code by using list comprehension:
print (np.array([ [ 0 if i != j else -1 for j in range(ax1) ] for i in range(ax0) ]))
This output the same and is also using for loops and conditional assignment for the diagonal.
This question already has answers here:
Index a 2D Numpy array with 2 lists of indices
(5 answers)
Closed 4 years ago.
for example,I have a matrix like this
mat = np.diag((1,1,1,1,1,1))
print(mat)
out:[[1 0 0 0 0 0]
[0 1 0 0 0 0]
[0 0 1 0 0 0]
[0 0 0 1 0 0]
[0 0 0 0 1 0]
[0 0 0 0 0 1]]
I may need some slices that can be combination of any lines and columns.
if it is lines=[0,1,2] columns=[0,1,2],I could use:
mat[0:3,0:3]
If I need lines=[0,1,2,5] columns=[0,1,2,5],I write:
mat[[0,1,2,5],[0,1,2,5]]
I can only get:
out:[1 1 1 1]
But I wanna get a matrix of 4×4.By the way,the columns always equal lines.
For non-contiguous indices you can do:
mat[[0,1,2,5],:][:,[0,1,2,5]]
i.e. first get the specified rows (gets a 4x6 matrix out of it) then get the specified columns from that.
This question already has answers here:
String to list in Python
(5 answers)
Closed 5 years ago.
Given:
['1 -1 1 1 1 1 1 1 1']
How can I convert it (efficiently) to be a vector of integers something like:
[1 -1 1 1 1 1 1 1 1]
Thank you.
return [int(n)
for n in s.split()]
This question already has answers here:
Count all values in a matrix less than a value
(6 answers)
Closed 5 years ago.
Data = matrix R
First I wanted to count elements of each row
countR = np.count_nonzero(R, axis=1)
Then, I could get matrix countR.
[25 2 1 2 2 55 1 2 1 2 1 1 2 2 1 1 1 1 2 2 1 2 14 1 3 ..
Second, I want to count elements in matrix
"if element>1 "
So what I did is here
countR1 = pd.value_counts(countR.values, sort>1)
But there was an error.
How can i count elements?
you can do it easily like this:
y=np.array(countR)
len(y[y>1])
if I understand correctly you want to count all the elements that are larger than 1 in the matrix R.
you can filter the data-frame by doing so(to dispose of the elements that are larger than 1):
biggerThanOne = R[R<1]
you can then get the size of the array and get the number of elements:
biggerThanOne.size
if you mean you want to count the elements of countR you can practically do the same thing
This question already has answers here:
2D array of lists in python
(6 answers)
Closed 6 years ago.
I have a list 2D
a = {}
a[0,0] = 1
a[0,1] = 2
a[1,0] = 3
a[1,1] = 4
a[1,2] = 5
...
a[1,n] = 6
Now, I want to access the element a[1,x] (x = is 0 to n)
How can I do?
You'll need a nested loop if I understand what your asking for.
somearray = [[0,1],[2,3],[4,5]]
for row in range(len(somearray)):
for col in range(len(somearray[row])):
print(somearray[row][col], end =" ")
will output:
0 1 2 3 4 5