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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:58:21+00:00 2026-06-13T10:58:21+00:00

UPDATE: I’ve done some testing, and the solution of Jonas is the fastest for

  • 0

UPDATE: I’ve done some testing, and the solution of Jonas is the fastest for a range of different size input vectors. In particular, as angainor points out, the solution scales up to large sizes incredibly well – an important test as it is usually the large size problems that prompt us to pose these kind of questions on SO. Thanks to both Jonas and tmpearce for your solutions – based on the efficiency of the solution for large size problems I’m giving the answer tick to Jonas.

My Question: I have this column vector:

Vec = [0; 1; 2; -1; -3; 0; 0; 2; 1; -1];

I would like to convert every element greater than one into a sequence of ones that has length equal to the value of the element. Similarly, I want to convert every element less than minus one into a sequence of minus ones. Thus my output vector should look like this:

VecLong = [0; 1; 1; 1; -1; -1; -1; -1; 0; 0; 1; 1; 1; -1];

Note that each 2 has been changed into two 1’s, while the -3 has been changed into three -1’s. Currently, I solve the problem like this:

VecTemp = Vec;
VecTemp(VecTemp == 0) = 1;
VecLong = NaN(sum(abs(VecTemp)), 1);
c = 1;
for n = 1:length(Vec)
    if abs(Vec(n)) <= 1
        VecLong(c) = Vec(n);
        c = c + 1;
    else
        VecLong(c:c + abs(Vec(n))) = sign(Vec(n));
        c = c + abs(Vec(n));
    end    
end

This doesn’t feel very elegant. Can anyone suggest a better method? Note: You can assume that Vec will contain only integer values. Thanks in advance for all suggestions.

  • 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-13T10:58:22+00:00Added an answer on June 13, 2026 at 10:58 am

    You can use the good old cumsum-approach to repeating the entries properly. Note that I’m assigning a few temporary variables that you can get rid of, if you want to put everything into one line.

    %# create a list of values to repeat
    signVec = sign(Vec);
    
    %# create a list of corresponding indices that repeat
    %# as often as the value in signVec has to be repeated
    
    tmp = max(abs(Vec),1); %# max: zeros have to be repeated once
    index = zeros(sum(tmp),1);
    index([1;cumsum(tmp(1:end-1))+1])=1; %# assign ones a pivots for cumsum
    index = cumsum(index); %# create repeating indices
    
    %# repeat
    out = signVec(index);
    out'
    out =
    
         0     1     1     1    -1    -1    -1    -1     0     0     1     1     1    -1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update : I found almost exact similar question , yet it has slightly different
UPDATE Suddenly - without changing anything - it works again ON SOME PAGES, on
[UPDATE] Was just an idiot mistake. See end for solution. I am trying to
UPDATE maybe changing the Bitmap size in the new Activity may fix the problem
UPDATE: Solution at bottom. I recently switched from using a set up such as
UPDATE : A commenter told me to change some codes, this is the new
UPDATE: Sorry, forgot to include some of the code (face-palm). I included it in
Update: It is solved .. For some reason, the data is not getting inserted
Update :Michael got it right. Here is my solution: - (void) connectNextCarOnMainThread:(id)annotation{ [self performSelectorOnMainThread:@selector(connectNextCar:)
Update: Is there a way to achieve what I'm trying to do in an

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.