Pack/Unpack Logicals

Compress large logical variables to one bit per element, instead of the usual 8.
1.2K Downloads
Updated 28 Dec 2004

View License

MATLAB (6.5 at least) stores logical variables in memory as 8 bits per element, even though only one bit is required. This means a 512x512x100 logical mask takes up 25 MB of memory. By compressing this mask the same amount of data takes only 3MB.

A limitation is that compressed logicals are stored in a struct array, and cannot be operated on in a meaningful way without unpacking them. I use this compression to store many large masks in memory, and then I unpack them when I am ready to use them.

Cite As

James Alaly (2024). Pack/Unpack Logicals (https://www.mathworks.com/matlabcentral/fileexchange/6586-pack-unpack-logicals), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0

Updated to store size of original logical vector/array in the packed data structure. Idea by Iram Weinstein.

Also added a function isPackedLogical to test if a variable needs to be unpacked.