matrix

Multiply tensors containing matrices, following matrix multiplication rule

Multiply tensors containing matrices, following matrix multiplication rule Question: Say I have a tensor, where A, B, C, and D are all 2×2 matrices: M = [[A, B], [C, D]] How do I get to the power of n, for example with n=2, with Python or MATLAB M^2 = [[A@A + B@C, A@B + B@D], …

Total answers: 3

Python 3 matrix calculator

Python 3 matrix calculator Question: I have an issue with this code, It works wrong on option 7 at least is the main problem, It should do the matrix modify and save new value like 2A(A s 2) should be saved on A and maybe because it doesn’t update to new value it gives wrong …

Total answers: 2

Optimize this linear transformation for images with Numpy

Optimize this linear transformation for images with Numpy Question: Good evening, I’m trying to learn NumPy and have written a simple Linear transformation that applies to an image using for loops: import numpy as np M = np.array([ [width, 0], [0, height] ]) T = np.array([ [1, 3], [0, 1] ]) def transform_image(M, T): T_rel_M …

Total answers: 2

How to reshape matrices using index instead of shape inputs?

How to reshape matrices using index instead of shape inputs? Question: Given an array of shape (8, 3, 4, 4), reshape them into an arbitrary new shape (8, 4, 4, 3) by inputting the new indices compared to the old positions (0, 2, 3, 1). Asked By: mariogarcc || Source Answers: Numpy’s transpose "reverses or …

Total answers: 2

Values in numpy matrix based on an array using index and value of each element

Values in numpy matrix based on an array using index and value of each element Question: I would like to use a numpy array to index a numpy matrix using the values of the array indicating the columns, and indices indicating the corresponding row numbers.As an example, I have a numpy matrix, a = np.tile(np.arange(1920), …

Total answers: 1

OpenCV Mat cpp operation only on condition

OpenCV Mat cpp operation only on condition Question: Having a hard time figuring out how to do this python operation in c++ without looping. The goal is to perform an operation only on a part of the cv::Mat that meets a condition. In this case, scaling values of the image that were originally between -5 …

Total answers: 1

Sorting matrix columns

Sorting matrix columns Question: I have a matrix 4*5 and I need to sort it by several columns. Given these inputs: sort_columns = [3, 1, 2, 4, 5, 2] matrix = [[3, 1, 8, 1, 9], [3, 7, 8, 2, 9], [2, 7, 7, 1, 2], [2, 1, 7, 1, 9]] the matrix should first …

Total answers: 1

Indexing ndarray with unknown number of dimensions with range dynamically

Indexing ndarray with unknown number of dimensions with range dynamically Question: I have data array with unknown shape and array bounds of bounds for slicing data. This code is for 3D data, but is there any way of generalizing this to N-dim? for b in bounds: l0, u0 = b[0] l1, u1 = b[1] l2, …

Total answers: 1

How to use numpy instead of for loop with different vectors

How to use numpy instead of for loop with different vectors Question: I want to improve my code to make it faster and for now, I have a for loop that I don’t know how to replace it with numpy functions. import numpy as np N = 1000000 d = 2000 p = np.linspace(0,210,211) alpha …

Total answers: 2