Re: [R] distance matrix?

From: Kjetil Halvorsen <kjetilbrinchmannhalvorsen_at_gmail.com>
Date: Mon, 28 Jun 2010 18:50:15 -0400

 abs(outer(1:10, 1:10, FUN="-"))

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    2    3    4    5    6    7    8     9
 [2,]    1    0    1    2    3    4    5    6    7     8
 [3,]    2    1    0    1    2    3    4    5    6     7
 [4,]    3    2    1    0    1    2    3    4    5     6
 [5,]    4    3    2    1    0    1    2    3    4     5
 [6,]    5    4    3    2    1    0    1    2    3     4
 [7,]    6    5    4    3    2    1    0    1    2     3
 [8,]    7    6    5    4    3    2    1    0    1     2
 [9,]    8    7    6    5    4    3    2    1    0     1
[10,]    9    8    7    6    5    4    3    2    1     0

>

Kjetil

On Mon, Jun 28, 2010 at 5:44 PM, John Ramey <johnramey_at_gmail.com> wrote:
> x <- 0:10
> y <- t(replicate(11, 0:10))
> abs(sweep(y, 1, x))
>
> Hope this helps.
>
> On Mon, Jun 28, 2010 at 5:21 AM, clips10 <m.mcquillan_at_lancaster.ac.uk> wrote:
>>
>> I have a vector 0 to 10 and want to create a matrix with the differences
>> between the numbers in it for instance:
>>
>>        0   1   2   3   4   5   6   7   8   9   10
>>
>> 0       0   1   2   3   4   5   6   7   8   9   10
>> 1      1   0   1   2   3   4   5   6   7    8    9
>> 2
>> 3
>> 4
>> 5
>> 6
>> 7
>> 8
>> 9
>> 10
>>
>> Etc etc. So that the matrix is filled with the differences between in
>> absolute value so there are no negatives.
>>
>> Any ideas?
>>
>> Thanks
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/distance-matrix-tp2270722p2270722.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help_at_r-project.org mailing list
>> 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.
>>
>
>
>
> --
> John A. Ramey, M.S.
> Ph.D. Candidate
> Department of Statistics
> Baylor University
> http://www.ramhiser.com
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> 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.
>



R-help_at_r-project.org mailing list
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. Received on Mon 28 Jun 2010 - 22:53:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Jun 2010 - 23:20:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive