given value of matrix, getting it's coordinate
which()
takes an argument, arr.ind=TRUE
, which will return the indices of all TRUE
elements in a logical matrix to which it is applied.
## An example matrix
set.seed(1)
m <- matrix(sample(1:100, 10), ncol=2)
m
# [,1] [,2]
# [1,] 27 86
# [2,] 37 97
# [3,] 57 62
# [4,] 89 58
# [5,] 20 6
## An example application
which(m==58, arr.ind=TRUE)
# row col
# [1,] 4 2
Searching a Matrix and Returning Coordinates of a Value
You are doing weird things in your for-for loop :-)
This should fix it:
public static Pos findIndexWithValue(int[][] maze, int value) {
if (maze == null) {
System.out.println("Maze is NULL! (FindIndexWithValue)");
return null;
} else {
for (int x = 0; x < getMazeWidth(maze); x++) {
for (int y = 0; y < getMazeHeight(maze); y++) {
if (maze[x][y] == value) {
Pos returnPos = new Pos(x, y);
return returnPos;
}
}
}
}
System.out.println("The Value You Are Looking For is not Present (findIndexWithValue)");
return null;
}
Also, just a tip: NEVER name your variables i1 or i2. Whoever reads your code will have a hard time understanding it.
Get the coordinates of a matrix from its flatten index
You can use this simple algorithm:
Let's say you have the matrix A[a][b][c][d] (where a,b,c,d are the dimensions) and the index X.
To get the first coordinate of the index X you simply divide X by b*c*d.
Let it be this next matrix, having the sizes [2][5] and the index X=7
0 1 2 3 4
5 6 7 8 9
You first divide X by the last dimension to find the first coordinate. X/5=1 . Then, from there you move forward and give X the value X%=5 . So you'll have X = 7%5 =2. Now you have to search the coordinates for the remaining dimensions using the same algorithm. If you reach the last dimension , the coordinate will be the remaining X, in this case 2. So the coordinates for X=7 are [1][2] , which is actually the answear.
Again, for the general case, where you have a,b,c,d dimensions.
I'll note with (yd) the y'th dimension.
X=index
(1d)=X/b*c*d
X gets value X % b*c*d
(2d)=X/c*d
X gets value X % c*d
(3d)=X/d
X gets value X % d
(4d)=X
If you had the dimensions [2][2][5] you would get:
X=9;
(1d) = 9/2*5 = 0
X = 9%10 = 9
(2d) = 9/5 = 1
X = 9%5 = 4
(3d) = 4
Result: [0][1][4] is the 9th element.
To get from [0][1][4] to the index 9 , you do the reverse algorithm by multiplying:
X=(1d)*b*c + (2d)*c + 3d = 0 + 1*5 +4 =9
R: assigning values to a new matrix based on value and coordinate in another matrix
You can try the code below
idxc <- col(m1)
idxr <- row(m1)
msk <- idxr %in% 1:3 & idxc %in% 1:3 | idxr %in% 4:5 & idxc %in% 4:5
m1[msk] <- ifelse(m1>2,"C","D")[msk]
m1[!msk] <- ifelse(m1>2,"A","B")[!msk]
m2 <- `diag<-`(m1,NA)
which gives
> m2
[,1] [,2] [,3] [,4] [,5]
[1,] NA "D" "D" "A" "A"
[2,] "D" NA "C" "A" "A"
[3,] "D" "C" NA "A" "B"
[4,] "A" "A" "A" NA "C"
[5,] "A" "A" "B" "C" NA
Data
> dput(m1)
structure(c(0L, 1L, 2L, 3L, 4L, 1L, 0L, 5L, 6L, 7L, 2L, 5L, 0L,
8L, 1L, 3L, 6L, 8L, 0L, 9L, 4L, 7L, 1L, 9L, 0L), .Dim = c(5L,
5L))
How to get the center coordinate of a 1 dimension array in 2d matrix
The center of the matrix is the center of the array. This is because there will be an equal number of rows above and below the center row. And on the center row, there will be an equal number of cells to the left and right of the center cell.
int c = mat.length / 2;
or, if you want:
int c = (width * height) / 2;
This assumes that there is a single center of the matrix. That is, there is an odd number of rows and columns.
If you want the median (mean of all centers), it will become more complicated:
int x1 = (width - 1)/2;
int x2 = width/2;
int y1 = (height - 1)/2;
int y2 = height/2;
double median = (mat[width*y1 + x1] + mat[width*y1 + x2] +
mat[width*y2 + x1] + mat[width*y2 + x2])*0.25;
If you only need one of the center cells, pick one of the four combinations of x1
,x2
,y1
,y2
. Simplest would be:
int c = width * (height / 2) + (width / 2); // lower right center
How do i code for find coordinates in 2d matrix?
After initializing the value of the matrix value you want,
val = 7
here is a nice one-liner:
array = [(ix,iy) for ix, row in enumerate(a) for iy, i in enumerate(row) if i == val]
Output of print(array)
:
[(1, 2)]
Note the one-liner will catch all instances of the number 7 in a matrix, not just one. Also note the indexes start at 0, so row 2 will be displayed as 1 and column 3 will be displayed as 2. If, say, you have more than one instance of 7 in a row and want the actual row and column numbers (not starting at 0), this may be helpful:
a=[[1,7,7,4], [5,6,7,8], [9,10,11,7]]
val = 7
array = [(ix+1,iy+1) for ix, row in enumerate(a) for iy, i in enumerate(row) if i == val]
print(array)
Output:
[(1, 2), (1, 3), (2, 3), (3, 4)]
Finding coordinates of maximum values of a matrix
The problem you've been having with max
so far is because it operates on one dimension. If you call it on a matrix, using its default parameters, it will return a single maximum element (and indices) for each column of the matrix. In your case, you want all maximums, and the global maximum at that.
Try this:
[I,J] = find(M == max(M(:)))
First, max(M(:))
finds the maximum element, then we construct a logical matrix M == max(M(:))
showing which elements are the maximum. Finally, you can use find
to get the co-ordinates of those (if necessary).
Related Topics
How to Assign from a Function with Multiple Outputs
Set a Functions Environment to That of the Calling Environment (Parent.Frame) from Within Function
Cbind: How to Have Missing Values Set to Na
Remove Part of a String in Dataframe Column (R)
Index Unique Values in Data.Table
Add Axis Tick-Marks on Top and to the Right to a Ggplot
Linear Model Function Lm() Error: Na/Nan/Inf in Foreign Function Call (Arg 1)
Element-Wise Concatenation of String Vectors
Dplyr Summarize with Subtotals
Applying a Function to Each Row of a Data.Table
Make a List of Many Objects from a Vector of Object Names
Display a Summary Line Per Facet Rather Than Overall
How to Add Only Missing Dates in Dataframe