Matlab - Why ismember does not work? [closed]

社会主义新天地 提交于 2019-12-13 11:27:37

问题


I have a cell vector with different & redundant company names. And a list of interesting companies. I try to figure out, where in the big matrix the interesting companies appear. I am interested in all appearances. My Code for some reason does not work. the ismember returns always an error.

Comps = Data0(:,7);
for i = 1: length(relCompQ)
    comp = relCompQ{i,1};
    c(:,i) = find(ismember(Comps,comp));
end

errors:

Error using cell/ismember>cellismemberR2012a (line 192) Input A of class cell and input B of class char must be cell arrays of strings, unless one is a string.

Error in cell/ismember (line 56)

[varargout{1:max(1,nargout)}] = cellismemberR2012a(A,B);

Both Vectors have values like: 'Nike', 'Adidas', 'BMW' etc.


回答1:


Input A of class cell and input B of class char must be cell arrays of strings, unless one is a string.

This means that every entry in the cell must be a string, and in this case it's not. If your cell array contains numeric values, or even an empty matrix, you will get this error.

For example, this is fine (cell array of strings, and a char)

ismember({'b','c'},'b');

This is not:

ismember({'b',[1 2]},'b');
ismember({'b',[]},'b');
ismember({'b',NaN},'b');

The most likely one to get mixed into your cell array accidentally is probably the empty matrix. In these cases I always recommend using the debugging tools (dbstop if error is our friend), to doublecheck exactly what is going on. What you think the variable should be or contain, and what it does in fact contain, are not always the same thing.

If it does contain empties, see this question for ideas on how to deal with it.



来源:https://stackoverflow.com/questions/27227291/matlab-why-ismember-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!