How to find samevalue rectangular areas of a given size in a matrix most efficiently?
Date : March 29 2020, 07:55 AM
hop of those help? You can do it in O(n^2) relatively easy. First, someprecalculations. For each cell in matrix, calculate how many consecutive cells below it have the same number. 0 0 0 0 0 2 2
1 1 2 2 2 1 1
0 0 1 1 1 0 0
1 1 0 0 0 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0

4 4 2 2 2 0 0
4 4 2 2 2 0 0
0 0 2 2 2 1 1

0 0 0 0 0 1 1
int current_width = 0;
for (int j = 0; j < matrix.width; ++j) {
if (a[i][j] < height  1) {
// this column has different numbers in it, no game
current_width = 0;
continue;
}
if (current_width > 0) {
// this column should consist of the same numbers as the one before
if (matrix[i][j] != matrix[i][j  1]) {
current_width = 1; // start streak anew, from the current column
continue;
}
}
++current_width;
if (current_width >= width) {
// we've found a rectangle!
}
}

find all rectangular areas with certain properties in a matrix
Date : March 29 2020, 07:55 AM
may help you . I finally found a solution that works almost in linear time (there is a small factor depending on the number of found areas). I think this is the fastest possible solution. Inspired by this answer: https://stackoverflow.com/a/7353193/145999 (pictures also taken from there) foundAreas = new list()
For each row y backwards:
potentialAreas = new List()
for each column x:
if M2[x,y]>M2[x1,y]:
add to potentialAreas:
new Area(left=x,height=M2[x,y],hasOne=M1[x,y],hasTop=false)
if M2[x,y]<M2[x1,y]:
for each area in potentialAreas:
if area.hasTop and area.hasOne<area.height:
add to foundAreas:
new Box(Area.left,yarea.height,x,y)
if M2[x,y]==0: delete all potentialAreas
else:
find the area in potentialAreas with height=M2[x,y]
or the one with the closest bigger height: set its height to M2[x,y]
delete all potentialAreas with a bigger height
for each area in potentialAreas:
if area.hasOne>M1[x,y]: area.hasOne=M1[x,y]
if M2[x,y+1]==0: area.hasTop=true

How to find whether a 2D array (rectangular matrix) have a straight line
Date : March 29 2020, 07:55 AM
seems to work fine Well, let's start from two degenerated cases: if you have 0 points, answer as you wish (either there's a line, or there's no such line) if you have 1 or 2 points, the answer is always yes (y1  y2) * (xi  x2) == (yi  y2) * (x1  x2)
(y2*x1  y1*x2) * (xi  x2) == (y2*xi  yi*x2) * (x1  x2)
(2, 4)
(4, 3)
(6, 2)
(8, 1)
private static bool SameLine(IEnumerable<Tuple<int, int>> points) {
if (null == points)
return true;
Tuple<int, int>[] data = points.ToArray();
// i = 2  first two points are always on the same line
for (int i = 2; i < data.Length; ++i) {
int x1 = data[0].Item1;
int y1 = data[0].Item2;
int x2 = data[1].Item1;
int y2 = data[1].Item2;
int xi = data[i].Item1;
int yi = data[i].Item2;
// y = k * x + b where k = infinity
if (x1 == x2) {
if (xi != x1)
return false;
continue;
}
// Same k in y = k * x + b
if (!((y1  y2) * (xi  x2) == (yi  y2) * (x1  x2)))
return false;
// Same b in y = k * x + b
if (!((y2 * x1  y1 * x2) * (xi  x2) == (y2 * xi  yi * x2) * (x1  x2)))
return false;
}
return true;
}
Tuple<int, int>[] test = new Tuple<int, int>[] {
new Tuple<int, int>(2, 4),
new Tuple<int, int>(4, 3),
new Tuple<int, int>(6, 2),
new Tuple<int, int>(8, 1),
};
// Same line
Console.Write(SameLine(test) ? "Same line" : "different lines");

How can I find a basis for the column space of a rectangular matrix?
Date : March 29 2020, 07:55 AM
Does that help One way is to use the LU decomposition. The factor U will be of the same size as your matrix, but will be uppertriangular. In each row of U, pick the first nonzero element: these are pivot elements, which belong to linearly independent columns. A selfcontained example: import numpy as np
from scipy.linalg import lu
A = np.array([[1, 2, 3], [2, 4, 2]]) # example for testing
U = lu(A)[2]
lin_indep_columns = [np.flatnonzero(U[i, :])[0] for i in range(U.shape[0])]

Find inverse of matrix using b^nth power
Tag : c , By : hammer_1968
Date : March 29 2020, 07:55 AM

