Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical vs Numerical array in MATLAB

I am comparing two binary arrays. I have an array where values can either be one or zero, one if the values are the same and zero if they are not. Please note I am doing other stuff beyond checking, so we don't need to get into vectorization or the nature of the code.

What is more efficient, using a numerical array or a logical array in MATLAB?

like image 413
Elpezmuerto Avatar asked Jun 28 '10 15:06

Elpezmuerto


People also ask

What is a logical vector in MATLAB?

You can use logical vectors to extract a selection of rows or columns from a matrix, for example, if a is the original 3-by-3 matrix defined above, the statement: From: Essential Matlab for Engineers and Scientists (Fifth Edition), 2013.

How many types of arrays are there in MATLAB?

There are 17 fundamental classes in MATLAB. Each of these classes is in the form of a matrix or array. With the exception of function handles, this matrix or array is a minimum of 0-by-0 in size and can grow to an n-dimensional array of any size.

What is logical data type in MATLAB?

MATLAB® represents Boolean data using the logical data type. This data type represents true and false states using the numbers 1 and 0 , respectively. Certain MATLAB functions and operators return logical values to indicate fulfillment of a condition.


2 Answers

Logical values take up fewer bytes than most numeric values, which is a plus if you're dealing with very large arrays. You can also use logical arrays to do logical indexing. For example:

>> valArray = 1:5;                   %# Array of values
>> numIndex = [0 1 1 0 1];           %# Numeric array of ones and zeroes
>> binIndex = logical([0 1 1 0 1]);  %# Logical array of ones and zeroes
>> whos
  Name          Size            Bytes  Class      Attributes

  binIndex      1x5                 5  logical       %# 1/8 the number of bytes
  numIndex      1x5                40  double        %#   as a double array
  valArray      1x5                40  double               

>> b = valArray(binIndex)            %# Logical indexing

b =

     2     3     5

>> b = valArray(find(numIndex))      %# You have to use the FIND function to
                                     %#   find the indices of the non-zero
b =                                  %#   values in numIndex

     2     3     5

One note: If you will be dealing with arrays of zeroes and ones that are very sparse (i.e. very few ones), it may be best to use an array of numeric indices such as you would get from the FIND function.Take the following example:

>> binIndex = false(1,10000);      %# A 1-by-10000 logical array
>> binIndex([2 100 1003]) = true;  %# Set 3 values to true
>> numIndex = find(binIndex)       %# Find the indices of the non-zero values

numIndex =

           2         100        1003

>> whos
  Name          Size               Bytes  Class      Attributes

  binIndex      1x10000            10000  logical       %# 10000 bytes versus
  numIndex      1x3                   24  double        %#   many fewer bytes
                                                        %#   for a shorter array
like image 114
gnovice Avatar answered Oct 23 '22 04:10

gnovice


Logical of course! Matlab has the option of squeezing 8 items into 1 byte. (Whether it does or not is another matter).

a=ones(1000); b=(a==1);
tic;for(k=1:100)for(i=1:1000);for(j=1:1000);a(i,j)=a(i,j);end;end;end;toc
tic;for(k=1:100)for(i=1:1000);for(j=1:1000);b(i,j)=b(i,j);end;end;end;toc

result

4.561173 seconds
3.454697 seconds

but the benefit will be much greater if you're doing more logical operations rather than just looping!

like image 35
Sanjay Manohar Avatar answered Oct 23 '22 03:10

Sanjay Manohar