做到這一點(所有版本兼容)最簡單的方法是隻用strcmp
,它可以接受電池陣列,並做了「字符串比較」。
一個襯墊
indexresult = find(strcmp(C(:,1), 'name') & strcmp(C(:,3), '23'));
% indexresult = [1; 4; 6];
說明
% Get logical array of rows where first column is 'name'
logicalname = strcmp(C(:,1), 'name');
% Get logical array of rows where third column is '23'
logical23 = strcmp(C(:,3), '23');
% Get logical array where both of the above are true, using and (&)
logicalname23 = strcmp(C(:,1), 'name') & strcmp(C(:,3), '23');
% Get indices from logical array using find
indexresult = find(strcmp(C(:,1), 'name') & strcmp(C(:,3), '23'));
% If you wanted a row vector instead of column vector, just transpose too
indexresult = find(strcmp(C(:,1), 'name') & strcmp(C(:,3), '23')).';
如果你想不區分大小寫(匹配'name', 'NAME', 'Name', ...
),然後使用strcmpi
代替strcmp
。
請注意,'strfind'也會匹配任何包含*''name''的字符串,例如''name1234'' – Wolfie