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

  • SEARCH
  • Home
  • 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 5946601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:50:45+00:00 2026-05-22T16:50:45+00:00

For a vector V of size n x 1 , I would like to

  • 0

For a vector V of size n x 1, I would like to create binary indicator matrix M of the size n x Max(V) such that the row entries of M have 1 in the corresponding columns index, 0 otherwise.

For eg: If V is

V = [ 3
      2
      1
      4]

The indicator matrix should be

M= [ 0 0 1 0
     0 1 0 0
     1 0 0 0
     0 0 0 1]
  • 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-22T16:50:45+00:00Added an answer on May 22, 2026 at 4:50 pm

    The thing about an indicator matrix like this, is it is better if you make it sparse. You will almost always be doing a matrix multiply with it anyway, so make that multiply an efficient one.

    n = 4;
    V = [3;2;1;4];
    M = sparse(V,1:n,1,n,n);
    M =
       (3,1)        1
       (2,2)        1
       (1,3)        1
       (4,4)        1
    

    If you insist on M being a full matrix, then making it so is simple after the fact, by use of full.

    full(M)
    ans =
         0     0     1     0
         0     1     0     0
         1     0     0     0
         0     0     0     1
    

    Learn how to use sparse matrices. You will gain greatly from doing so. Admittedly, for a 4×4 matrix, sparse will not gain by much. But the example cases are never your true problem. Suppose that n was really 2000?

    n = 2000;
    V = randperm(n);
    M = sparse(V,1:n,1,n,n);
    FM = full(M);
    
    whos FM M
      Name         Size                 Bytes  Class     Attributes
    
      FM        2000x2000            32000000  double              
      M         2000x2000               48008  double    sparse    
    

    Sparse matrices do not gain only in terms of memory used. Compare the time required for a single matrix multiply.

    A = magic(2000);
    
    tic,B = A*M;toc
    Elapsed time is 0.012803 seconds.
    
    tic,B = A*FM;toc
    Elapsed time is 0.560671 seconds.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a binary matrix M of size n x k , i would like
I would like to create a thread passing a vector as parameter. but i
I have some vector graphic files in XAML format and I would like to
I have the following piece of code that I would like help with... public
I wrote a sparse vector class (see #1 , #2 .) I would like
I have a bunch of related matrices of different sizes, and would like to
I would like to create a list or map of references in C++ which
I require a linear vector container to behave like a multi-dimensional vector container. That
Possible Duplicates: How to subtract a vector from each row of a matrix? How
I have a class that has a vector of another class objects as a

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.