逻辑VS在MATLAB数值数组 [英] Logical vs Numerical array in MATLAB

查看:164
本文介绍了逻辑VS在MATLAB数值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我比较两个二进制数组。我有一个阵列,其中值可以是一或零,一个,如果该值是相同的和零,如果他们不。请注意,我做其他的东西超出了检查,所以我们并不需要进入量化或code的性质。

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.

这是更有效的,使用数值阵列或在MATLAB逻辑阵列

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

推荐答案

逻辑值占用较少的字节比大多数的数字值,这是一个加号,如果你正在处理非常大的阵列。您还可以使用逻辑阵列做逻辑索引。例如:

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

一注:如果您将处理是非常稀疏的(即极少数的)的0和1的阵列,它可能是最好使用数字索引的数组,如你会从 FIND function.Take下面的例子:

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

这篇关于逻辑VS在MATLAB数值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆