Replacing elements of multilevel cell arrays
Afficher commentaires plus anciens
Hello, I have an multilevel cell arrays of meancalc. For example: To reach any of them, I have to write meancalc{1,1}{:,:}, meancalc{1,2}{:,:},meancalc{1,3}{:,:}. I want to reach the Inf values and change them with epsilon. But I couldn't reach them out with meancalc(meancalc{:,:}==Inf) = eps; Thanks in advance.
3 commentaires
Guillaume
le 10 Avr 2018
Why eps(1)? If it's just an arbitrary small value, eps(1) is not that small (compared to realmin)
Sukru Yavuz
le 10 Avr 2018
Well, you of course do whatever you want, but replacing an easily spotted erroneous value (maybe?) by an arbitrary constant value does not sound logical to me. Why eps(1) instead of exp(1) or sqrt(2) or eps(1e-10) or any other random arbitrary value that has no more significance than eps(1) to your algorithm?
Réponse acceptée
Plus de réponses (1)
Usage of cellfun might help:
idx=cellfun(@(x) isinf(x),meancalc)
This will output logical array. Then,
meancalc(idx)={234};%Inf's are replaced with a random value, change this with eps
8 commentaires
Sukru Yavuz
le 10 Avr 2018
Birdman
le 10 Avr 2018
Hmm, which version of MATLAB are you using?
No cellfun won't help because:
a) you want to do an assignment which is not supported by anonymous functions. You'd need a separate function to do the assignment, at which point you're better off using an explicit loop (in my opinion)
b) the content of the cell array is itself a cell array, so you'd need at least a double cellfun but... see a)
Birdman
le 10 Avr 2018
Actually this approach worked for me.
Oh, it says multiple cell arrays. Then,
idx=cellfun(@(x) isinf(x{:}),meancalc)
this one works.
Guillaume
le 10 Avr 2018
isinf(x{:})
will return Error using isinf. Too many input arguments if the cell array x contains more than one element. e,g.
x = {1 2 3}
isinf(x{:})
x{:} is expanded in the comma-separated list 1,2,3, so the above is equivalent to
isinf(1, 2, 3)
Let me share the code that I worked so that we can clear the confusion:
%demo data
A(:,:,1)=[1 2;3 Inf];
A(:,:,2)=[Inf 2;5 6];
A(:,:,3)=[3 Inf;Inf 3];
meancalc=num2cell(A);
% what I understood by multilevel cell was each element was contained as a cell
meancalc=num2cell(meancalc)
%this found the indexes of Inf's
idx=cellfun(@(x) isinf(x{:}),meancalc)
I don't think that's what Sukry has (and a cell array where each element is a scalar would be a big waste of memory). A representative example would be
A{1} = {[1 2; 3 Inf], [1 2 3]; [Inf; -1; NaN], [3 Inf; Inf 3]}
A{2} = {[], [Inf 2; 5 6], magic(3)}
That is you have matrices inside cell arrays themselves inside a cell array. This would be consistent with the notation A{i}{j}
Catégories
En savoir plus sur Numeric Types dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!