Mat in C++ to Numpy [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have three C++ matrices called myMatrix, myMatrix2 and canvas respectively using OpenCV. I'm pretty new to C++, so it's unclear to me what Range::all() does. I understand the second Range statement, and I'm wondering if Range::all() is equivalent to Range(0,myMatrix.rows)?
myMatrix.copyTo(canvas(Range::all(), Range(0, myMatrix2.cols)));
I'm thinking it should be something like:
canvas = np.copy(myMatrix[:][:myMatrix2.cols])

If I did understand properly, the numpy equivalent can be written as:
canvas = np.copy(myMatrix[:, :myMatrix2.shape[1]])
Assuming that both myMatrix and myMatrix2 exist. If canvas also exists in python beforehand, you can update it inplace (rather than creating a new copy) as:
canvas[:] = myMatrix[:, :myMatrix2.shape[1]]
The : is the equivalent to Range::all() while :myMatrix2.shape[1] is equivalent to Range(0, myMatrix2.cols).
You should first learn how to use numpy (it has a great manual) rather than trying to make something work blindly.

Related

matrix of variable size in global variables , python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I was doing a project in python and I would like to create a global variable which must be a two-dimensional array, but the dimension must be assigned when the user enters it.
how can i declare it initially? and then resize it when I want?
thanks for your attention
You can declare two-dimensional array doing simply this:
a = [[]]
If you print it out, you're gonna get something like this: [[]] which is two-dimensional array. Then you can append to this array and get elements using a[y][x]

How to iterate and change all elements of a numpy array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on a machine learning task and trying to convert all strings in a set of data to floats using hash() to do this I need to iterate over all the elements of a numpy array whilst not knowing if it is a 2D 3D or 4D array and then change each element. Is there any way to do this without using nested loops?
You can try numpu.vectorize, already mentioned here
Note:
The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.here
arr = np.array([['aba', 'baa', 'bbb'],
['xxy', 'xyy', 'yyy']])
v_hash = np.vectorize(hash)
v_hash(arr)
array([[-1538054455328520296, -1528482088733019667, -7962229468338304433],
[ 5621962119614158870, 1918700875003591346, -3216770211373729154]])

How to calculate powers of complex numbers in Python without using the complex numbers data type [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm wondering how I can calculate powers of a complex number without using the complex numbers data type. So I have a function
def Power_complex(re, im, n):
How can I calculate (re + im * i)^n with this? Thank you!
You can use the Biniomial theorem for arbitrary exponents, although positive integers is the easiest case.
Or you can treat the problem in polar coordinates (this link simply gives you the answer, only click if you really don't want to figure it out on your own)

Pseudocode Python Concepts [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I know that pseudocode by its very nature is based more on natural language syntax and principles than anything else, but it is supposed to represent coding concepts. I am still a beginner , so I don't understand all of the concepts.
int string (char s[1..m], char t[1..n])
// d is a table with m+1 rows and n+1 columns
declare int d[0..m, 0..n]
I understand this except for the last line. Could somebody explain to this to me in python ?
This is declaring a 2D array of size mxn. With statically typed languages (and even with pseudocode), it's necessary/useful to state what the variable is before using it. If you've used C for example, we define variables before using them. With dynamic languages like Python, this is not necessary and you won't see it.
I guess the closest thing in Python would be like creating and empty list of lists holding integers and assigning it to d.

Convert < > from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to get < from a string that contains the character.
So what I want to do is:
magic_function('<') = <
in order to make. For example:
1 magic_function('<') 3
I expect, of course, that the program returns True
This isn’t possible with specifically what you want. A function returns an object, not an operator. I’m not sure what your main intention is here but you might want to rethink your architecture.
What you can do (but what I don’t recommend) is using eval() like so:
eval(“1 < 3”)
But there are many resources online about why eval is “evil”: to sum them up, if you don’t know what your data source is, then you could be performing unexpected operations which you might not want.

Categories