Thread Subject: Error using ==> nanmean Too many input arguments.

Subject: Error using ==> nanmean Too many input arguments.

From: Giusep

Date: 26 Jan, 2012 15:17:10

Message: 1 of 9

Hello,

I'm pretty new to matlab and have read the help manual for nanmean, inspected the variables in question, and made sure there are no other files named 'nanmean' (from other "too many arguments" posts it seems the wrong path is listed or there is something else using the same name), but I continue to get the error:

Error using ==> nanmean
Too many input arguments.

Error in ==> monthlyaverage at 18
   xm(i) = nanmean(precip{mo==i});

The code in question is:

[yr,mo] = datevec(newdate, 'dd-mm-yyyy'); % d must be array of strings,
xm = zeros(12,1);
for i = 1:12
   xm(i) = nanmean(precip{mo==i}); %Logical indexing of months
end

precip is an array of <1x14375 cell> int32 elements
>> class(precip)
ans =
cell

>> class(precip{1})
ans =
int32

The majority of them are empty
>> precip{1}
ans =
   Empty matrix: 0-by-1

I appear to be using only the appropriate nanmean and do not find other files named nanmean in the path.
>> which nanmean
C:\Program Files\MATLAB\R2008b\toolbox\stats\nanmean.m

Checking these things seemed to be a common procedure in other threads with 'too many input arguments', but I'm not sure where to go from here.

Thanks in advance for any help!

Subject: Error using ==> nanmean Too many input arguments.

From: Steven_Lord

Date: 26 Jan, 2012 18:26:25

Message: 2 of 9



"Giusep " <lyngly@ymail.com> wrote in message
news:jfrqpm$64g$1@newscl01ah.mathworks.com...
> Hello,
>
> I'm pretty new to matlab and have read the help manual for nanmean,
> inspected the variables in question, and made sure there are no other
> files named 'nanmean' (from other "too many arguments" posts it seems the
> wrong path is listed or there is something else using the same name), but
> I continue to get the error:
>
> Error using ==> nanmean
> Too many input arguments.
>
> Error in ==> monthlyaverage at 18
> xm(i) = nanmean(precip{mo==i});
>
> The code in question is:
>
> [yr,mo] = datevec(newdate, 'dd-mm-yyyy'); % d must be array of strings, xm
> = zeros(12,1); for i = 1:12
> xm(i) = nanmean(precip{mo==i}); %Logical indexing of months
> end
>
> precip is an array of <1x14375 cell> int32 elements

When you index into a cell array using {} and logical indexing, what you get
back is a comma-separated list of length equal to the number of true values
in the index.

A = {1, 2, 3};
A{[true, false, true]}

You should see two ans values displayed; that second line of code is
essentially the same as typing:

1, 3

So you ARE calling NANMEAN with too many inputs.

What you want to do is concatenate all those int32's together or make precip
a regular int32 array (NOT a cell array) from the start, something like:

t = [A{[true, false, true]}]

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Error using ==> nanmean Too many input arguments.

From: Giusep

Date: 27 Jan, 2012 04:00:10

Message: 3 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message <jfs5sh$eue$1@newscl01ah.mathworks.com>...
>
>
> "Giusep " <lyngly@ymail.com> wrote in message
> news:jfrqpm$64g$1@newscl01ah.mathworks.com...
> > Hello,
> >
> > I'm pretty new to matlab and have read the help manual for nanmean,
> > inspected the variables in question, and made sure there are no other
> > files named 'nanmean' (from other "too many arguments" posts it seems the
> > wrong path is listed or there is something else using the same name), but
> > I continue to get the error:
> >
> > Error using ==> nanmean
> > Too many input arguments.
> >
> > Error in ==> monthlyaverage at 18
> > xm(i) = nanmean(precip{mo==i});
> >
> > The code in question is:
> >
> > [yr,mo] = datevec(newdate, 'dd-mm-yyyy'); % d must be array of strings, xm
> > = zeros(12,1); for i = 1:12
> > xm(i) = nanmean(precip{mo==i}); %Logical indexing of months
> > end
> >
> > precip is an array of <1x14375 cell> int32 elements
>
> When you index into a cell array using {} and logical indexing, what you get
> back is a comma-separated list of length equal to the number of true values
> in the index.
>
> A = {1, 2, 3};
> A{[true, false, true]}
>
> You should see two ans values displayed; that second line of code is
> essentially the same as typing:
>
> 1, 3
>
> So you ARE calling NANMEAN with too many inputs.
>
> What you want to do is concatenate all those int32's together or make precip
> a regular int32 array (NOT a cell array) from the start, something like:
>
> t = [A{[true, false, true]}]
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Oh! I understand now. Thank you. A pair of well-placed brackets makes all the difference. I just changed the offending line:

   xm(i) = nanmean([prcp{mo==i}]); %Logical indexing of months

(There is a lot of grumbling from matlab but it appears to do the trick: Warning: Concatenation involves an empty array with an incorrect number of rows. This may not be allowed in a future release.)

Thanks for your time and help.

Subject: Error using ==> nanmean Too many input arguments.

From: Giusep

Date: 27 Jan, 2012 22:51:09

Message: 4 of 9

"Giusep " <lyngly@ymail.com> wrote in message <jft7ga$i7$1@newscl01ah.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <jfs5sh$eue$1@newscl01ah.mathworks.com>...
> >
> >
> > "Giusep " <lyngly@ymail.com> wrote in message
> > news:jfrqpm$64g$1@newscl01ah.mathworks.com...
> > > Hello,
> > >
> > > I'm pretty new to matlab and have read the help manual for nanmean,
> > > inspected the variables in question, and made sure there are no other
> > > files named 'nanmean' (from other "too many arguments" posts it seems the
> > > wrong path is listed or there is something else using the same name), but
> > > I continue to get the error:
> > >
> > > Error using ==> nanmean
> > > Too many input arguments.
> > >
> > > Error in ==> monthlyaverage at 18
> > > xm(i) = nanmean(precip{mo==i});
> > >
> > > The code in question is:
> > >
> > > [yr,mo] = datevec(newdate, 'dd-mm-yyyy'); % d must be array of strings, xm
> > > = zeros(12,1); for i = 1:12
> > > xm(i) = nanmean(precip{mo==i}); %Logical indexing of months
> > > end
> > >
> > > precip is an array of <1x14375 cell> int32 elements
> >
> > When you index into a cell array using {} and logical indexing, what you get
> > back is a comma-separated list of length equal to the number of true values
> > in the index.
> >
> > A = {1, 2, 3};
> > A{[true, false, true]}
> >
> > You should see two ans values displayed; that second line of code is
> > essentially the same as typing:
> >
> > 1, 3
> >
> > So you ARE calling NANMEAN with too many inputs.
> >
> > What you want to do is concatenate all those int32's together or make precip
> > a regular int32 array (NOT a cell array) from the start, something like:
> >
> > t = [A{[true, false, true]}]
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com
>
> Oh! I understand now. Thank you. A pair of well-placed brackets makes all the difference. I just changed the offending line:
>
> xm(i) = nanmean([prcp{mo==i}]); %Logical indexing of months
>
> (There is a lot of grumbling from matlab but it appears to do the trick: Warning: Concatenation involves an empty array with an incorrect number of rows. This may not be allowed in a future release.)
>
> Thanks for your time and help.

I just realized that this concatenation method is unexpectedly changing my results.
All NaN are being concatenated as zeroes. The rest of the code I ran is as follows:

yy = unique(yr); % The years
ny = length(yy); % Total number of years
xy = zeros(ny,1); % Annual average
for i = 1:ny
   xy(i) = nanmean([precip{yr==yy(i)}]);
end

xym = zeros(ny,12);
for i = 1:ny
   for j = 1:12
      xym(i,j) = nanmean([precip{(yr==yy(i)) & (mo==i)}]); %monthly average each year
   end
end

precip is a cell with a number of empty elements, so when calculating xy(i) I get an error that :
??? In an assignment A(I) = B, the number of elements in B and
 I must be the same.
So I replaced the empty elements with NaN:
ix=cellfun('isempty', precip);
precip(ix) = {nan};

but I was getting different resulting numbers in that initial monthly average code as a result, and I discovered that the NaN were being automatically converted to zeroes, which obviously gives different means.

aa = [precipCopy{mo==1}]; % precipCopy has empty cells converted to NaN
ab = [precip{mo==1}]; % precip has empty cells untouched, producing those warnings mentioned in the last post
aa is 1x1165
ab is 1x266

I tried to make an array in different ways, like using cell2mat(precip), but the NaN is double and the cells with numbers are int32. I'm trying to use the cast function to make it all the same type... something like:

ix = cellfun(@(x) all(isnan(x)), precipCopy);
newprecip = cast([precipCopy{ix}], 'int32');

this isn't working yet, but i'm wondering if this is even the direction I should be taking. Does anyone recommend another way to concatenate NaNs that doesn't convert them to zeroes? Or another direction I'm missing in approaching these calculations?
Many thanks.

Subject: Error using ==> nanmean Too many input arguments.

From: Peter Perkins

Date: 27 Jan, 2012 23:12:15

Message: 5 of 9

On 1/26/2012 11:00 PM, Giusep wrote:
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <jfs5sh$eue$1@newscl01ah.mathworks.com>...
>>
>>
>> "Giusep " <lyngly@ymail.com> wrote in message
>> news:jfrqpm$64g$1@newscl01ah.mathworks.com...
>> > Hello,
>> >
>> > I'm pretty new to matlab and have read the help manual for nanmean,
>> > inspected the variables in question, and made sure there are no
>> other > files named 'nanmean' (from other "too many arguments" posts
>> it seems the > wrong path is listed or there is something else using
>> the same name), but > I continue to get the error:
>> >
>> > Error using ==> nanmean
>> > Too many input arguments.
>> >
>> > Error in ==> monthlyaverage at 18
>> > xm(i) = nanmean(precip{mo==i});
>> >
>> > The code in question is:
>> >
>> > [yr,mo] = datevec(newdate, 'dd-mm-yyyy'); % d must be array of
>> strings, xm > = zeros(12,1); for i = 1:12
>> > xm(i) = nanmean(precip{mo==i}); %Logical indexing of months
>> > end
>> >
>> > precip is an array of <1x14375 cell> int32 elements
>>
>> When you index into a cell array using {} and logical indexing, what
>> you get back is a comma-separated list of length equal to the number
>> of true values in the index.
>>
>> A = {1, 2, 3};
>> A{[true, false, true]}
>>
>> You should see two ans values displayed; that second line of code is
>> essentially the same as typing:
>>
>> 1, 3
>>
>> So you ARE calling NANMEAN with too many inputs.
>>
>> What you want to do is concatenate all those int32's together or make
>> precip a regular int32 array (NOT a cell array) from the start,
>> something like:
>>
>> t = [A{[true, false, true]}]
>>
>> --
>> Steve Lord
>> slord@mathworks.com
>> To contact Technical Support use the Contact Us link on
>> http://www.mathworks.com
>
> Oh! I understand now. Thank you. A pair of well-placed brackets makes
> all the difference. I just changed the offending line:
>
> xm(i) = nanmean([prcp{mo==i}]); %Logical indexing of months

Not exactly. I think what you want is nanmean(prcp(mo==i)). Compare
what you get from prcp(1:3) and prcp{1:3} to understand what's up here.

What you have can have some funny side effects, as you have noticed.

>
> (There is a lot of grumbling from matlab but it appears to do the trick:
> Warning: Concatenation involves an empty array with an incorrect number
> of rows. This may not be allowed in a future release.)
>
> Thanks for your time and help.

Subject: Error using ==> nanmean Too many input arguments.

From: Giusep

Date: 28 Jan, 2012 00:06:09

Message: 6 of 9

Peter Perkins <Peter.Remove.Perkins.This@mathworks.com> wrote in message <jfvb0f$37t$1@newscl01ah.mathworks.com>...
> On 1/26/2012 11:00 PM, Giusep wrote:
> > "Steven_Lord" <slord@mathworks.com> wrote in message
> > <jfs5sh$eue$1@newscl01ah.mathworks.com>...
> >>
> >>
> >> "Giusep " <lyngly@ymail.com> wrote in message
> >> news:jfrqpm$64g$1@newscl01ah.mathworks.com...
> >> > Hello,
> >> >
> >> > I'm pretty new to matlab and have read the help manual for nanmean,
> >> > inspected the variables in question, and made sure there are no
> >> other > files named 'nanmean' (from other "too many arguments" posts
> >> it seems the > wrong path is listed or there is something else using
> >> the same name), but > I continue to get the error:
> >> >
> >> > Error using ==> nanmean
> >> > Too many input arguments.
> >> >
> >> > Error in ==> monthlyaverage at 18
> >> > xm(i) = nanmean(precip{mo==i});
> >> >
> >> > The code in question is:
> >> >
> >> > [yr,mo] = datevec(newdate, 'dd-mm-yyyy'); % d must be array of
> >> strings, xm > = zeros(12,1); for i = 1:12
> >> > xm(i) = nanmean(precip{mo==i}); %Logical indexing of months
> >> > end
> >> >
> >> > precip is an array of <1x14375 cell> int32 elements
> >>
> >> When you index into a cell array using {} and logical indexing, what
> >> you get back is a comma-separated list of length equal to the number
> >> of true values in the index.
> >>
> >> A = {1, 2, 3};
> >> A{[true, false, true]}
> >>
> >> You should see two ans values displayed; that second line of code is
> >> essentially the same as typing:
> >>
> >> 1, 3
> >>
> >> So you ARE calling NANMEAN with too many inputs.
> >>
> >> What you want to do is concatenate all those int32's together or make
> >> precip a regular int32 array (NOT a cell array) from the start,
> >> something like:
> >>
> >> t = [A{[true, false, true]}]
> >>
> >> --
> >> Steve Lord
> >> slord@mathworks.com
> >> To contact Technical Support use the Contact Us link on
> >> http://www.mathworks.com
> >
> > Oh! I understand now. Thank you. A pair of well-placed brackets makes
> > all the difference. I just changed the offending line:
> >
> > xm(i) = nanmean([prcp{mo==i}]); %Logical indexing of months
>
> Not exactly. I think what you want is nanmean(prcp(mo==i)). Compare
> what you get from prcp(1:3) and prcp{1:3} to understand what's up here.
>
> What you have can have some funny side effects, as you have noticed.
>

Okay, so to compare:
>> precip(1:3) % making another cell array
ans =
    [NaN] [NaN] [NaN]

>> [precip{1:3}] % making a regular array
ans =
   NaN NaN NaN

But don't I just want to concatenate the elements inside the cells to make a regular array, instead of making another cell array? I thought this is what Steve Lord was writing:
> >> What you want to do is concatenate all those int32's together or make
> >> precip a regular int32 array (NOT a cell array) from the start,
> >> something like:
> >>
> >> t = [A{[true, false, true]}]

If I try running the code without curly braces:
??? Undefined function or method 'isnan' for input arguments of type 'cell'.

Error in ==> nanmean at 17
nans = isnan(x);

Error in ==> monthlyaverage at 26
   xm(i) = nanmean(precip(mo==i)); %Logical indexing of months

Unless I'm missing something, I think this part of the code is working when concatenating the cell elements into an array with the form [A{i}], but my issue was that Matlab started converting all the NaN cell elements into 0's in the other bits of code (my previous post).

Subject: Error using ==> nanmean Too many input arguments.

From: Tom Lane

Date: 30 Jan, 2012 15:09:22

Message: 7 of 9

> Unless I'm missing something, I think this part of the code is working
> when concatenating the cell elements into an array with the form [A{i}],
> but my issue was that Matlab started converting all the NaN cell elements
> into 0's in the other bits of code (my previous post).

I do not follow all of what you are trying to do. However, I understand you
have a precip array that has, I guess, int32 values (not sure if scalar or
larger) in some elements and NaN in other elements.

There is no int representation for NaN, so you are right, if you concatenate
a NaN with int32 value you will get zeros in those spots. Would it make
sense for you to simply have empty values in those cells instead of NaN?

>> c = {int32(1), [], int32(2)}
c =
    [1] [] [2]
>> nanmean([c{:}])
ans =
    1.5000

-- Tom

Subject: Error using ==> nanmean Too many input arguments.

From: Giusep

Date: 30 Jan, 2012 16:28:09

Message: 8 of 9

"Tom Lane" <tlane@mathworks.com> wrote in message <jg6br2$elq$1@newscl01ah.mathworks.com>...
> > Unless I'm missing something, I think this part of the code is working
> > when concatenating the cell elements into an array with the form [A{i}],
> > but my issue was that Matlab started converting all the NaN cell elements
> > into 0's in the other bits of code (my previous post).
>
> I do not follow all of what you are trying to do. However, I understand you
> have a precip array that has, I guess, int32 values (not sure if scalar or
> larger) in some elements and NaN in other elements.
>
> There is no int representation for NaN, so you are right, if you concatenate
> a NaN with int32 value you will get zeros in those spots. Would it make
> sense for you to simply have empty values in those cells instead of NaN?
>
> >> c = {int32(1), [], int32(2)}
> c =
> [1] [] [2]
> >> nanmean([c{:}])
> ans =
> 1.5000
>
> -- Tom

Thanks for responding. In message 4 I detailed how I can't use empty values because I get the error:
??? In an assignment A(I) = B, the number of elements in B and I must be the same.

The int32 values are scalar, and it was ill-advised to store these values in a cell I think. I'm just trying to concatenate them in a regular array and run my code so that matlab doesn't either ignore empty cells or convert NaN to 0. From what you're telling me I think I should focus on using cast() to convert the whole cell array to another data type than can preserve NaN--it appears 'double' is the way to go. Thanks for the tip.

Subject: Error using ==> nanmean Too many input arguments.

From: Steven_Lord

Date: 31 Jan, 2012 15:47:45

Message: 9 of 9



"Giusep " <lyngly@ymail.com> wrote in message
news:jg6gep$1v9$1@newscl01ah.mathworks.com...
> "Tom Lane" <tlane@mathworks.com> wrote in message
> <jg6br2$elq$1@newscl01ah.mathworks.com>...

*snip*

> The int32 values are scalar, and it was ill-advised to store these values
> in a cell I think. I'm just trying to concatenate them in a regular array
> and run my code so that matlab doesn't either ignore empty cells or
> convert NaN to 0.

>> y = int32(NaN)

y =

           0

When you convert NaN to an integer class, what you get back is a zero of
that integer class.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
nan concatenation Giusep 27 Jan, 2012 17:54:17
rssFeed for this Thread

Contact us at files@mathworks.com