我正在比较两个二进制数组。我有一个数组,其中值可以是一或零,如果值相同则为 1,如果不同则为零。请注意,我正在做检查之外的其他事情,因此我们不需要进入矢量化或代码的性质。
在 MATLAB 中使用数值数组和逻辑数组哪个更有效?
Logical http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-97022.html值比大多数值占用更少的字节numeric http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-12135.html值,如果您正在处理非常大的数组,这是一个优点。您还可以使用逻辑数组来执行逻辑索引 http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-85462.html#bq7egb6-1。例如:
>> 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
一则注释:如果您要处理非常稀疏(即很少)的零和一数组,则最好使用数字索引数组,例如从FIND http://www.mathworks.com/access/helpdesk/help/techdoc/ref/find.html函数。看下面的例子:
>> 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)