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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:56:39+00:00 2026-06-17T19:56:39+00:00

in Matlab, I’m trying to pack arbitrary bit-length unsigned integers (e.g., an array of

  • 0

in Matlab, I’m trying to pack arbitrary bit-length unsigned integers (e.g., an array of 3-bit integers) into an array of uint8. Given the tip here, I can generate code that works for “small” arrays (say 10,000 elements), but it consumes all memory for large arrays (such as 16 million elements). The code I use is below, borrowing from previous postings:

function x_bytes = stuff_bits(x, n)
    r = dec2bin(x,n);                 % bitstring for each uint in x
    s = reshape(r',[],1);             % one continuous string of bits
    t = reshape(str2num(s),8,[])';    % array of 8-bit numbers (stuffed)
    u = t*(2.^(size(t,2)-1:-1:0))';   % array of bytes representing all the bits stuffed together
    x_bytes = uint8(u);              % should be compressed byte stream
end

I realize that I’m taking a uint, converting it to a string, then converting it back to a bit; I’ve also read that dec2bin is not terribly efficient.

When I try this with 16mil elements (on a 64-bit windows box with 8 GB of memory), all memory is consumed. Blah. So I loop over subsections and it requires about 10 minutes to complete the 16mil elements. So, something is very inefficient.

Anyone got a better way to generate bit strings like python’s BitArray?

thanks,

  • 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-17T19:56:40+00:00Added an answer on June 17, 2026 at 7:56 pm

    It seems to be similat to this one and this one

    At the first one, it was suggested to use dec2bitvec inside a for loop. This may be enought to you (altought slow).

    The second one sugests to create a lookup table using bitget and then use it (instead of using dec2bit or dec2bitvec)

    You can try to use something ‘in the middle’.

    B = 3; % Number of bits per int.
    A = randi(7, 16000000, 1); % 16M random elements between 1 and 7 (3bits).
    
    tic
    % get each group of bits in a column of K.
    K = cell2mat(arrayfun(@(bit)bitget(A, B+1-bit), 1:B, 'UniformOutput', 0))';
    % reshape to have them in 8 packs
    K = reshape(K, [8, numel(K)/8])';
    % get the uint8 vec.
    U = K*(2.^(size(K,2)-1:-1:0))';
    toc
    

    Mine was elapsed in 3.5 seconds. (Win8 64bits, i5 4GB ram)

    Instead of creating a lookup table, this code is creating a matrix (K) with bit values of each integer (stored in columns), reshaping it (to create 8bin value) and then using the same math as you used before to create the uint8 vector.

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

Sidebar

Related Questions

In Matlab, I was trying to put anonymous functions in an array: >> a=[@(k)0.1/(k+1)
When Matlab is installed in its 64 bit version, it can use only 64
In MATLAB, how do you write a matrix into an image of EPS format?
(MATLAB 2010 A Student version) Question: I am trying to solve 9 symbolic equations
MATLAB code exists to find the so-called minimum volume enclosing ellipsoid (e.g. here ,
In Matlab I have a class classdef myClass properties % some properties here... end
In MATLAB, vectorized code is faster than using for-loops. I have been trying to
Using MATLAB , I have an array of values of size 8 rows x
In Matlab, how can i get a String containing GRUMPY given the following declaration:
in MATLAB I have a cell array like this a = { 1 2

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.