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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:47:54+00:00 2026-05-31T11:47:54+00:00

Given a positive integer n, I want to generate all possible n bit combinations

  • 0

Given a positive integer n, I want to generate all possible n bit combinations in matlab.
For ex : If n=3, then answer should be

000
001
010
011
100
101
110
111

How do I do it ?
I want to actually store them in matrix. I tried

for n=1:2^4 
r(n)=dec2bin(n,5); 
end; 

but that gave error “In an assignment A(:) = B, the number of elements in A and B must be the same.

  • 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-05-31T11:47:55+00:00Added an answer on May 31, 2026 at 11:47 am

    Just loop over all integers in [0,2^n), and print the number as binary. If you always want to have n digits (e.g. insert leading zeros), this would look like:

    for ii=0:2^n-1,
        fprintf('%0*s\n', n, dec2bin(ii));
    end
    

    Edit: there are a number of ways to put the results in a matrix. The easiest is to use

    x = dec2bin(0:2^n-1);
    

    which will produce an n-by-2^n matrix of type char. Each row is one of the bit strings.

    If you really want to store strings in each row, you can do this:

    x = cell(1, 2^n);
    for ii=0:2^n-1,
        x{ii} = dec2bin(ii);
    end
    

    However, if you’re looking for efficient processing, you should remember that integers are already stored in memory in binary! So, the vector:

    x = 0 : 2^n-1;
    

    Contains the binary patterns in the most memory efficient and CPU efficient way possible. The only trade-off is that you will not be able to represent patterns with more than 32 of 64 bits using this compact representation.

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

Sidebar

Related Questions

Given k positive integer numbers a 1 < a 2 < a 3 <
Let A be an array of n positive integers, and k a given integer.
I had this question: Given an unsorted array of positive integers and an integer
I want to calculate the result, given any exponent (negative or positive) and a
Given a positive integer sequence of numbers in an array with common difference 2
Consider this problem: You are given an array containing positive integers. All the integers
Given an array of positive integer, we need to exchange each element with first
Suppose I am given a sorted list of elements and I want to generate
Given a positive integer X , how can one partition it into N parts,
Question: Given an unsorted array of positive integers, is it possible to find a

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.