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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:12:08+00:00 2026-06-09T23:12:08+00:00

I want to make an array called result , which has dimensions (14,12,10) and

  • 0

I want to make an array called result, which has dimensions (14,12,10) and has values random[0 2], but it should not contain 0 more than three consecutive times in each row (dimension 2), and the sum of all values in each row must be <= 11.

This is my current approach:

jum_kel = 14;
jum_bag = 12;
uk_pop = 10;

for ii = 1:uk_pop,
  libur(:,:,ii) = randint(jum_kel,jum_bag,[0 2]); %#initialis
  sum_libur(1,:,ii) = sum(libur(:,:,ii),2); %#sum each row
end

for jj = 1:jum_kel
  while sum_libur(1,jj,ii) > 11, %# first constraint : sum each row should be <=11,
    libur(jj,:,ii) = randint(1,jum_bag,[0 2])
    sum_libur(1,:,ii)=  sum(libur(:,:,ii),2);
    for kk = 1:jum_bag
      if kk>2
        o = libur(jj,kk,ii)+libur(jj,kk-1,ii)+libur(jj,kk-2)
        while kk>2 && o==0 %# constraint 2: to make matrix will not contain consecutive triplets (0-0-0) in any row.
          libur(jj,:,ii) = randint(1,jum_bag,[0 2]); 
          sum_libur(1,:,ii)=  sum(libur(:,:,ii),2);
        end
      end
    end
  end
end

but this is extremely slow…Does anyone see a faster method?

  • 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-09T23:12:09+00:00Added an answer on June 9, 2026 at 11:12 pm

    As indicated by @ninjagecko, rejecting and recreating is the most obvious (if not the only) way to go here. In that light, I think this’ll do nicely:

    R = 14;
    C = 12;
    T = 10;
    
    result = zeros(C,R*T);
    
    for ii = 1:(R*T)
        done = false;
        while ~done
            newRow = randi([0 2],C,1);
            done = ...
                (sum(newRow)<12) && ...
                (~any(diff([0;find(newRow);C+1])>3));
        end
        result(:,ii) = newRow;
    end
    
    result = permute(reshape(result,C,R,T), [2 1 3]);
    

    Note that <12 equals <=11 for integer math (and >3 equals >=4), but requires one less check and is thus faster. The variable newRow is created as a column vector, since such things are better organized in memory and can be checked faster. 3D arrays are generally slower to assign to than 2D matrices, therefore everything is kept 2D until after all operations. The most computationally intense check (~any(diff(find(newRow))>3)) is done last, so that short-circuiting (&&) may render its evaluation unnecessary. Both loops are small and fulfil all conditions to be compiled to machine code by Matlab’s JIT.

    So this is pretty close to optimized, and will be pretty fast. Unless someone comes up with an entirely different algorithm for this, I believe this is pretty close to the theoretical maximum speed in Matlab. If you need to go faster, you’ll have to switch to C (which will allow optimization of the constraint checks).

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

Sidebar

Related Questions

I want to make an array in PHP but it should be in specific
i want to find values in array according to alphabate and want to make
I got a result set in array format and i want to make square
i have a class called (User) and i want to make a multidimensional array
I want to make my vertex array dynamic. So that values would be added
I want to make an array that contains several arrays that store results of
I want to make an array of specific types of files with .txt that
I want to make a 2D array dij(i and j are subscripts). I want
I want to make a variable length array in Javascript. Is this possible. A
I want to combine 2 parts of the same array to make a complex

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.