COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### removing NaN values from a matrix

• Email
• Follow

```I am importing data from text files using textread().
Many of the vectors I am defining have zeros in them. I changed all of these zeros to NaN values so they would not show up when I plot.

I am now trying to take the median values of many vectors, but my output says the median value=NaN because there are so many of those values.  The number of zero/NaN values differ for each vector, so I cannot simply take the first '#' amount of values from the vector.

Would there be a simple way of finding the median value of a vector while ignoring the NaN values. If not, would there be an easy way of ignoring the zero values while importing a text file with the textread() function?

I have tried using (%f*0) in my textread() function to define a vector, but then it will ignore all the zero values
(so a value like 0.30482 would be altered)

```
 0

See related articles to this posting

```"Christopher " <cpdavid87@gmail.com> wrote in message <iit3ph\$e4k\$1@fred.mathworks.com>...
> I am importing data from text files using textread().
> Many of the vectors I am defining have zeros in them. I changed all of these zeros to NaN values so they would not show up when I plot.
>
> I am now trying to take the median values of many vectors, but my output says the median value=NaN because there are so many of those values.  The number of zero/NaN values differ for each vector, so I cannot simply take the first '#' amount of values from the vector.
>
> Would there be a simple way of finding the median value of a vector while ignoring the NaN values. If not, would there be an easy way of ignoring the zero values while importing a text file with the textread() function?
>
> I have tried using (%f*0) in my textread() function to define a vector, but then it will ignore all the zero values
> (so a value like 0.30482 would be altered)
>

median(v(~isnan(v)))

James Tursa
```
 0

```hi
you can use the if function for this... let X{i} be your array

then put if X{i}>0 u can proceed ur median computation ... i think u ve to take ur input into arrays ...

kirthi
```
 0

```> median(v(~isnan(v)))
>
> James Tursa

Thank you for the help; that worked perfectly.  I also have a matrix that is 40 columns long and has lots of rows.

I am trying to find the median of each row.

I tried:
MedianFirstRow = median(BIGMATRIX(1,:)(~isnan(BIGMATRIX(1,:))))
I write a forloop for this, but it is giving me an indexing problem

but it gave the error:
()-indexing must appear last in an index expression.

Are there any quick ways to find the median of each row?
```
 0

```Christopher wrote:
....

> Are there any quick ways to find the median of each row?
....

doc median % especially optional DIM argument

--
```
 0

```On Feb 9, 1:10=A0pm, "Christopher " <cpdavi...@gmail.com> wrote:
> > median(v(~isnan(v)))
>
> > James Tursa
>
> Thank you for the help; that worked perfectly. =A0I also have a matrix th=
at is 40 columns long and has lots of rows.
>
> I am trying to find the median of each row.
>
> I tried:
> MedianFirstRow =3D median(BIGMATRIX(1,:)(~isnan(BIGMATRIX(1,:))))
> I write a forloop for this, but it is giving me an indexing problem
>
> but it gave the error:
> ()-indexing must appear last in an index expression.
>
> Are there any quick ways to find the median of each row?
> Thank you for your help

arrayfun(@(x) median(BIGMATRIX(~isnan(BIGMATRIX(:,x)),x)),
1:size(BIGMATRIX,2))

/reza
```
 0

```> > I am trying to find the median of each row.
> >
> > I tried:
> > MedianFirstRow = median(BIGMATRIX(1,:)(~isnan(BIGMATRIX(1,:))))
> > I write a forloop for this, but it is giving me an indexing problem
> >
> > but it gave the error:
> > ()-indexing must appear last in an index expression.
> >
> > Are there any quick ways to find the median of each row?
> > Thank you for your help
>
> arrayfun(@(x) median(BIGMATRIX(~isnan(BIGMATRIX(:,x)),x)),
> 1:size(BIGMATRIX,2))
>
> /reza

Hi Reza, Thanks for the help.

I tried the above code, but it is still taking the medians of each column for some reason and giving me the NaN value for each median.  My BIGMATRIX has 40 columns and many rows.  I tried switching the code to

arrayfun(@(x) median(BIGMATRIX(~isnan(BIGMATRIX(:,x)),x)), 1:size(BIGMATRIX,1))

with a 1 at the end (instead of a 2) so it would reference the number of rows, but it says, "Index exceeds matrix dimensions".

I could generate a new matrix saying

median1=median(vec1(1) vec2(1) vec3(1) vec4(1)...vecN(1));
median2=median(vec1(2) vec2(2) vec3(2) vec4(2)...vecN(2));
....
medianN=median(vec1(N) vec2(N) vec3(N) vec4(N)...vecN(N));

MedianALL=[median1 median2 ... medianN], but that would just take forever

Any suggestions on how to take the medians of the rows and not the columns?
```
 0

```Please try this:

% Data
A=rand(7,10);
A(rand(1,numel(A))>0.7)=NaN

% Engine
As = sort(A,1);
As(end+1,:) = NaN;
[~, last] = max(isnan(As),[],1);
mid = last/2;
[m n] = size(As);
i = (0:n-1)*m;
medianA = 0.5*(As(i + floor(mid))+As(i + ceil(mid)))

% Bruno
```
 0

```I found this great function called "nanmedian" and just wrote a forloop with it. Works Perfect!

for i=1:size(BIGMATRIX,1)
Median(i)=nanmedian(BIGMATRIX(i,:));
end

```
 0

```% work on row

As = sort(A,2);
As(:,end+1) = NaN;
[~, last] = max(isnan(As),[],2);
mid = last/2;
[m n] = size(As);
i = (1:m)';
i1 = i + m*(floor(mid)-1);
i2 = i + m*(ceil(mid)-1);
medianA = 0.5*(As(i1)+As(i2))

% Bruno
```
 0