similar to: Matrix Indexing

Displaying 20 results from an estimated 20000 matches similar to: "Matrix Indexing"

2008 Nov 21
2
Extracting diagonal matrix
Dear All, I have a correlation matrix of size 100 x 100 and would like to extract the diagonal matrix from it. I have used the for loop to store tha correlation values of the diagonal matrix. Is there a 'R way' of doing this? Thanks in advance. Kind regards, Ezhil
2008 Dec 05
1
Cartesian Product Of Character Vectors
Hi all (I'm sure this question has been asked before, but I cant find it). If I have two character vectors: > x <- c("aaa","bbb","ccc") > y <- c("1","2","3") How can I get the cartesian product of the string values? > expand.grid(x,y) Gives me a data frame with separate columns...however, I cant seem to get *apply
2006 Oct 03
4
how ot replace the diagonal of a matrix
Dear useRs, Trying to replace the diagonal of a matrix is not working for me. I want a matrix with .6 on the diag and .4 elsewhere. The following code looks like it should work--when I lookk at mps and idx they look how I want them too--but it only replaces the first element, not each element on the diagonal. mps <- matrix(rep(.4, 3*3), nrow=n, byrow=TRUE) idx <- diag(3) mps idx mps[idx]
2008 Jun 08
2
Adding a Rotated Density Plot to an Existing Plot
Hi Consider the following graph: x <- rnorm(1000) x <- x + exp(-x/2) layout(matrix(rep(c(1,1,2), 2), 2, 3, byrow=TRUE)) boxplot(x) rug(jitter(x), side=2) plot(density(x)) What I would really like to do is to have the density plot rotated by 90 degrees so that I can see it line up with the rug plot on the side axis. I cant seem to do this. I have seen references to the grid
2011 Jan 20
2
splitting a square symmetric matrix
So many matrices are square symmetrical (i.e. variance-covariance matrices), is there any way to get R to split the matrix on its diagonal and just return one diagonal? So if I have mat<-matrix(c(1,4,3,4,1,2,3,2,1), nrow = 3, ncol=3, byrow=TRUE) is there anyway to get the lower right diagonal instead of the entire symmetric matrix? -------------------------------------------
2010 Mar 01
1
function odiag(): assigning values to off-diagonal
hi I'm trying to use the function odiag(x) for matrix calculations. I would like to assign new values to an off-diagonal in a matrix. When I use the diag (x) function I could write something like p<-matrix(seq(1:12),ncol=4) p.new<-matrix(rep(0,12),ncol=4) diag(p.new)<-diag(p) p.new But this won't work with odiag. How can I turn odiag (x) into something like diag (x) in order
2007 Sep 03
2
Derivative of a Function Expression
Hi I am currently (for pedagogical purposes) writing a simple numerical analysis library in R. I have come unstuck when writing a simple Newton-Raphson implementation, that looks like this: f <- function(x) { 2*cos(x)^2 + 3*sin(x) + 0.5 } root <- newton(f, tol=0.0001, N=20, a=1) My issue is calculating the symbolic derivative of f() inside the newton() function. I cant seem to get R to
2004 Jan 12
1
Matrix indexes
Two questions about matrix indexing: Is is correct that V <- V[lower.tri(V, diag=TRUE)] returns the lower triangular of matrix V, that is: all elements above diagonal are set to zero? I understand that the triangle of matrix elements of V for which lower.tri is TRUE are returned while the others (above diagonal) are set to zero (or NA ???). If D and B are vectors of logicals, what
2011 Dec 15
2
Data Manipulation - make diagonal matrix of each element of a matrix
Dear R list, I have the following data: set.seed(1) n <- 5 # number of subjects tt <- 3 # number of repeated observation per subject numco <- 2 # number of covariates x <- matrix(round(rnorm(n*numco),2), ncol=numco) # the actual covariates x > x [,1] [,2] [1,] -0.63 -0.82 [2,] 0.18 0.49 [3,] -0.84 0.74 [4,] 1.60 0.58 [5,] 0.33 -0.31 I need to form a matrix
2010 Apr 30
2
Flattening and unflattening symmetric matrices
Here's an easy question: I'd like to convert a symmetric matrix to a vector composed of the upper.tri() part of it plus the diagonal, and convert it back again. What's the best way to achieve this? I'm wondering if there are some built in functions to do this easily. I can encode fine: v <- c(diag(A),A[upper.tri(A)]) but I don't see an easy way to recover A from v
2015 Oct 29
2
Extraer elementos diagonales de submatrices
El código que me olvide pegar input m <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 5, 5, 5), .Dim = c(10L, 5L)) m ## output output <- c(1:3, 1:5, 1:2) output nfilas <- nrow(m) while(nfilas > 0) { diagonal <- diag(m)
2008 Jun 05
1
Limit distribution of continuous-time Markov process
I have (below) an attempt at an R script to find the limit distribution of a continuous-time Markov process, using the formulae outlined at http://www.uwm.edu/~ziyu/ctc.pdf, page 5. First, is there a better exposition of a practical algorithm for doing this? I have not found an R package that does this specifically, nor anything on the web. Second, the script below will give the right
2005 Jul 12
3
elegant matrix creation
Hi I want to write a little function that takes a vector of arbitrary length "n" and returns a matrix of size n+1 by n+1. I can't easily describe it, but the following function that works for n=3 should convey what I'm trying to do: f <- function(x){ matrix(c( 1 , 0 , 0 , 0, x[1] , 1 , 0 , 0, x[1]*x[2] , x[2] , 1 , 0,
2008 Apr 05
2
Adding a Matrix Exponentiation Operator
Hi all I recently started to write a matrix exponentiation operator for R (by adding a new operator definition to names.c, and adding the following code to arrays.c). It is not finished yet, but I would like to solicit some comments, as there are a few areas of R's internals that I am still feeling my way around. Firstly: 1) Would there be interest in adding a new operator %^% that performs
2011 Apr 12
5
B %*% t(B) = R , then solve for B
Hello,.. Apologies for the newbie question but... I have a matrix R, and I know that *B %*% t(b) = R* *I'm trying to solve for B *(aka. 'factoring the correlation matrix' I think) Please help! I've read that 'to solve for B we define the eigenvalues of R and then apply the techniques of Principal Component Analysis' This made me reach for princomp() but now I'm
2007 Oct 10
3
as.dist with diagonal unequal zero
Hello and sorry that I still haven?t found a solution for my problem. I need to extract the lower and upper triangle from a square matrix including the diagonal. This diagonal is not zero in that special case. I tried with as.dist w<-as.dist(w, diag = TRUE) > w 1 2 3 4 5 1 0 2 2 0 3 3 8 0 4 4 9 14 0 5 5 10 15 20 0 but found no way to keep the diagonal that is in the
2008 Nov 24
4
Calculating sum of letter values
Hi all If I have a string, say "ABCDA", and I want to convert this to the sum of the letter values, e.g. A -> 1 B -> 2 etc, so "ABCDA" = 1+2+3+4+1 = 11 Is there an elegant way to do this? Trying something like which(LETTERS %in% unlist(strsplit("ABCDA", ""))) is not quite correct, as it does not count repeated characters. I guess what I need is
2008 Nov 25
3
Line color based on data values?
Hi all Does anyone know if it is possible when plotting a line or scatter plot, to selectively color the data points based on the data value? i.e. if plotting say the percentage change in stock price movements, to color +ve points in green and -ve points in red? And extending this to a user-defined range of colors based on the quartile of the data points? Thanks Rory Rory Winston RBS Global
2009 Aug 26
1
Problem with standard generic methods in Matrix package
I have posted this message on r-lang, but it is perhaps more appropriate on r-devel: --- Hello, I'm puzzled by a problem with call to diag(), rowSums(), rownames() on objects of class "dgtMatrix", created by sparseMatrix() or spMatrix(). I use Matrix 0.999375-30. The weird thing is that I don't encounter any problem when I use this functions on the R prompt, or
2012 Apr 12
3
writing spdiags function for R
Dear R-list, I am in the process of translating a long function written in Matlab into R (mainly because I am a big of fan of R, and folks will not have to pay to use it :). In the translation of this function I got stack because they use spdiags, which, as far as I can tell it is not available in R. I have explored the Matrix package, from which I borrowed some of the functions (e.g.,