finding some specific numbers in a numeric cell array
9 ビュー (過去 30 日間)
古いコメントを表示
I have a cell array that its cells contain numeric arrays of different sizes. each numeric array contains various number of numbers ranging from 7 to 13. i want to find the number and location of occurrences of all the 7s,8s,9s ...and 13s in the whole cell array. i used the following function:
I = cellfun(@(x) find(x==7), new_data, 'UniformOutput', false);
for finding 7 for example. new_data is my array. i encounter this error after running it:
Undefined function or method 'eq'for input arguments of type 'cell'.
can any body help me in this regard?
2 件のコメント
Adam
2016 年 9 月 13 日
Are you sure you don't have nested cell arrays rather than numeric arrays inside your outer cell array?
採用された回答
dpb
2016 年 9 月 13 日
Looks like you've nested the cell array; either create an array of cells with the double arrays each per cell or dereference inside your anonymous function--example:
>> dat(1)={randi([7 13],10,1)}; % make sample cell array
>> dat(2,1)={randi([7 13],8,1)}; % two cells w/ a double array in each
>> whos dat
Name Size Bytes Class Attributes
dat 2x1 264 cell
>> dat
dat =
[10x1 double]
[ 8x1 double]
>> cellfun(@(x) find(x==7), dat, 'UniformOutput', false)
ans =
[3x1 double]
[ 5]
>> ans{:}
ans =
7
9
10
ans =
5
>> data={dat} % now nest that in a single cell...
data =
{2x1 cell}
>> cellfun(@(x) find(x==7), data, 'UniformOutput', false)
Undefined function 'eq' for input arguments of type 'cell'.
Error in @(x)find(x==7)
>>
Look familiar??? :)
>> cellfun(@(x) find(x==7), data{:}, 'UniformOutput', false)
ans =
[3x1 double]
[ 5]
>> whos data
Name Size Bytes Class Attributes
data 1x1 324 cell
>>
NB: The curlies "{:}" to dereference the content of the cell in the latter...
8 件のコメント
Adam
2016 年 9 月 14 日
find returns the index of the found item so 13 is at index 5 in each of those arrays.
If you just want to know that the value exists and not where it is then use:
cellfun(@(x) ~isempty( find(x==13) ), new_data(:,1:6));
You also don't need the UniformOutput as false in that case as the output will be a logical array.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!