finding some specific numbers in a numeric cell array

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 comentarios

Adam
Adam el 13 de Sept. de 2016
Are you sure you don't have nested cell arrays rather than numeric arrays inside your outer cell array?
Please show small example of your array cells.

Iniciar sesión para comentar.

 Respuesta aceptada

dpb
dpb el 13 de Sept. de 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 comentarios

zahra zol
zahra zol el 13 de Sept. de 2016
hie. thanks for answering but i hardly understand. my cell array has 332 rows 6 columns. for example, row 9th of my array is like this: [9;10;12] [9;10;12] [10;12] [7;9;10;11;12;13] [9;10;12;13] [9;10;12] others row are more or less like this one. containing numbers between 7 to 13. all i want is to determine,how many of each of these numbers exist in the whole array. and in which locations(row and column of the occurrence of each number)
dpb
dpb el 13 de Sept. de 2016
Editada: dpb el 13 de Sept. de 2016
And why doesn't the solution shown provide precisely that?
If you're still having trouble, we need to see the details of what
whos new_data
shows so we can tell what the actual structure looks like altho it would seem as I demonstrated it's the latter of a cell array within a cell given the illustration of how the specific error you received can be generated. That's not the only way, certainly, but it's about the easiest to have created (perhaps inadvertently); showing how you created the data in the first place could lead to folks here being able to show simplifications in that as well.
zahra zol
zahra zol el 14 de Sept. de 2016
this is the mat file of my "new_data". forget about the last three columns. I am talking about the first six ones.
Adam
Adam el 14 de Sept. de 2016
Editada: Adam el 14 de Sept. de 2016
Your command works fine for me on your data if you remove the last 3 columns, though obviously not if you just try to run it on the whole matrix.
i.e.
cellfun(@(x) find(x==7), new_data(:,1:6), 'UniformOutput', false)
zahra zol
zahra zol el 14 de Sept. de 2016
Editada: zahra zol el 14 de Sept. de 2016
it is really odd! i had written that line of code exactly like what u just said(columns 1 to 6 specified) before. and i swear it would get back that error! and i ran it again right now and it was ok! magic! maybe it is the power of your words! thanks any way. really thanks :))
zahra zol
zahra zol el 14 de Sept. de 2016
sorry but there seems to be another problem. for example when i am looking for number 13, the result matrix in row 15 has 6, 5s! that is odd because in my original matrix, row 5 has got 6 double arrays each of which contain one 13. isnt that supposed to get me 6 ones in row 15 as the result of cellfun?? and the same problem occures in other rows as well. am i doing sth wrong ?
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 el 14 de Sept. de 2016
yea that is exactly how is should be. thank you very much for your help.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 13 de Sept. de 2016

Comentada:

el 14 de Sept. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by