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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:23:57+00:00 2026-06-07T03:23:57+00:00

Okay, this might seem like a weird question, but bear with me. So I

  • 0

Okay, this might seem like a weird question, but bear with me.

So I have a random vector in a .m file, with certain constraints built into it. Here is my code:

 randvecall = randsample(done, done, true);
 randvec = randvecall([1;diff(randvecall(:))]~=0);

“Done” is just the range of values we take the sample from, so don’t worry about that. As you can see, this randsamples from a range of values, and then prunes this random vector with the diff function, so that consecutive duplicate values are removed. There is still the potential for duplicate values in the vector, but they simply cannot be consecutive.

This is all well and good, and works perfectly fine.

So, say, randvec looks like this:

randvec =

    54
    47
    52
    26
    39
     2
    14
    51
    24
     6
    19
    56
    34
    46
    12
     7
    41
    18
    29
     7

It is actually a lot longer, with something like 60-70 values, but you get the point.

What I want to do is add a little extra constraint on to this vector. When I sample from this vector, the values are classified according to their range. So values from 1-15 are category 1, 16-30 are category 2, and so on. The reasons for this are unimportant, but it is a pretty important part of the program. So if you look at the values I provided above, you see a section like this:

     7
    41
    18
    29
     7

This is actually bad for my program. Because the value ranges are treated separately, 41, 18, and 29 are used differently than 7 is. So, for all intents and purposes, 7 is appearing consecutively in my script. What I want to do is somehow parse/modify/whatever the vector when it is generated so that the same number from a certain range cannot appear twice “in a row,” regardless of how many other numbers from different ranges are between them. Does this make sense/did I describe this well? So, I want MATLAB to search the vector, and for all values within certain ranges (1-15,16-30,31-45,46-60) make sure that “consecutive” values from the same range are not identical.

So, then, that is what I want to do. This may not by any means be the best way to do this, so any advice/alternatives are, of course, appreciated. I know I can do this better with multiple vectors, but for various reasons I need this to be a single, long vector (the way my script is designed it just wouldn’t work if I had a separate vector for each range of values).

  • 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-07T03:23:59+00:00Added an answer on June 7, 2026 at 3:23 am

    What you may want to do is create four random vectors, one for each category, ensure that they do not contain any two consecutive equal values, and then build your final random vector by ordered picking of values from random categories, i.e.

    %# make a 50-by-nCategories array of random numbers
    categories = [1,16,31,46;15,30,45,60]; %# category min/max
    nCategories = size(categories,2);
    randomCategories = zeros(50,nCategories);
    
    for c=1:nCategories
       %# draw 100 numbers for good measure
       tmp = randi(categories(:,c),[100 1]);
       tmp(diff(tmp==0)) = []; %# remove consecutive duplicates
    
       %# store
       randomCategories(:,c) = tmp(1:50);
    end
    
    %# select from which bins to pick. Use half the numbers, so that we don't force the 
    %# numbers of entries per category to be exactly equal
    
    bins = randi(nCategories,[100,1]);
    
    %# combine the output, i.e. replace e.g. the numbers
    %# '3' in 'bins' with the consecutive entries
    %# from the third category
    out = zeros(100,1);
    for c = 1:nCategories
       cIdx = find(bins==c);
       out(cIdx) = randomCategories(1:length(cIdx),c);
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so this might be kind of strange but here's the question: I have
This might be a n00bish question, but whatever. Is okay to use exceptions for
Okay this may be a simple question but I have yet to come with
Okay, the question might seem dumb, but I'm asking it anyways. After struggling for
Okay, this might be a very silly beginner question, but: I've got an ClassA,
okay this might be a pretty lame and basic question but its stuck in
Okay this is probably a rookie question, but I have never done GUI programming
okay, this might be a silly question. So I have some tuples of size
Okay, this question comes through a friend so it might be lost in translation...
okay so this is probably a soft pitch question for sombody, but I want

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.