MATLAB:计算表列中的字符串出现次数 [英] MATLAB: Count string occurrences in table columns

查看:54
本文介绍了MATLAB:计算表列中的字符串出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找此表中的单词数:

I'm trying to find the amount of words in this table:

在此处下载表: http://www.mediafire.com/file/m81vtdo6bdd7bw8/Table_RandomInfoMiddle.mat/file

单词由"Type"表示.标准,即字母".要注意的关键是,不是表中的所有内容都是单词,并且条目"位于表中.注册为单词.换句话说,我需要通过仅计算字母"来确定单词的数量,除非它是缺失".

Words are indicated by the "Type" criteria, being "letters". The key thing to notice is that not everything in the table is a word, and that the entry "" registers as a word. In other words I need to determine the amount of words, by only counting "letters", except if it is a "missing".

这是我的尝试(但未成功-请注意问题区域"的两个提及):

Here is my attempt (Yet unsuccessful - Notice the two mentions of "Problem area"):

for col=1:size(Table_RandomInfoMiddle,2)
    column_name = sprintf('Words count for column %d',col);
    MiddleWordsType_table.(column_name) = nnz(ismember(Table_RandomInfoMiddle(:,col).Variables,{'letters'}));
    MiddleWordsExclusionType_table.(column_name) = nnz(ismember(Table_RandomInfoMiddle(:,col).Variables,{'<missing>'})); %Problem area
end
%Call data from table
    MiddleWordsType = table2array(MiddleWordsType_table);
    MiddleWordsExclusionType = table2array(MiddleWordsExclusionType_table); %Problem area
%Take out zeros where "Type" was
    MiddleWordsTotal_Nr = MiddleWordsType(MiddleWordsType~=0); 
    MiddleWordsExclusionTotal_Nr = MiddleWordsExclusionType(MiddleWordsExclusionType~=0);
%Final answer
    FinalMiddleWordsTotal_Nr = MiddleWordsTotal_Nr-MiddleWordsExclusionTotal_Nr;

任何帮助将不胜感激.谢谢!

Any help will be appreciated. Thank you!

推荐答案

使用第2列满足某些条件时,可以从第1列中获得唯一值.

You can get the unique values from column 1 when column 2 satisfies some condition using

MiddleWordsType = numel( unique( ...
   Table_RandomInfoMiddle{ismember(Table_RandomInfoMiddle{:,2}, 'letters'), 1} ) );

< missing> 是分类数组中的关键字,而不是字面上的字符串"< missing>" .这就是为什么它在工作区中显示为蓝色和斜体的原因.如果要专门检查缺失值,可以使用它代替 ismember :

<missing> is a keyword in a categorical array, not literally the string "<missing>". That's why it appears blue and italicised in the workspace. If you want to check specifically for missing values, you can use this instead of ismember:

ismissing( Table_RandomInfoMiddle{:,1} ) 

这篇关于MATLAB:计算表列中的字符串出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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