# [R] filling the matrix row by row in the order from lower to larger elements

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Sun Apr 8 22:58:43 CEST 2012

```Agreed, Rui provided a very elegant vectorized solution - and I am
very thankful to him. Unfortunately (for myself), I am not as
proficient in vectorization - otherwise, I would not have asked the
question.
Why I did not follow on the original hint to use apply? For this reason (quote):
> However - isn't apply the same as a loop, just hidden?
with slight caveats, yes.
Bert

Dimitri

On Sun, Apr 8, 2012 at 3:16 PM, ilai <keren at math.montana.edu> wrote:
> On Sun, Apr 8, 2012 at 8:26 AM, Dimitri Liakhovitski
> <dimitri.liakhovitski at gmail.com> wrote:
>> Sorry, I didn't have time to check the speed, indeed.
>> However - isn't apply the same as a loop, just hidden?
>> D.
>>
>
> Yes ?apply is a loop but not the same as ?for, see "Intro to R". As
> Bert Gunter pointed out the issue here was not speed but that your
> problem could have been vectorized to avoid loops all together (which
> would have the added benefit of speed). This was given to you in my
> first response which assumed a small number of columns in the matrix,
> but Rui gave an elegant expansion to use on any ncol(matrix). Using
> apply was suggested at some point by others, my comment was simply
> that you failed to meet even that adjustment.
> Cheers
>
>
>
>> On Fri, Apr 6, 2012 at 6:59 PM, ilai <keren at math.montana.edu> wrote:
>>> On Fri, Apr 6, 2012 at 4:02 PM, Dimitri Liakhovitski
>>> <dimitri.liakhovitski at gmail.com> wrote:
>>>  This works great:
>>>
>>> Really ? surprising given it is the EXACT same for-loop as in your
>>> original problem with counter "i" replaced by "k" and reorder to
>>> matrix[!100]<- 0 instead of matrix(0)[i]<- 100
>>> You didn't even attempt to implement Carl's suggestion to use apply
>>> family for looping (which I still think is completely unnecessary).
>>>
>>> The only logical conclusion is N=nrow(input) was not large enough to
>>> pose a problem in the first place. In the future please use some brain
>>> power before waisting ours.
>>>
>>> Cheers
>>>
>>>>
>>>> input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
>>>> result<-input
>>>> N<-nrow(input)
>>>> for (k in 1:N){
>>>>  foo <- which (input == k,arr.ind=T)
>>>>  result[k,foo[2]] <-100
>>>> }
>>>> result[result !=100]<-0
>>>>
>>>> Dimitri
>>>>
>>>>
>>>> On Fri, Apr 6, 2012 at 5:14 PM, Carl Witthoft <carl at witthoft.com> wrote:
>>>>> I think the OP wants to fill values in an arbitrarily large matrix. Now,
>>>>> first of all, I'd like to know what his real problem is, since this seems
>>>>> like a very tedious and unproductive matrix to produce.  But in the
>>>>> meantime,  since he also left out important information, let's assume the
>>>>> input matrix is N rows by M columns, and that he wants therefore to end up
>>>>> with N instances of "100", not counting the original value of 100 that is
>>>>>
>>>>> Then either loop or lapply over an equation like (I've expanded things more
>>>>> than necessary for clarity
>>>>> result<-inmatrix
>>>>> for (k in 1:N){
>>>>> foo <- which (inmatrix == k,arr.ind=T)
>>>>> result[k,foo[2]] <-100
>>>>>
>>>>> }
>>>>>
>>>>>
>>>>>
>>>>> I maybe missing something but this seems like an indexing problem
>>>>> which doesn't require a loop at all. Something like this maybe?
>>>>>
>>>>> (input<-matrix(c(5,1,3,7,2,6,4,8),nc=2))
>>>>> output <- matrix(0,max(input),2)
>>>>> output[input[,1],1] <- 100
>>>>> output[input[,2],2] <- 100
>>>>> output
>>>>>
>>>>> Cheers
>>>>>
>>>>>
>>>>> On Fri, Apr 6, 2012 at 1:49 PM, Dimitri Liakhovitski
>>>>> <dimitri.liakhovitski at gmail.com> wrote:
>>>>>> Hello, everybody!
>>>>>>
>>>>>> I have a matrix "input" (see example below) - with all unique entries
>>>>>> that are actually unique ranks (i.e., start with 1, no ties).
>>>>>> I want to assign a value of 100 to the first row of the column that
>>>>>> contains the minimum (i.e., value of 1).
>>>>>> Then, I want to assign a value of 100 to the second row of the column
>>>>>> that contains the value of 2, etc.
>>>>>> The results I am looking for are in "desired.results".
>>>>>> My code (below) does what I need. But it's using a loop through all
>>>>>> the rows of my matrix and searches for a matrix element every time.
>>>>>> My actual matrix is very large. Is there a way to do it more efficiently?
>>>>>> Thank you very much for the tips!
>>>>>> Dimitri
>>>>>>
>>>>>> input<-as.matrix(data.frame(a=c(5,1,3,7),b=c(2,6,4,8)))
>>>>>> (input)
>>>>>> desired.result<-as.matrix(data.frame(a=c(100,0,100,0),b=c(0,100,0,100)))
>>>>>> (desired.result)
>>>>>> result<-as.matrix(data.frame(a=c(0,0,0,0),b=c(0,0,0,0)))
>>>>>> for(i in 1:nrow(input)){ # i<-1
>>>>>>  mymin<-i
>>>>>>  mycoords<-which(input==mymin,arr.ind=TRUE)
>>>>>>  result[i,mycoords[2]]<-100
>>>>>>  input[mycoords]<-max(input)
>>>>>> }
>>>>>> (result)
>>>>>>
>>>>> --
>>>>>
>>>>> Sent from my Cray XK6
>>>>> "Quidvis recte factum, quamvis humile, praeclarum."
>>>>>
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>>>>
>>>> --
>>>> Dimitri Liakhovitski
>>>> marketfusionanalytics.com
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>> --
>> Dimitri Liakhovitski
>> marketfusionanalytics.com

--
Dimitri Liakhovitski
marketfusionanalytics.com

```