[英]How can I count the number of items using the FIND function in MATLAB?
How can I use the function FIND
to count the number of items of a given value instead of using a loop? 如何使用函数
FIND
计数给定值的项目数,而不是使用循环? For example, in the array item
below there are 3 occurrences of the number 23, 2 occurrences of the number 22, and 2 occurrences of the number 20. 例如,在下面的数组
item
中,数字23出现3次,数字22出现2次,数字20出现2次。
....
for i=2:n
if item(i-1)~=item(i)
nItem21(i)=1;
else
nItem21(i)=nItem21(i-1)+1;
end
end
item Num
23 2
23 4
23 6
22 3
22 1
20 6
20 8
You can do the following: identify where the value of item
changes, then use diff to get the counts. 您可以执行以下操作:确定
item
的值在哪里更改,然后使用diff获取计数。
item = [
23
23
23
22
22
20
20];
% find the 'last' entries of each consecutive group of numbers
chgRowNum = [find(item(1:end-1) ~= item(2:end);length(item)];
counts = diff([0;chgRowNum]);
correspondingItems = item(chgRowNum);
Find
returns the indices of nonzero elements in an array. Find
返回数组中非零元素的索引。 If you want a count of all the occurrences of your elements (assuming they're integers), you can use the hist
function. 如果您希望对元素的所有出现进行计数(假设它们是整数),则可以使用
hist
函数。 By assigning an output, it won't plot a histogram. 通过分配输出,它将不会绘制直方图。 Instead it will return an array of occurrences.
相反,它将返回事件数组。
x=[20 23 20 22 23 21 23 22];
bins=min(x):max(x);
count=hist(x,bins);
list=unique(x);
Now count
contains the number of occurrences, and list
contains each unique array element. 现在
count
包含出现的次数,而list
包含每个唯一的数组元素。 To get rid of zero count elements: 摆脱零计数元素:
idx=find(count);
count=count(idx);
or the one line option (without using find): 或单行选项(不使用查找):
count=count(count~=0);
Just for completeness I would use the histc
function. 为了完整
histc
我将使用histc
函数。
item = [
23
23
23
22
22
20
20];
%get the unique items
[uni_items, minds, uinds] = unique(item);
%count them
counts = histc(uinds, 1:numel(uni_items));
%put them in the original order
ocounts = counts(minds);
This takes care of them not being in order or if they're not integers. 这样可以避免它们不按顺序排列或不是整数。
Another option for this situation is to use the function ACCUMARRAY , which wouldn't require that the list be sorted first. 这种情况的另一种选择是使用功能ACCUMARRAY ,它不需要先对列表进行排序。 This is particularly useful if you were to have a set of numbers in
item
that span a range of 1:N
, where N
is any integer value. 如果您要在
item
中使用一组范围为1:N
的数字,这特别有用,其中N
是任何整数值。 Here's how it would work for your example: 这是您的示例的工作方式:
item = [23; 23; 23; 22; 22; 20; 20]; %# A column vector of integers
counts = accumarray(item,1); %# Collect counts of each item into
%# a 23-by-1 array
The array counts
is a 23-by-1 array where the elements indexed by 23, 22, and 20 contain the counts 3, 2, and 2, respectively. 数组
counts
是一个23 x 1数组,其中由23、22和20索引的元素分别包含计数3、2和2。 All the other elements are 0 (ie there are no counts found for the numbers 1 through 19 or 21). 所有其他元素均为0(即,没有找到1到19或21的数字)。
If you want to get a list of the unique values in item
and their corresponding counts, you can do this using the function UNIQUE : 如果要获取
item
中的唯一值及其对应计数的列表,可以使用UNIQUE函数:
>> uniqueValues = unique(item) %# Get the unique values in item
uniqueValues =
20
22
23
>> counts = counts(uniqueValues) %# Get just the counts for each unique value
counts =
2
2
3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.