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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:45:59+00:00 2026-05-26T04:45:59+00:00

Possible Duplicate: MATLAB indexing question How to extract non-vertical column from matrix in Matlab

  • 0

Possible Duplicate:
MATLAB indexing question
How to extract non-vertical column from matrix in Matlab

I feel like there should be a simple way to do what I want, but I cannot figure this out.

INPUT: an n x t matrix M of reals and an n x 1 vector I of indices

OUTPUT: an n x 1 vector P such that P(i) = M( i, I(i) )

It’s obvious how to do this with a for loop, but this is Matlab and n is large. Is there a way to vectorize this problem and avoid the for loop?

  • 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-05-26T04:46:00+00:00Added an answer on May 26, 2026 at 4:46 am

    Here’s a simple, fast, vectorized solution using linear indexing.

    indx = (1:n)' + (I-1)*n; %'
    P=M(indx);
    

    Example:

    M = randi(10,[3,4]);     %# test matrix
    
    M =
    
         9    10     3    10
        10     7     6     2
         2     1    10    10
    
    n = size(M,1);
    I = [3,1,4]';            %'# index vector
    indx = (1:n)' + (I-1)*n; %'
    P = M(indx)
    
    P =
    
     3
    10
    10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: Bayesian networks in MATLAB Is there a toolbox in Matlab which implement
Possible Duplicate: Matlab Cross correlation vs Correlation Coefficient question When I cross correlate 2
Possible Duplicate: matlab - variable in plot title I would like to quote the
Possible Duplicate: Import / read / load variables from matlab matfile it C# I
Possible Duplicate: Find location of current m-file in Matlab I would like to write
Possible Duplicate: Octave/MATLAB: How to compare structs for equality? is there a simple comparison
My question is a possible duplicate of this: matlab mex files and C++ dll
Possible Duplicate: MATLAB: How to vector-multiply two arrays of matrices? Is there a way
Possible Duplicate: Local Binary Pattern in MATLAB I would like to implement uniform LBP.

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.