我有一个单元格数组(2000 * 10),每个单元格都包含一个字符串,例如'25:20:55'
。
我想编写一个接受10个输入的函数(比如'25:02:33'
,'58:69:88'
,'25:54:96'
,'48:58:36'
,'58:54:88'
等等对应于该特定列的输入值的每列中的匹配(即,第一个输入数据对应于存储数据的第1列,第2列到第2列,等等。
如何为上述比较编写函数?
答案 0 :(得分:0)
假设单元格数组包含字符串,您可以使用strcmp
找到匹配项:
input = {'25:02:33', '58:69:88', '25:54:96', '48:58:36', '58:54:88'};
match_idx = strcmp(repmat(input, size(cell_data,1),1), cell_data);
如果你只是想知道相应列中是否有匹配而不关心匹配的行索引,你可以这样做
match = any(match_idx,1);
答案 1 :(得分:0)
使用ismember
ismember(your_var, your_cell);
e.g。
c = {'a', 'b', 'c', 'd'};
ismember('b', c)
ans =
1
ismember('q', c)
ans =
0
在你的情况下,类似的东西可以工作
function arr = myfun(inputvec, inputmat)
for i=1:length(inputvec) %// where inputvec is the cell-vector with the strings you want to search for
arr(i) = ismember(inputvec{i}, inputmat{:,i});
end