finding some specific numbers in a numeric cell array

9 views (last 30 days)
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 Comments
Adam
Adam on 13 Sep 2016
Are you sure you don't have nested cell arrays rather than numeric arrays inside your outer cell array?

Sign in to comment.

Accepted Answer

dpb
dpb on 13 Sep 2016
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 Comments
Adam
Adam on 14 Sep 2016
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.
zahra zol
zahra zol on 14 Sep 2016
yea that is exactly how is should be. thank you very much for your help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!