Remove NaN from the cell array without changing the row and column of matrix

17 ビュー (過去 30 日間)
Pichawut Manopkawee
Pichawut Manopkawee 2016 年 9 月 21 日
編集済み: Walter Roberson 2016 年 9 月 26 日
Hi all,
I have a question related to how to remove NaN in cell array without changing any rows and column of matrix.
for example,
A = [ NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN,
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN,
NaN NaN NaN NaN 25 NaN NaN NaN NaN NaN,
NaN NaN 23 24 25 27 NaN NaN NaN NaN,
NaN 22 30 35 23 23 23 23 NaN NaN,
24 23 33 23 23 19 20 20 20 21,
NaN NaN 23 21 28 22 NaN NaN NaN NaN,
NaN NaN NaN NaN 25 NaN NaN NaN NaN NaN,
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]
I would like to get rid of NaN but keeping the same character of matrix, such as
A = [ _ _ _ _ _ _ _ _ _ _ ,
_ _ _ _ _ _ _ _ _ _,
_ _ _ _ 25 _ _ _ _ _,
_ _ 23 24 25 27 _ _ _ _,
_ 22 30 35 23 23 23 23 _ _,
24 23 33 23 23 19 20 20 20 21,
_ _ 23 21 28 22 _ _ _ _,
_ _ _ _ 25 _ _ _ _,
_ _ _ _ _ _ _ _ _ _ ]
* _ means no any valu, even 0
How could I do that, Thanks very much in advance.
  4 件のコメント
Pichawut Manopkawee
Pichawut Manopkawee 2016 年 9 月 21 日
i'm sorry if I make you get confused between cell array and matrix. A is a matrix containing cell array as I show you above. Actually, the NaN is from a tiff file exported from ArcMap. I think it is a black zone without any elevation value of DEM. It makes the calculation weird even I make all NaN as zero value.
As James told me, all spots should fill a value. They still have value eventually.
Thank you so much, and I will find another way to cope with this.
James Tursa
James Tursa 2016 年 9 月 21 日
What does this return:
class(A)

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

回答 (1 件)

Michelle Wu
Michelle Wu 2016 年 9 月 26 日
It is my understanding that you are trying to remove the NaNs from a cell array since they would somehow affect the calculations you are trying to perform. However, the code snippet you provided in your initial description contradicts with your comments of A being a matrix containing cell arrays. I will assume A to be a 9-by-10 cell array according to your example, and hopefully my answer still applies to whatever kind of cell array you are actually using.
Create the 9-by-10 cell array A from an array arr:
>> arr = [NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN;
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN;
NaN NaN NaN NaN 25 NaN NaN NaN NaN NaN;
NaN NaN 23 24 25 27 NaN NaN NaN NaN;
NaN 22 30 35 23 23 23 23 NaN NaN;
24 23 33 23 23 19 20 20 20 21;
NaN NaN 23 21 28 22 NaN NaN NaN NaN;
NaN NaN NaN NaN 25 NaN NaN NaN NaN NaN;
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN];
>> A = num2cell(arr);
The cell array A would look like:
A =
[NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN]
[NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN]
[NaN] [NaN] [NaN] [NaN] [ 25] [NaN] [NaN] [NaN] [NaN] [NaN]
[NaN] [NaN] [ 23] [ 24] [ 25] [ 27] [NaN] [NaN] [NaN] [NaN]
[NaN] [ 22] [ 30] [ 35] [ 23] [ 23] [ 23] [ 23] [NaN] [NaN]
[ 24] [ 23] [ 33] [ 23] [ 23] [ 19] [ 20] [ 20] [ 20] [ 21]
[NaN] [NaN] [ 23] [ 21] [ 28] [ 22] [NaN] [NaN] [NaN] [NaN]
[NaN] [NaN] [NaN] [NaN] [ 25] [NaN] [NaN] [NaN] [NaN] [NaN]
[NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN] [NaN]
Find the NaNs in A, and replace them with []:
>> A(cellfun(@isnan,A)) = {[]};
Now A becomes:
A =
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [25] [] [] [] [] []
[] [] [23] [24] [25] [27] [] [] [] []
[] [22] [30] [35] [23] [23] [23] [23] [] []
[24] [23] [33] [23] [23] [19] [20] [20] [20] [21]
[] [] [23] [21] [28] [22] [] [] [] []
[] [] [] [] [25] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
To learn more about the usage of “cellfun”, refer to the documentation page.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by