[R] symmetric matrix on both diagonals
David Winsemius
dwinsemius at comcast.net
Sat Apr 21 13:53:04 CEST 2012
On Apr 21, 2012, at 1:13 AM, Petr Savicky wrote:
> On Fri, Apr 20, 2012 at 10:52:47AM -0400, David Winsemius wrote:
>>
>> On Apr 20, 2012, at 7:05 AM, Petr Savicky wrote:
>>
>>> On Fri, Apr 20, 2012 at 03:03:40AM -0700, juliane0212 wrote:
>>>>
>>>> I'm having some problems computing a matrix being symmetric on
>>>> both
>>>> diagonals.
>>>>
>>>> Does anyone know a way to get from this matrix
>>>>
>>>>
>>>> M <- matrix(c(1,0,0,0,2,7,0,0,3,4,0,0,6,0,0,0), ncol=4)
>>>>
>>>> to this one
>>>>
>>>> M_final <- matrix(c(1,2,3,6,2,7,4,3,3,4,7,2,6,3,2,1),
>>>> ncol=4)
>>>
>>> Hi.
>>>
>>> Try the following.
>>>
>>> M[row(M) > col(M)] <- t(M)[row(M) > col(M)]
>>> n <- nrow(M)
>>> M[row(M) + col(M) > n + 1] <- M[n:1, n:1][row(M) + col(M) > n + 1]
>>> all(M == M_final)
>>>
>>> [1] TRUE
>>
>> How about?
>>
>>> M[3:4, ] <- rev(M[1:2,])
>>> M
>> [,1] [,2] [,3] [,4]
>> [1,] 1 2 3 6
>> [2,] 2 7 4 3
>> [3,] 3 4 7 2
>> [4,] 6 3 2 1
>
> Hi.
>
> I am not sure, which matrix did you start from.
You are right. I misunderstood the requested task. (...and then used
the wrong starting matrix.) Sorry for the noise.
--
David.
> If we start
> from the original matrix, then we get
>
> M <- matrix(c(1,0,0,0,2,7,0,0,3,4,0,0,6,0,0,0), ncol=4)
> M[3:4, ] <- rev(M[1:2,])
> M
>
> [,1] [,2] [,3] [,4]
> [1,] 1 2 3 6
> [2,] 0 7 4 0
> [3,] 0 4 7 0
> [4,] 6 3 2 1
>
> where the components 2 and 3 have two and not four copies.
>
> Petr.
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list