Replacing elements of multilevel cell arrays

6 ビュー (過去 30 日間)
Sukru Yavuz
Sukru Yavuz 2018 年 4 月 10 日
編集済み: Guillaume 2018 年 4 月 10 日
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 件のコメント
Sukru Yavuz
Sukru Yavuz 2018 年 4 月 10 日
I am calculating the standard deviation of columns. And the values of some columns are identical. That's why the results are 0. After that step, I am using those zeros in a multiplication. That's why I am getting the Inf results. My advisor told me to replace them with epsilon to avoid Inf or NaN or 0s.
Guillaume
Guillaume 2018 年 4 月 10 日
編集済み: Guillaume 2018 年 4 月 10 日
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?

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2018 年 4 月 10 日
Assuming that all meancalc{t,u}{v,w} contain matrices, you'll have to use a double for loop:
for outer = 1:numel(meancalc)
for inner = 1:numel(meancalc{outer})
innermat = meancalc{outer}{inner};
innermat(isinf(innermat)) = eps(1);
meancalc{outer}{inner} = innermat;
end
end
Note: acellarray{:} will return a comma separated list, so your meancalc{:,:}==Inf would never work.
  1 件のコメント
Sukru Yavuz
Sukru Yavuz 2018 年 4 月 10 日
It works perfectly. Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Birdman
Birdman 2018 年 4 月 10 日
編集済み: Birdman 2018 年 4 月 10 日
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 件のコメント
Birdman
Birdman 2018 年 4 月 10 日
編集済み: Birdman 2018 年 4 月 10 日
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)
Guillaume
Guillaume 2018 年 4 月 10 日
編集済み: Guillaume 2018 年 4 月 10 日
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}

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by