Find indices in cell array

Hello everybody,
I have some problems to get the indices of a certain value, in this case -1, within a cell array. The structure of the cell array is like this: A is a 27x1 cell where each of the 27 rows has 500000 cell entries (numeric values between -1 and 999). What I want are the indices where the value -1 occurs, since I have to replace them afterwards. For example A{1,1}{4,1} or A{24,1}{95423,1} are containing the value -1. First of all I tried index=cellfun(@(x) x==-1, A{1,1}, 'Uniform', false) and then I used index2=cellfun(@(x) x==-1, A{2,1}, 'Uniform', false) and so on (. So I could get the information separately but I think there should exist a solution where I don't have to define 27 index cell arrays. Hopefully someone can help and recommend a more meaningful solution. I used the search function but none of the solutions in similar topics did work for my case (using cellfun(@(x) find(x==-1), A{1,1}, 'UniformOutput',false) leads to the same approach like I did it already for example).
Thanks in advance :)

Respuestas (3)

Andrei Bobrov
Andrei Bobrov el 25 de Oct. de 2017

0 votos

Aw = cellfun(@(x)[x{:}]',A,'un',0);
Aw = [Aw{:}];
[ii,jj] = find(Aw == -1) ;
out = [jj,ii];

7 comentarios

Timo
Timo el 25 de Oct. de 2017
Thanks a lot for your suggestion! For my test array it works really good, but it seems that something within my original cell array must be wrong, since I always get the error: Undefined operator '==' for input arguments of type 'cell'.
Andrei Bobrov
Andrei Bobrov el 25 de Oct. de 2017
Hm. Please attach small part your data here as mat-file.
Timo
Timo el 26 de Oct. de 2017
Hello Andrei, do you mean my exact matlab code or do you mean the data that I was reading into matlab and which I try to process actually?
Greetings Timo
Stephen23
Stephen23 el 26 de Oct. de 2017
@Timo Schaffhauser: the data. Exactly as Andrei Bobrov requested.
Timo
Timo el 27 de Oct. de 2017
That's the way I have the data, each file consists of several observation station which contain dates (hourly data) and corresponding observed values. The reason why I used cell arrays ( I guess that it was totally silly and a bad idea maybe) was that I wanted to separate all the observed values (of each station) and assign them to a reference time series where I fill all missing values with -99 and also replace all -1 values (they indicate measurement error) with -99. Probably I should restart the whole code by using conventional array?
Thank you for the effort :).
Stephen23
Stephen23 el 27 de Oct. de 2017
Editada: Stephen23 el 27 de Oct. de 2017
@Timo: always store data in the simplest array-class possible. It is up to you to decide where that boundary is. A good rule of thumb is to start with simple numeric arrays, and only change to a more complex data class when it proves necessary to do so.
Timo
Timo el 27 de Oct. de 2017
Yes, you are absolutely right, I thought it is more clearly arranged, when a file for example consists of 100 stations and 100 time series since I need to write a text file for each of them. Hopefully it works soon :). Thank you!

Iniciar sesión para comentar.

Jos (10584)
Jos (10584) el 25 de Oct. de 2017

0 votos

A = {[-1 0 0 -1],1:4,[1 -1 1 -1 1 -1]} % example array
fhFind = @(x) find(x == -1) ;
IDX = cellfun(fhFind ,A,'un',0)
Jan
Jan el 25 de Oct. de 2017
Editada: Jan el 25 de Oct. de 2017

0 votos

After the data are converted to a numerical array, the processing is very efficient. Consider, if it would be better to store the data as numerical array directly instead of the complicated nested cell.
If you really need the cells, and want to replace the -1 only, it might be better to avoid the conversion to one huge numerical array:
for iA = 1:numel(A)
B = A{iA};
for iB = 1:numel(B)
C = B{iB};
C(C == -1) = 42;
B{iB} = C;
end
end
This is not nice, but it is worth to try if it is faster.

7 comentarios

Timo
Timo el 25 de Oct. de 2017
Editada: Timo el 25 de Oct. de 2017
Thank you all for the fast responses! When I try it with a self-made example nested cell array the code works pretty fine. After that I tried to apply it for my above mentioned case but there I always get the error "Undefined operator '==' for input arguments of type 'cell'.". I am not really practiced with Matlab, I guess this error occurs when some of the cells within the cell array are maybe strings or something like that?
I am glad for every help I can get :)
Jan
Jan el 26 de Oct. de 2017
@Timo: This means, that your data differs from the expected content. Check the types of the inputs:
cellfun('isclass', A, 'cell')
Timo
Timo el 26 de Oct. de 2017
Hello and thanks a lot again, I tried your code and it returns a 1 for each field and even when I try the code for a certain cell like A{23,1} it returns me 500.000 values of 1, so it seems that my cell array should have the right data type, or am I wrong?
Jan
Jan el 27 de Oct. de 2017
This is very strange, because my code replaces -1 by 42. I do not know, what you are doing exactly. Please post the code and a small example for the input.
Timo
Timo el 27 de Oct. de 2017
Yes, now it works that the above mentioned error disappeared but it still doesn't replace the -1 values. I attached the input data and my code. The code contains some explanations what the intention was by using the several commands but I think it is really confusing and chaotic since I am a beginner with matlab. If you have some questions I can write you what my final intention is and maybe I really should use normal arrays then it would be easier to process data (f.e. create the sum of several hours).
Jan
Jan el 28 de Oct. de 2017
@Tima: You have set all values of the output to {-99} in this line already:
union{i,1}(:,2)={-99};
and later crop of all other information:
union2{i,1}=union{i,1}(:,2);
Therefore it is not clear to me, what you want to achieve.
Timo
Timo el 28 de Oct. de 2017
I wanted to give all dates of the reference time series that were not observed in the certain station a value of -99. Afterwards I wanted to give the dates which contain measurements the corresponding values. And in a third step I wanted to replace the dates that contain a value of -1 also with a value of -99. Now I was trying to follow your recommendation to work with normal arrays, what really seems to simplify everything a lot until now :)!

Iniciar sesión para comentar.

Categorías

Preguntada:

el 25 de Oct. de 2017

Comentada:

el 28 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by