[R] Query to find minimum value in a matrix in R
S Ellison
S.Ellison at LGCGroup.com
Fri Sep 16 16:20:17 CEST 2016
> I am unable to understand the line in red code
Colour does not survive plain text transmission; try adding comments (# ...) instead, or state which line of code you do not understand.
In the mean time you could take a look, first, as
?cbind
?apply
?'['
with particular attention to the meaning of negative indices (like '-n' in dd[-n,])
S Ellison
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of susmita T
> Sent: 16 September 2016 13:09
> To: r-help at r-project.org
> Subject: [R] Query to find minimum value in a matrix in R
>
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If
you have received this message in error, please notify the sender
immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com
and delete this message and any copies from your computer and network.
LGC Limited. Registered in England 2991879.
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
More information about the R-help
mailing list