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

PIKAL Petr petr.pikal at precheza.cz
Fri Sep 16 17:15:29 CEST 2016

```Hi

you can follow logic of functions by using debug and see how they operate by inspecting objects evaluated within functions.

See
?debug

However it seems to me that your functions are quite complicated. If I understand correctly, they compute minimum value of upper part of matrix. If I am correct, this function does the same, is shorter, more understandable and extensible.

min.upper <- function(x) {
mm <- min(x[upper.tri(x)])
x[lower.tri(x)] <- NA
ind <- which(x==mm, arr.ind=TRUE)
c(mm, ind)
}

mat <- structure(c(0, 5, 9, 13, 5, 0, 10, 14, 9, 10, 0, 15, 13, 14,
15, 0), .Dim = c(4L, 4L), .Dimnames = list(NULL, c("col1", "col2",
"col3", "col4")))
min.upper(mat)
[1] 5 1 2

Cheers
Petr

> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of susmita T
> Sent: Friday, September 16, 2016 2:09 PM
> 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
>
>
>       [[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
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
```