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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:28:18+00:00 2026-06-03T20:28:18+00:00

Sorry for vague question title, i couldn’t figure out something more specific. I have

  • 0

Sorry for vague question title, i couldn’t figure out something more specific.

I have 3×2 matrix c:

> c
     [,1] [,2]
[1,]    1    2
[2,]    1    3
[3,]    2    3

It is important that ncol(c) == 2.

I also have matrix ind:

> ind
      [,1] [2] [,3] [,4]
[1,]    2    2    2    1
[2,]    1    1    2    2
[3,]    2    2    2    1

It is important that nrow(c) == nrow(ind), and that the values of matrix ind are 1 and 2 (like column indices for each row of c)

What i want to get is matrix a with same dim as ind such that a[i,j] == c[i,ind[i,j]]:

> a
      [,1] [2] [,3] [,4]
[1,]    2    2    2    1
[2,]    1    1    3    3
[3,]    3    3    3    2

I can do something similar in less comprehensive situations, for example if nrow(c) == 1 i’ll use apply:

 > apply(c,2,function(x){return(matrix(x[ind], nrow(ind)))})

I know there is a way to iterate by 2 lists using mapply, but

1) i don’t know what’s the best way to represent matrix as list of rows
2) i fing this solution ugly

What is the best way to achieve what i descibed here?

  • 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-03T20:28:19+00:00Added an answer on June 3, 2026 at 8:28 pm

    Matrix indexing to the rescue!

    > c.mat <- matrix(c(1,1,2,2,3,3), ncol=2)
    > ind <- matrix(c(2,1,2,2,1,2,2,2,2,1,2,1), ncol=4)
    > matrix(c.mat[cbind(as.vector(row(ind)), as.vector(ind))], ncol=ncol(ind))
         [,1] [,2] [,3] [,4]
    [1,]    2    2    2    1
    [2,]    1    1    3    3
    [3,]    3    3    3    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, sorry for the vague question title. I couldn't come up with a more
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry about the vague question title, but I have these typedefs here: typedef std::list<AnimationKeyframe>
Sorry for the vague thread title; hard to succinctly describe my question. I have
Sorry for a vague title but it's hard to be more specific. Platform used:
Sorry for vague question title. I've got a table containing huge list of, say,
Sorry for being to vague last time I asked this question. I have this
Sorry about the vague title, but i didnt know how to ask the question
Sorry about the vague question title, I just want to ascertain some things. Static
Sorry for vague title, I am not able to describe my question in one

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.