计算充满向量的单元格中的唯一行

计算充满向量的单元格中的唯一行

本文介绍了计算充满向量的单元格中的唯一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有一个单元格,其中每个元素都包含一个不同长度的向量

I have a cell in MATLAB where each element contains a vector of a different length

例如

C = {[1 2 3], [2 4 5 6], [1 2 3], [6 4], [7 6 4 3], [4 6], [6 4]}

如您所见,某些向量是重复的,其他向量是唯一的.

As you can see, some of the the vectors are repeated, others are unique.

我想计算每个向量发生的次数并返回计数,这样我就可以在GUI中填充表格,其中每一行都是唯一的组合,而日期则显示每种组合发生的次数.

I want to count the number of times each vector occurs and return the count such that I can populate a table in a GUI where each row is a unique combination and the date shows how many times each combination occurs.

例如

            Count
"[1 2 3]"     2
"[6 4]"       2
"[2 4 5 6]"   1
"[7 6 4 3]"   1
"[4 6]"       1

我应该说每个向量中数字的顺序很重要,即[6 4]与[4 6]不同.

I should say that the order of the numbers in each vector is important i.e. [6 4] is not the same as [4 6].

任何人都在想我如何才能有效地做到这一点?

Any thoughts how I can do this fairly efficiently?

感谢到目前为止发表评论的人.正如@Divakar友好指出的那样,我忘了提及向量中的值可以超过一位数的长度.即[46, 36 28].我的原始代码会将向量[1 2 3 4]连接到1234,然后使用hist进行计数.当然,当您获得一位以上的数字时,这会分崩离析,因为您可以分辨出[1, 2, 3, 4][12, 34]之间的区别.

Thanks to people who have commented so far. As @Divakar kindly pointed out, I forgot to mention that the values in the vector can be more than one digit long. i.e. [46, 36 28]. My original code would concatenate the vector [1 2 3 4] into 1234 then use hist to do the counting. Of course this falls apart when you got above single digits as you can tell the difference between [1, 2, 3, 4] and [12, 34].

推荐答案

您可以将所有条目转换为char,然后转换为2D数字数组,最后使用unique(...'rows')获取唯一行的标签,并使用它们来获取它们的标签.计数.

You can convert all the entries to char and then to a 2D numeric array and finally use unique(...'rows') to get labels for unique rows and use them to get their counts.

C = {[46, 36 28], [2 4 5 6], [46, 36 28], [6 4], [7 6 4 3], [4 6], [6 4]} %// Input

char_array1 = char(C{:})-0; %// convert input cell array to a char array
[~,unqlabels,entry_labels] = unique(char_array1,'rows'); %// get unique rows
count = histc(entry_labels,1:max(entry_labels)); %// counts of each unique row

出于以问题所要求的格式显示输出的目的,可以使用此-

For the purpose of presenting the output in a format as asked in the question, you can use this -

out = [C(unqlabels)' num2cell(count)];


输出-


Output -

out =
    [1x4 double]    [1]
    [1x2 double]    [1]
    [1x2 double]    [2]
    [1x4 double]    [1]
    [1x3 double]    [2]

并使用celldisp-

ans{1} =
     2     4     5     6
ans{2} =
     4     6
ans{3} =
     6     4
ans{4} =
     7     6     4     3
ans{5} =
    46    36    28


如果其中有负数,则需要做更多的工作来设置char_array1,如下所示,其余代码保持不变-


If you have negative numbers in there, you need to do little more work to setup char_array1 as shown here and rest of the code stays the same -

lens = cellfun(@numel,C);
mat1(max(lens),numel(lens))=0;
mat1(bsxfun(@ge,lens,[1:max(lens)]')) = horzcat(C{:});
char_array1 = mat1';

这篇关于计算充满向量的单元格中的唯一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:44