# [R] Query to find minimum value in a matrix in R

susmita T susmitha117 at gmail.com
Fri Sep 16 14:08:30 CEST 2016

```Hi,
Good Morning! I am new to R and finding difficulty in understanding the code. Since few days I am stuck at single line of code which I am unable to understand.
Though there may be number of logics to find min value. As a new beginner I am following a book and as it has the following code

mind<-function(d)
{
n<-nrow(d)
dd<-cbind(d,1:n)
wmins<-apply(dd[-n,],1,imin)
i<-which.min(wmins[2,])
j<-wmins[1,i]
return(c(d[i,j],i,j))
}
imin<-function(x)
{
lx<-length(x)
i<-x[lx]
j<-which.min(x[(i+1):(lx-1)])
k<-i+j
return(c(k,x[k]))
}

So when executed this with mind(below matrix) I get
0	12	13	8	20
12	0	15	28	88
13	15	0	6	9
8	28	6	0	33
20	88	9	33	0
the answer as 6 , row 3 column 4

Due to the symmetry of the matrix , the skipping of the early part of row is done by using expression (x[(i+1):(lx-1)])..(which is in red color in the code shown above). I am unable to understand
the line in red code and how it is implemented in the line 5(i.e wins)…(shown in pink color in the code above
I have done necessary homework to understand but still finding it hard to get it. Please someone help.

[[alternative HTML version deleted]]

```