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 9263537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:36:36+00:00 2026-06-18T13:36:36+00:00

suppose I have a matrix, I want to select values from column1 for the

  • 0

suppose I have a matrix, I want to select values from column1 for the first row, column5 for the second row, and column4 for the third row (…). The columns are stored as column names in a vector and the position in this vector equals the row in which the column is to be chosen.

How can I achieve this efficiently, i.e. without looping?

(The background is: My purpose is to use this in a simulation, that’s why I would love to vectorize it to speed it up)

A minimal example:

    # Creating my dummy matrix
    aMatrix <-matrix(1:15,3,5,dimnames=list(NULL,LETTERS[1:5]))
    aMatrix
         A B C  D  E
    [1,] 1 4 7 10 13
    [2,] 2 5 8 11 14
    [3,] 3 6 9 12 15

    # Here are the columns I want for each row
    columns <-c("A","E","D")
    columns
    [1] "A" "E" "D"
    # means: select for row 1 column "A" = 1,
    # select for row 2 column "E" = 11,
    # select for row 3 column "D" = 12

    # Now obviously I could do looping, but this is inefficient
    for (i in columns) print(grep(i,colnames(aMatrix))) #grep is necessary for my specific matrix-names in my simulation only.
    [1] 1 #wanting col. 1 for row 1
    [1] 5 #wanting col. 5 for row 2
    [1] 4 #wanting col. 4 for row 3

I just saw that looping the way I did it does not work very efficiently.

I was thinking about sapply/tapply but somehow could not get that to work, since there are two arguments that change (the row to be searched in the matrix, and the letter to be chosen from the target columnname-vector).

I would apprechiate your help a lot.
Thanks!

Jana

P.S. I use “grep” here as the column names are substrings of the actual column names in the simulation I will run. But that substring-creation would have made the example more complicated, thus I skipped it.

  • 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-18T13:36:37+00:00Added an answer on June 18, 2026 at 1:36 pm

    As the help page ?`[` says, you can subset with a matrix to get individual elements. Each row of the subsetting matrix is an element, and the columns specify the indices for each dimension.

    match(columns,colnames(aMatrix)) #gets the column indices
    # [1] 1 5 4
    b <- cbind(seq_along(columns),match(columns,colnames(aMatrix))) #subset matrix
    #      [,1] [,2]
    # [1,]    1    1 #first element: first row first column
    # [2,]    2    5 #second element: second row fifth column
    # [3,]    3    4 #third element: third row fourth column
    aMatrix[b]
    # [1]  1 14 12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a matrix of weights, and another matrix of data values. Can
Suppose I have a matrix with n columns and m rows. In my case
Suppose that I have an n row, m column matrix A , and I
Suppose I have a NxN matrix M (lil_matrix or csr_matrix) from scipy.sparse, and I
I want to build a square matrix. Let's suppose We have this matrix called
Suppose I have a matrix object in R with the values: aa <- matrix(c(0,4,1,10,3,2,1,6,0),
Suppose I have two matrices, each with two columns and differing numbers of row.
Suppose I have a matrix such that each row is a standard basis vector,
Suppose you have a matrix of N*M of floats, I want to have a
Suppose I have the following matrix in python: [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]] I want

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.