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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:13:13+00:00 2026-06-05T17:13:13+00:00

I have a 2D matrix, say M=zeros(10,10); I have another column matrix, V=[1;2;3;4;5;6;5;4;3;2]; I

  • 0

I have a 2D matrix, say M=zeros(10,10);

I have another column matrix, V=[1;2;3;4;5;6;5;4;3;2];

I would like to be able to set M(i,j) = 1 for all j >= V(i)

I know I can do this in a loop

for i=1:10
   M(i,V(i):10) = 1;
end

but it seems that it would be possible to use some form of Matlab indexing to avoid using a loop. For example something like :

M(:,V:10)=1;

or

M(:,V(:):10)=1;

but neither of these produces the expected result.

Is there some syntactic sugar I can use to achieve this or should I revert to looping?

  • 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-05T17:13:14+00:00Added an answer on June 5, 2026 at 5:13 pm

    Since you’re looking for syntactic sugar, here’s a sort of esoteric way of doing it. 🙂

    Assuming the length of V is the size of both dimensions in the desired matrix M, first create an identity matrix of the same size, then index appropriately and take cumsum:

    V = [1;2;3;4;5;6;5;4;3;2]; #% 10x1 vector
    E = eye(length(V), length(V)); #%10x10 identity matrix
    M = cumsum(E(V,:),2)
    
    M =
    
         1     1     1     1     1     1     1     1     1     1
         0     1     1     1     1     1     1     1     1     1
         0     0     1     1     1     1     1     1     1     1
         0     0     0     1     1     1     1     1     1     1
         0     0     0     0     1     1     1     1     1     1
         0     0     0     0     0     1     1     1     1     1
         0     0     0     0     1     1     1     1     1     1
         0     0     0     1     1     1     1     1     1     1
         0     0     1     1     1     1     1     1     1     1
         0     1     1     1     1     1     1     1     1     1
    

    Ok, now: less fun, but (on my machine) faster than any of the other options that have been tested so far:

    n=10000;
    V = randi(n-1, 1, n); #% as in @KevinRatelle's answer (but not transposed)
    
    tic;
    Vlinear = reshape(V + (0:n-1)*n, 1, []); #% find linear indices of first "ones"
    M = zeros(n);
    M(Vlinear)=1;
    M=cumsum(M);
    toc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a matrix of ones and zeros, and I would like a
Say in Matlab I have a matrix like this: 1 2 3 4 5
let's say I have a matrix (array) like this example, but much larger: 0
Say I have a matrix that looks something like this: {{foobar, 77},{faabar, 81},{foobur, 22},{faabaa,
let's say I have a matrix like this: [,1] [,2] [,3] [,4] [,5] [,6]
This is an easy question but say I have an MxN matrix. All I
Say I have a matrix of values set.seed(1) A <- matrix(runif(25),ncol=5) I'd like to
Let's say I have a matrix with all 0's with the exception of the
So let's say I have a matrix, mdat and I only know the index
I have matrix (a) with (1:10),<10 x 1> double. I would like to copy

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.