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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:42:40+00:00 2026-05-26T23:42:40+00:00

Suppose I have a matrix such that each row is a standard basis vector,

  • 0

Suppose I have a matrix such that each row is a standard basis vector, i.e. each row contains exactly one 1, the other columns being 0.

Is there a convenient way to create such a matrix (i.e. given a vector of positions of where the ones are in each row)?

Also, is there a way I should represent such a matrix so that multiplications with it can be done more efficiently in octave?

  • 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-26T23:42:41+00:00Added an answer on May 26, 2026 at 11:42 pm

    Suppose you want a 3×3 matrix with the ones in columns 3, 1, and 2 respectively:

    > pos = [3,1,2];
    > x = eye(3)(pos,:);
    

    will give you a matrix with 9 elements, most zero, with the ones in the desired places. You can save memory by using a sparse representation: sparse_x = sparse(x);. But the following test on my machine implies that the natural form multiplies faster:

    > N = 10000;
    > s = rand(N,N);
    > x = eye(N)(randperm(N),:);
    > sx = sparse(x);
    > t = cputime(); ss = s*x; cputime()-t
    ans = 0.41124
    > t = cputime(); ss2 = s*sx; cputime()-t
    ans = 1.0313
    

    This was Octave 3.4 on a Core i7, YMMV.

    Looking at whos it appears that Octave is doing something clever with x:

    > whos
    Variables in the current scope:
    
      Attr Name        Size                     Bytes  Class
      ==== ====        ====                     =====  ===== 
           N           1x1                          8  double
           s       10000x10000              800000000  double
           ss      10000x10000              800000000  double
           ss2     10000x10000              800000000  double
           sx      10000x10000                 160004  double
           x       10000x10000                  40000  double  <---SMALLER THAN s!
    

    If it knows x is special, maybe it’s already taking advantage of speedups in the multiplication.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two matrices, each with two columns and differing numbers of row.
Suppose, in MATLAB, that I have a matrix, A, whose elements are either 0
I have a matrix class that takes the row and column reference type that
Suppose I have a two column matrix. How do I pack the columns into
I have a view that displays a table: rows represent days - one row
Suppose I have an arbitrary transformation matrix A such as, A = 0.9966 0.0007
Suppose I have an NxN matrix A, an index vector V consisting of a
Suppose I have a matrix foo as follows: foo <- cbind(c(1,2,3), c(15,16,17)) > foo
Suppose you have an NxN matrix and you have to check whether it's a
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new

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.