Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8039801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:41:37+00:00 2026-06-05T03:41:37+00:00

I am trying to create a matrix like this from a vector: vec= c(2,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:41:39+00:00Added an answer on June 5, 2026 at 3:41 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a matrix to look like this: [1 x x^2
I am trying to create a pointer to the start of a matrix to
I'm trying to create a vector from an array of doubles. I then want
I am trying to use R to create a raster image from a matrix
I am trying to create a matrix that is 3 x n, with each
I am trying to create the below matrix in my vb.net so during processing
Trying to create a table with a bigint column creates a standard integer column
I'm trying to find some way to substract a size 3 vector from each
I'm trying to create a matrix data structure in C. I have a struct
I would like to know which method should I choose when trying to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.