过滤Matlab表中的单词(与Excel中一样) [英] Filter on words in Matlab tables (as in Excel)

查看:159
本文介绍了过滤Matlab表中的单词(与Excel中一样)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Excel中,您可以使用过滤器"功能在您的列中查找某些单词.我想在Matlab的整个表格中做到这一点.

In Excel you can use the "filter" function to find certain words in your columns. I want to do this in Matlab over the entire table.

以Matlab示例表"Patients.dat"为例;我的第一个想法是使用:

Using the Matlab example-table "patients.dat" as example; my first idea was to use:

patients.Gender=={'Female'}

这不起作用.

strcmp(patients.Gender,{'Female'})

仅在一列(性别")中起作用.

workd only in one column ("Gender").

我的问题:我有一个表,表中的列以任意方式散布着不同的单词,例如"A","B",香蕉",苹果"....我只想要包含"A"和"B"的行.

My problem: I have a table with different words say 'A','B','bananas','apples',.... spread out in an arbitrary manner in the columns of the table. I only want the rows that contain, say, 'A' and 'B'.

奇怪的是我没有在matlab帮助"中找到它,因为它看起来很基础.我看着stackedO,但那里也没有找到答案.

It is strange I did not find this in matlab "help" because it seems basic. I looked in stackedO but did not find an answer there either.

推荐答案

Matlab中的table可以看作是扩展的cell array.例如,它还允许命名列.

A table in Matlab can be seen as an extended cell array. For example it additionally allows to name columns.

但是,在您的情况下,您希望搜索整个cell array,而不关心table的任何其他功能.因此,请使用table2cell进行转换.

However in your case you want to search in the whole cell array and do not care for any extra functionality of a table. Therefore convert it with table2cell.

然后您要搜索某些单词.您可以使用regexp,但在示例中提到strcmp也足够.两者都可以在cell arrays上立即工作.

Then you want to search for certain words. You could use a regexp but in the examples you mention strcmp also is sufficient. Both work right away on cell arrays.

最后,您只需要find逻辑搜索矩阵的行.

Finally you only need to find the rows of the logical search matrix.

以下示例从Matlab示例数据集中为您获取所有男性"且处于优秀"状况的患者行:

Here the example that gets you the rows of all patients that are 'Male' and in 'Excellent' conditions from the Matlab example data set:

patients = readtable('patients.dat');
patients_as_cellarray = table2cell(patients);
rows_male = any(strcmp(patients_as_cellarray, 'Male'), 2); % is 'Male' on any column for a specific row
rows_excellent = any(strcmp(patients_as_cellarray, 'Excellent'), 2); % is 'Excellent' on any column for a specific row
rows = rows_male & rows_excellent; % logical combination
patients(rows, :)

实际上只打印出状况良好的男性患者.

which indeed prints out only male patients in excellent condition.

这篇关于过滤Matlab表中的单词(与Excel中一样)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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