Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

remove NAN from a matrix and truncate it

330 views
Skip to first unread message

MOHAMMAD YASIR ALI

unread,
Mar 18, 2017, 2:14:11 PM3/18/17
to
I have a matrix of size 701*1251 there are several Nan values, I want to take average of this matrix, my question is
1) to remove all the cells that have Nan values, changing them to zero would not change the size of matrix hence a wrong result
2) how do we take mean of a 2-D matrix

dpb

unread,
Mar 18, 2017, 4:00:30 PM3/18/17
to
nanmean(x(:)) % using builtin

mean(x(isfinite(x(:)))) % what nanmean basically is/does

NB: the {:} to address all elements of an N-D array as a (column) vector.

I recommend reading/working thru the tutorials in "Getting Started" if
any of this is new to you.

--

Loren Shure

unread,
Mar 20, 2017, 4:17:31 AM3/20/17
to
"MOHAMMAD YASIR ALI" wrote in message
news:oajtdd$2lg$1...@newscl01ah.mathworks.com...
Use the mean functions with the omitnan flag:

A = [1 0 0 1 NaN 1 NaN 0];
M = mean(A,'omitnan')

--Loren

http://blogs.mathworks.com/loren

Saravanan Mani

unread,
Mar 20, 2017, 5:41:12 AM3/20/17
to
Dear Loren,

Following error message received.

>> A = [1 0 0 1 NaN 1 NaN 0]

A =

1 0 0 1 NaN 1 NaN 0

>> M = mean(A,'omitnan')
??? Error using ==> sum
Trailing string input must be 'double' or 'native'.

Error in ==> mean at 30
y = sum(x,dim)/size(x,dim);

Thanks,
Saravanan


"Loren Shure" wrote in message <oao36m$n1k$1...@newscl01ah.mathworks.com>...

Saravanan Mani

unread,
Mar 20, 2017, 5:53:07 AM3/20/17
to
Dear Mohammad,

Can you try this,....

% Method #1
% Just remove the NAN index, size of the matrix is varied.
A = [1 0 0 1 NaN 1 NaN 0];
B = [];
for k = 1:length(A)
if ~isnan(A(k))
B = [B ; A(k)];
else
end
end

% Method #2
% Replacing NAN into Zero, The same size of matrix.
C = [1 0 0 1 NaN 1 NaN 0];
D = [];
for k1 = 1:length(C)
if ~isnan(C(k1))
D = [D ; C(k1)];
else
D = [D ; 0];
end
end

Thanks & Regards,
Saravanan


"MOHAMMAD YASIR ALI" wrote in message <oajtdd$2lg$1...@newscl01ah.mathworks.com>...

Saravanan Mani

unread,
Mar 20, 2017, 5:54:06 AM3/20/17
to

bartekltg

unread,
Mar 20, 2017, 6:43:02 AM3/20/17
to
On 20.03.2017 10:53, Saravanan Mani wrote:
> Dear Mohammad,
>
> Can you try this,....

No! Not in matlab.
This is the worse possible way ;-)
A loop for scalars is very slow, using it when
it is not absolutely necessary is a bad practise.


> % Method #1
> % Just remove the NAN index, size of the matrix is varied.
> A = [1 0 0 1 NaN 1 NaN 0];
> B = [];
> for k = 1:length(A)
> if ~isnan(A(k))
> B = [B ; A(k)];
> else
> end
> end

B = A(~isnan(A));

> % Method #2
> % Replacing NAN into Zero, The same size of matrix.
> C = [1 0 0 1 NaN 1 NaN 0];
> D = [];
> for k1 = 1:length(C)
> if ~isnan(C(k1))
> D = [D ; C(k1)];
> else
> D = [D ; 0];
> end
> end

B=A;
B(isnan(B))=0;

bartekltg

dpb

unread,
Mar 20, 2017, 1:57:05 PM3/20/17
to
On 03/20/2017 4:41 AM, Saravanan Mani wrote:
...

> Following error message received.
...

>>> M = mean(A,'omitnan')
> ??? Error using ==> sum
> Trailing string input must be 'double' or 'native'.
>
> Error in ==> mean at 30
...

Introduced at some point after R2014b; not sure when; online doc for
latest release doesn't have the dates at which additional options were
introduced, unfortunately.

See my initial response for the "Matlab classic" solution.

|nanmean| requires Statistics Toolbox, the other solution works for any
release.

--

Loren Shure

unread,
Mar 21, 2017, 4:48:07 AM3/21/17
to
"dpb" wrote in message news:oap4vs$2uf$1...@dont-email.me...
You are running an older release of MATLAB. Update?
--


--Loren

http://blogs.mathworks.com/loren

dpb

unread,
Mar 21, 2017, 10:20:50 AM3/21/17
to
On 03/21/2017 3:48 AM, Loren Shure wrote:
...

> You are running an older release of MATLAB. Update?

Lastest 32-bit version afaik _is_ R2014b?

--


Bruno Luong

unread,
Mar 21, 2017, 11:56:08 AM3/21/17
to
"dpb" wrote in message <oarcme$m3v$2...@dont-email.me>...
R2015b

Steven Lord

unread,
Mar 21, 2017, 12:59:15 PM3/21/17
to
"Bruno Luong" wrote in message <oariei$qlj$1...@newscl01ah.mathworks.com>...
According to the Release Notes, the 'omitnan' option for MEAN was introduced in release R2015a so it is possible to have a 32-bit release of MATLAB with a version of MEAN that recognizes the 'omitnan' option.

https://www.mathworks.com/help/matlab/release-notes.html?rntext=nan+mean&startrelease=R2015a&endrelease=R2015a&groupby=release&sortby=descending&searchHighlight=nan+mean

--
Steve Lord
sl...@mathworks.com
To contact Technical Support, use the Contact Us link at the top of http://www.mathworks.com

dpb

unread,
Mar 21, 2017, 3:21:45 PM3/21/17
to
On 03/21/2017 11:59 AM, Steven Lord wrote:
> "Bruno Luong" wrote in message <oariei$qlj$1...@newscl01ah.mathworks.com>...
>> "dpb" wrote in message <oarcme$m3v$2...@dont-email.me>...
>> > On 03/21/2017 3:48 AM, Loren Shure wrote:
>> > ...
>> > > > You are running an older release of MATLAB. Update?
>> > > Lastest 32-bit version afaik _is_ R2014b?
>>
>> R2015b
>
> According to the Release Notes, the 'omitnan' option for MEAN was
> introduced in release R2015a so it is possible to have a 32-bit release
> of MATLAB with a version of MEAN that recognizes the 'omitnan' option.
...

Hmmm...not much call for that on its own but there are some enhancements
to the _table_ that would be nice to be able to play with...I must've
misread; I surely thought I'd seen the plug was pulled on 32-bit after 2014.

That'll be another day's project...at some point I will update this old
machine, but having given up the consulting gig it's still all need with
no financial incentive to spend $$ don't need w/ wheat/corn prices in
the tank and no rain plus wild fires...

--
0 new messages