Find and replace values in a cell array containing 3-D matrixes with numeric values

Hi, I have a cell array with 6 cells, each cell containing a 3-D matrix of 640x480x30. I'm trying to find all zeros (0) in the cell array and replace them with NaNs.
In the general case, I have a cell array (MyCellArray) with -K- cells, each cell containing an arbitrary vector or matrix in an arbitrary size, with numeric values. I'm trying to find all places where MyCellArray contains the value of X (some number) and replace it with Y (some number).
Is there a way to do it without a for loop? Something like (that of course can't work) MyCellArray{MyCellArray==0}=NaN
Thanks!

 採用された回答

Akira Agata
Akira Agata 2018 年 4 月 20 日

Another possible solution:

YourCellArray = cellfun(@replaceZeroWithNan,YourCellArray,'UniformOutput',false);
function D = replaceZeroWithNan(D)
idx = D == 0;
D(idx) = nan;
end

3 件のコメント

Guillaume
Guillaume 2018 年 4 月 20 日

Yes, this is the solution I meant by: or cellfun, but in this case, you'd have to use a .m function not an anonymous function

Avishay Assayag
Avishay Assayag 2018 年 4 月 20 日
Thanks for your answer! That's an excellent solution for the general case. Kudos!
Guillaume
Guillaume 2018 年 4 月 20 日
編集済み: Guillaume 2018 年 4 月 20 日

Note that, while more concise, cellfun is generally slower than an explicit loop.

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

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 4 月 20 日
編集済み: Guillaume 2018 年 4 月 20 日

In the generic case, it is not possible to do it without a loop (or cellfun, but in this case, you'd have to use a .m function not an anonymous function:

for idx = 1:numel(yourcellarray)
   m = yourcellarray{idx};
   m(m == 0) = NaN;
   yourcellarray{idx} = m;
end

In your example case, where all the matrices are the same size, then you'd be better off not using a cell array but a 4-D matrix. The replacement is then trivial:

m = cat(4, yourcellarray{:});
m(m == 0) = NaN;

5 件のコメント

Avishay Assayag
Avishay Assayag 2018 年 4 月 20 日
The first line of your code do nothing to my cell array.

I'm not sure what you're referring to with "first line of code". The general solution does what you asked:

>> yourcellarray = {0:5, -1:1, eye(3)};
>> celldisp(yourcellarray)
yourcellarray{1} =
     0     1     2     3     4     5
yourcellarray{2} =
    -1     0     1
yourcellarray{3} =
     1     0     0
     0     1     0
     0     0     1

We have a cell array with 3 matrices of different size, with some 0s in them. Let's try the code:

>> for idx = 1:numel(yourcellarray)
   m = yourcellarray{idx};
   m(m == 0) = NaN;
   yourcellarray{idx} = m;
end
>> celldisp(yourcellarray)
yourcellarray{1} =
   NaN     1     2     3     4     5
yourcellarray{2} =
    -1   NaN     1
yourcellarray{3} =
     1   NaN   NaN
   NaN     1   NaN
   NaN   NaN     1

All the zeros have been changed to NaN.

Avishay Assayag
Avishay Assayag 2018 年 4 月 20 日
Sorry for being unclear. I meant to the example with m = cat(4, yourcellarray) which didn't work for me.
m = cat(4, yourcellarray{:});
Guillaume
Guillaume 2018 年 4 月 20 日
Thanks, Stephen. Silly but crucial typo, now fixed in the original post.

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by