MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   MATLAB中的逻辑数组与数值数组 (https://www.labfans.com/bbs/showthread.php?t=23605)

poster 2019-12-10 20:42

MATLAB中的逻辑数组与数值数组
 
我正在比较两个二进制数组。我有一个数组,其中值可以是一个或零,如果值相同,则为1,否则为零。请注意,除了检查之外,我还在做其他事情,所以我们不需要进入向量化或代码的本质。

在MATLAB中使用数值数组或逻辑数组更有效?



[B]回答:[/B]

[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-97022.html"]逻辑[/URL]值占用的字节数少于大多数[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-12135.html"]数字[/URL]值,如果要处理非常大的数组,则这是一个加号。您也可以使用逻辑数组进行[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-85462.html#bq7egb6-1"]逻辑索引[/URL] 。例如:

>> 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 [B]一个注意事项:[/B]如果要处理零和非常稀疏的零(即极少的一个)数组,则最好使用数字索引数组,例如从[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/find.html"]FIND[/URL]函数获得的数字索引。 :

>> 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

[url=https://stackoverflow.com/questions/3133712]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:08

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.