Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Binary matrix?

Subject: Binary matrix?

From: Chuck37

Date: 21 Oct, 2012 03:12:08

Message: 1 of 7

I'm looking for suggestions on how to work with a 2D array of binary values. I have the data in a file, but I'd like to have a 2D matrix of binaries that I can look up values in. The matrix size is way too big to just let the binaries be wastefully stored as doubles. I could read the file in as characters or uint8 or something, but then I'd need to figure out how to index into this. Is there an easier way?

Subject: Binary matrix?

From: Matt J

Date: 21 Oct, 2012 04:50:09

Message: 2 of 7

"Chuck37" wrote in message <k5vp68$h5o$1@newscl01ah.mathworks.com>...
> I'm looking for suggestions on how to work with a 2D array of binary values. I have the data in a file, but I'd like to have a 2D matrix of binaries that I can look up values in. The matrix size is way too big to just let the binaries be wastefully stored as doubles. I could read the file in as characters or uint8 or something, but then I'd need to figure out how to index into this. Is there an easier way?
===========

I'm not sure what you think is more difficult about indexing a uint8 matrix than a double matrix. It's exactly the same.

BTW, once you've read in to a uint8 matrix, it would probably be appropriate to convert it to type logical. Also, be aware that sparse matrices can hold type logical, if that's applicable to you.

Subject: Binary matrix?

From: Chuck37

Date: 21 Oct, 2012 15:12:07

Message: 3 of 7

Each element is a single bit. If I keep each element as uint8, I waste a lot of memory. I was only suggesting I could possibly read the file in as a stream of uint8, but then I'd have to index it, i.e. figure out which uint8 to use, then extract a particular bit from that uint8. If I could directly index B(x,y) and get binary/boolean results without wasting memory, that would be better.

"Matt J" wrote in message <k5vuu0$5ua$1@newscl01ah.mathworks.com>...
> "Chuck37" wrote in message <k5vp68$h5o$1@newscl01ah.mathworks.com>...
> > I'm looking for suggestions on how to work with a 2D array of binary values. I have the data in a file, but I'd like to have a 2D matrix of binaries that I can look up values in. The matrix size is way too big to just let the binaries be wastefully stored as doubles. I could read the file in as characters or uint8 or something, but then I'd need to figure out how to index into this. Is there an easier way?
> ===========
>
> I'm not sure what you think is more difficult about indexing a uint8 matrix than a double matrix. It's exactly the same.
>
> BTW, once you've read in to a uint8 matrix, it would probably be appropriate to convert it to type logical. Also, be aware that sparse matrices can hold type logical, if that's applicable to you.

Subject: Binary matrix?

From: Matt J

Date: 21 Oct, 2012 19:12:08

Message: 4 of 7

"Chuck37" wrote in message <k613c7$3kt$1@newscl01ah.mathworks.com>...
> Each element is a single bit. If I keep each element as uint8, I waste a lot of memory. I was only suggesting I could possibly read the file in as a stream of uint8, but then I'd have to index it, i.e. figure out which uint8 to use, then extract a particular bit from that uint8. If I could directly index B(x,y) and get binary/boolean results without wasting memory, that would be better.
=============

FREAD let's you read from the file with a 'ubitn' precision type. I've never used it, but I'm guessing that let's you read in individual bits. So if you passed a precision argument of 'ubit1=>uint8', that might do what you want.

Subject: Binary matrix?

From: Chuck37

Date: 22 Oct, 2012 04:34:08

Message: 5 of 7

"Matt J" wrote in message <k61he8$j5p$1@newscl01ah.mathworks.com>...
> "Chuck37" wrote in message <k613c7$3kt$1@newscl01ah.mathworks.com>...
> > Each element is a single bit. If I keep each element as uint8, I waste a lot of memory. I was only suggesting I could possibly read the file in as a stream of uint8, but then I'd have to index it, i.e. figure out which uint8 to use, then extract a particular bit from that uint8. If I could directly index B(x,y) and get binary/boolean results without wasting memory, that would be better.
> =============
>
> FREAD let's you read from the file with a 'ubitn' precision type. I've never used it, but I'm guessing that let's you read in individual bits. So if you passed a precision argument of 'ubit1=>uint8', that might do what you want.

Thanks, but then I'd still be using 8x the memory necessary if I stored every bit using 8 bits. For an image with millions of pixels, this is significant.

Subject: Binary matrix?

From: Bruno Luong

Date: 22 Oct, 2012 11:41:08

Message: 6 of 7

"Chuck37" wrote in message <k62ic0$6in$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <k61he8$j5p$1@newscl01ah.mathworks.com>...
>
> Thanks, but then I'd still be using 8x the memory necessary if I stored every bit using 8 bits. For an image with millions of pixels, this is significant.

MATLAB supports primitively bitwise operations (see bitand, bitor, bitshift, etc...). They are slow and not convenient to work with.

If you want something with decent performance, I see only one choice: program your own MEX file as bit manipulation engine.

Bruno

Subject: Binary matrix?

From: Matt J

Date: 22 Oct, 2012 11:45:08

Message: 7 of 7

"Chuck37" wrote in message <k62ic0$6in$1@newscl01ah.mathworks.com>...
>
> > FREAD let's you read from the file with a 'ubitn' precision type. I've never used it, but I'm guessing that let's you read in individual bits. So if you passed a precision argument of 'ubit1=>uint8', that might do what you want.
>
> Thanks, but then I'd still be using 8x the memory necessary if I stored every bit using 8 bits. For an image with millions of pixels, this is significant.
============

Millions of pixels doesn't actually sound all that significant, but regardless, MATLAB gives you no alternative, I'm afraid. It offers no matrix data types that consume less than 1 byte per element.

Remember what I said, though, about sparse matrices. If your bits are mostly zeros, storing them as type sparse would conserve a lot of memory.

  help sparse

Or, if they are mostly ones, then not(yourmatrix) would be sparse instead and you could work in terms of that.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us