3

I am trying to create a matrix like this from a vector:

    vec= c(2, 5, 9)
    > A
            [,1] [,2] [,3] [,4]
    [1,]     2    0    0    0
    [2,]     5    3    0    0
    [3,]     9    7    4    0

Actually always the first column is the vector element, the second column start with 0 and then the (5-2 = 3) and then the thirld element of second column is (9-2 = 7). Then the third column start with 0 and then 0 and (9-5 = 4) and the last column is always zero. May be the length of vec changes to any number for example 4, 5,... .How can I write an efficient function or code to create this matrix?

Bensor Beny
  • 285
  • 2
  • 13
  • I don't understand where the other 3 columns (after the first) in your matrix came from? You say you want to create a matrix from 'a' vector, but then your matrix looks like it was built from 4 of them, all the same length. But then you suggest the lengths of the vectors can be different? I think you need to make it a little bit clearer what you want to do. – Bogdanovist Jun 01 '12 at 01:45
  • Actually always the first column is the vector element, the second column start with 0 and then the (5-2 = 3) and then the thirld element of second column is (9-2 = 7). Then the third column start with 0 and then 0 and (9-5 = 4) and the last column is always zero. May be the length of $vec$ changes to any number for example 4, 5,... – Bensor Beny Jun 01 '12 at 01:48
  • @BensorBeny: That helps, thanks. Please edit your question with that information, and also specify how this is different than the stackoverflow question. – Aaron left Stack Overflow Jun 01 '12 at 01:52
  • @BensorBeny: Thanks for the edit. This sounds a lot like the stackoverflow question, though, for which you checked the mark saying you got an answer that worked for you. Is this different? How? – Aaron left Stack Overflow Jun 01 '12 at 02:06
  • @Aaron: Actually that one is a simple case, and I thought that that works for every vector, but when i changed my vector I have seen this is not my desire. This above matrix which is different form that one is my purpose. – Bensor Beny Jun 01 '12 at 02:10
  • The original verison of this question referred to is [here](http://stackoverflow.com/q/10826357/210673). – Aaron left Stack Overflow Jun 01 '12 at 16:38

2 Answers2

7

I don't know about efficiency, but here are two solutions without using for loops:

n <- length(vec)    
A <- replicate(n+1, vec) - cbind(0, t(replicate(n, vec)))
A[upper.tri(A)] <- 0

This one is longer but creates only one matrix

n <- length(vec)
A <- replicate(n, vec)
A <- A - t(A)
A <- cbind(vec, A)
A[upper.tri(A)] <- 0
ALiX
  • 1,021
  • 5
  • 9
  • Just i am wondering why the second answer without for loop have more system.time for large length vector than the fisrt answer function with for loop? As far as i know, for loop always increase the system.time and gets the code less efficient. Is it right always or not? – Bensor Beny Jun 01 '12 at 09:56
  • No, that's not always true. [It's complicated.](http://stackoverflow.com/a/8474941/210673) [Clarity is also a virtue.](http://stackoverflow.com/q/2275896/210673) – Aaron left Stack Overflow Jun 01 '12 at 16:36
5

I think this will do what you want:

f = function(vec)
{
   n = length(vec)  
   M = matrix(0,n,n+1)
   M[,1] = vec
   for(i in 1:n) M[,i+1] = c(rep(0,i),vec[-c(1:i)]-vec[i])
   return(M)
}

vec = c(2,5,9)
f(vec)
     [,1] [,2] [,3] [,4]
[1,]    2    0    0    0
[2,]    5    3    0    0
[3,]    9    7    4    0
Macro
  • 1,450
  • 1
  • 10
  • 18
  • Thanks a lot. That's exactly my desire matrix. but is there any other more efficeint way to avoid for loop? – Bensor Beny Jun 01 '12 at 03:03
  • No problem, @BensorBeny. If you've found this answer helpful please consider upvoting/accepting it :) –  Jun 01 '12 at 03:06