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

  • Home
  • SEARCH
  • 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 8812613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:38:35+00:00 2026-06-14T03:38:35+00:00

I would like to generate all possible integer arrays of a given length L

  • 0

I would like to generate all possible integer arrays of a given length L, up to a maximum element size M.

The minimum element size is 1.

If M = 3, and L = 2, the output would be the following:

[1,1]
[1,2]
[1,3]
[2,1]
[2,2]
[2,3]
[3,1]
[3,2]
[3,3]

There are M^L different combinations, so I guess the Matlab code would look something like this:

function [arrays] = allArrays(M,L)
  for i = 1:(M^L)
    arrays(i) = % Something here that translates i to the desired array.
  end
end

I’m not sure what should go in the middle of the loop, any help would be much appreciated!

  • 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-14T03:38:37+00:00Added an answer on June 14, 2026 at 3:38 am

    You should use ndgrid:

    [y x]=ndgrid(1:3,1:3);
    resu=[y(:) x(:)];
    

    If you want to give M and L as input, you should do the following trick:

    arg=repmat((1:M)',1,L);
    arg=mat2cell(arg,M,ones(1,L));
    resu=cell(1,L);
    [resu{:}]=ndgrid(arg{:});
    resu=cell2mat(cellfun(@(x) x(:), resu,'UniformOutput',0));
    

    result:

     resu =  
         1     1
         2     1
         3     1
         1     2
         2     2
         3     2
         1     3
         2     3
         3     3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to generate all possible strings from a given
I have the following need (in python): generate all possible tuples of length 12
we're going to generate all possible math equations today. Given a a syntax like
I would like to generate a list of all possible permutations of a string
I would like to generate all the possible combinations of the elements of a
I would like to generate a patch with all commits from a local branch.
I would like to generate POJO, XML for a given database. Database: ( contents
For given n inputs, I need to generate all possible input combinations using C++
I'm trying to generate all possible combinations for pair of 1's within given bit
i would like to generate a very simple report with some images and text

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.