フィルターのクリア

How to remove Nans from matrix

1 回表示 (過去 30 日間)
Ian
Ian 2013 年 9 月 10 日
I have a matrix (size 123317x6), and every 128 rows I have 3 nans on the last three columns of that thoses rows. Now what I want to do is remove all the NaNs completely without removing that row or column (just the NanNs) The data on the next row then needs to be moved up to where the Nans were. I will give you a quick example.
so I want this matrix:-
M=
1 3 6 nan nan nan 4 5 4 1 3 4 1 2 2 6 3 1 1 3 4 nan nan nan 3 4 5 6 7 8
M=
1 3 6 4 5 4 1 3 4 1 2 2 6 3 1 1 3 4 3 4 5 6 7 8
Hope this example explains what I want better!
Any help would be greatly appreicated.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 10 日
編集済み: Azzi Abdelmalek 2013 年 9 月 10 日
out=reshape(M(~isnan(M)),[],size(M,2))
Edit
M can not be reshaped unless we add some nan at the end of M
%---------Your array-------------------
M=rand(123317,6);
M(128:128:n,end-2:end)=nan;
%-----------------------------------------
n=size(M,1);
M=M(~isnan(M));
M(end:end+mod(6-mod(numel(M),6),6))=nan;
M=reshape(M,[],6);
  1 件のコメント
Geert
Geert 2013 年 9 月 10 日
I like your answer more than mine. There is however a small mistake, it should be:
out=reshape(M(~isnan(M)),[],size(M,2)-3)

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

その他の回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2013 年 9 月 10 日
編集済み: Andrei Bobrov 2013 年 9 月 10 日
out = resM(M==M)
ADD
M1 = M';
out = reshape(M1(M1==M1),size(M,2),[])';

Geert
Geert 2013 年 9 月 10 日
If it are exactly 3 NaN's per row, you could do something like this:
M = [1 3 6 nan nan nan 4 5 4 1 3 4 1 2 2;
6 3 1 1 3 4 nan nan nan 3 4 5 6 7 8];
M_new = zeros(size(M,1), size(M,2)-3);
for ii = 1:size(M,1)
M_new(ii,:) = M(ii,~isnan( M(ii,:)));
end

Ian
Ian 2013 年 9 月 15 日
This for you help guys much appreciated! :)

カテゴリ

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