error using unique function
22 次查看(过去 30 天)
显示 更早的评论
s is a cell matrix (178000x9) I want to find the unique values of s in the first column. ID=unique(s(:,1));
I received the following error. Error using cell/unique (line 95) Input A must be a cell array of string
what could be the reason?
1 个评论
Sean de Wolski
2012-7-26
If the first column is all non-numeric it would not be throwing that error. What is returned from:
iscellstr(s(:,1))
采纳的回答
Mike Hosea
2012-7-26
编辑:Mike Hosea
2012-7-26
What type of data is in the cell array? When you use UNIQUE on a cell array, the only case that's supported is when the elements of the cell array are strings. Is that case not working for you?
>> c = {'abc','abc','def','defg'}';
>> unique(c(:,1))
ans =
'abc'
'def'
'defg'
7 个评论
Mike Hosea
2012-7-26
Yes, this is what we expected. You do not have a cell array of only strings. You can find the non-strings with
find(cellfun(@(x)~ischar(x),s))
You do not mention where this data came from. If it began as a
s = cell(m,n);
and was later partially populated, then you might resolve the problem by changing that to
s = repmat({''},m,n);
更多回答(1 个)
Wayne King
2012-7-26
编辑:Wayne King
2012-7-26
Is it a cell array of numeric values?
You can use cell2mat()
A = {1 5 9 ; 2 3 4; 2 4 5};
B = unique(cell2mat(A(:,1)));
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!