如何从单元阵列比较后删除元素,而不会导致电池阵列得到空的? [英] How to delete element from cell arrays after comparisons without causing cell arrays to get empty?

查看:156
本文介绍了如何从单元阵列比较后删除元素,而不会导致电池阵列得到空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一维数组,显示它们发生的话,并在其中的句子。从那以后,我把路口显示哪些词出现在句子,其中其他剩余的字,每个:

I created 1D array which shows words and in which sentences they occur. After that I took the intersection to show which word occurs with which each of other remaining words in sentence:

OccursTogether = cell(length(Out1));
for ii=1:length(Out1)
for jj=ii+1:length(Out1)
OccursTogether{ii,jj} = intersect(Out1{ii},Out1{jj});
end
end
celldisp(OccursTogether)

以上code的输出如下:

the output of above code is as below:

OccursTogether{1,1} =

 4 11 14

OccursTogether{1,2} =

 1
OccursTogether{1,3} =

 []
OccursTogether{1,4} =

 1 4 8 14 15 19 20 22

OccursTogether{1,5} =

 4 11

我要检查一个单一的元素,如果将其删除不会导致空集它应该被删除,但如果将其删除引起空集,它不应该被删除。

I want to check a single element if its deletion doesn't cause empty set it should be deleted but if its deletion give rise to empty set it should not be deleted.

例如:

步骤1: 4删除从 {1,1} {1,5 } 它不会是空的,所以4应予删除。

step1:Delete 4 from {1,1}and{1,5} it will not be empty so 4 should be deleted.

第二步:删除 14 {1,1} {1,4 } 它不会是空的,所以14也应予以删除。

step2: Delete 14 from {1,1}and{1,4} it will not be empty so 14 should also be deleted.

第三步:如果我从删除11 {1,1} {1 ,5} 就会造成空集,因为 4 14 在<删除code>第1步和第2步所以它不应该被删除。

step3: If i delete 11 from {1,1}and{1,5} it will cause empty set because 4 and 14 are deleted in step 1 and step 2 so it should not be deleted.

元素删除操作应为阵列的所有单元来进行。 OccursTogether 声明为1D单元阵列

Element deletion operations should be carried out for all the cells of the arrays.OccursTogether is declared as 1D cell array.

如何code键使所有 OccursTogether 单元阵列的位置比较和缺失?

How can i code to make Comparisons and deletions for all OccursTogether cell array locations?

推荐答案

使用以下命令,其中 C 为你的 OccursTogether 细胞(更短,从而更易阅读此答案)。在code中的注释解释了一下什么相应的行做的。

Use the following, where C stands for your OccursTogether cell (shorter, thus easier to read for this answer). The comments in the code explain a bit what the corresponding lines do.

C = cell(3,2);

C{1,1} = [4 11 14];
C{2,1} = 1;
C{2,2} = [1 4 8 14 15 19 20 22];
C{3,2} = [4 11];
celldisp(C)

C = cellfun(@unique, C, 'UniformOutput', false); % remove duplicates from elements of C

numsInCell = unique([C{:}]); % numbers in cell, sorted

for n = numsInCell % loop over numbers in cell
    lSetIs1 = cellfun(@numel,C) == 1; % length of set is 1
    nInSet = cellfun(@(set) any(set==n), C); % set contains n
    nIsUnique = sum(nInSet(:))==1; % n occurs once
    condition = ~nIsUnique & ~any(nInSet(:) & lSetIs1(:)); % set neither contains n and has length of 1, nor n is unique
    if condition % if false for all sets...
        C = cellfun(@(set) set(set~=n), C, 'UniformOutput', false); % ... then remove n from all sets
    end
end

celldisp(C)

请注意我用的逻辑索引在 -loop与启动C = cellfun(... ,这样可以节省额外的 -loop超过 C 的元素。MATLAB函数 cellfun 执行在第二个参数中的单元格的内容它的第一个参数的函数句柄。这是一个非常有用的工具,prevents使用许多 -loops甚至有些如果 -statements。

Notice I use logical indexing in the line in the for-loop starting with C = cellfun(..., which saves you an additional for-loop over the elements of C. The MATLAB function cellfun performs the function handle in its first argument on the elements of the cell in the second argument. This is a very useful tool that prevents the use of many for-loops and even some if-statements.

这篇关于如何从单元阵列比较后删除元素,而不会导致电池阵列得到空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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