Thread Subject: How can I use plus operator on dataset element?

Subject: How can I use plus operator on dataset element?

From: Richard Johnson

Date: 9 Nov, 2009 10:46:01

Message: 1 of 4

Does anyone know how to use operators such as 'plus' with elements of a datest?

I want to do the following:

A is a dataset of size (m,5).
B=zeros(1,5)

for i =1:m
 for j=1:5
     B(1,j)=B(1,j)+A(i,j);
  end;
end;

This generates an error an error message because I can't use the operator 'plus' with an element of a dataset. MATLAB seems to want me to refer to each column in dataset A like this:

for i=1:m
   B(1,1)=B(1,1)+A.Var1(i)
end

But this isn't convenient if I have to perform the same operation on lots of columns.

Is there a way I can use the plus operator on a dataset?

Many thanks,

Richard.

Subject: How can I use plus operator on dataset element?

From: Steven Lord

Date: 9 Nov, 2009 14:23:29

Message: 2 of 4


"Richard Johnson" <Richard.Johnson@fsa.gov.uk> wrote in message
news:hd8rt9$9r6$1@fred.mathworks.com...
> Does anyone know how to use operators such as 'plus' with elements of a
> datest?
>
> I want to do the following:
>
> A is a dataset of size (m,5).
> B=zeros(1,5)
>
> for i =1:m
> for j=1:5
> B(1,j)=B(1,j)+A(i,j);
> end;
> end;
>
> This generates an error an error message because I can't use the operator
> 'plus' with an element of a dataset.

That's correct, PLUS is not defined for dataset objects. What would it mean
to add two dataset objects together if they had different numbers of
observations, different variables, etc? [If that's the type of thing you're
interested in doing, look at the HORZCAT, VERTCAT, CAT, and JOIN methods for
dataset arrays.]

> MATLAB seems to want me to refer to each column in dataset A like this:
>
> for i=1:m
> B(1,1)=B(1,1)+A.Var1(i)
> end
>
> But this isn't convenient if I have to perform the same operation on lots
> of columns.

Use DATASETFUN.

http://www.mathworks.com/access/helpdesk/help/toolbox/stats/dataset.datasetfun.html

> Is there a way I can use the plus operator on a dataset?

See above.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: How can I use plus operator on dataset element?

From: Richard Johnson

Date: 9 Nov, 2009 14:47:02

Message: 3 of 4

Dear Steve Lord,

thanks for your reply. Sorry, but I don't see how to use the datasetfun function. What am I supposed to substitute for 'fun' in this case? 'Plus' doesn't seem to work.

Maybe it would be easier not to use datasets. I don't think they existed when I first learned MATLB. Can I transform a dataset object A into a matrix B such that MATLAB will not complain about references to B(i,j)?

Many thanks,

Richard.


"Steven Lord" <slord@mathworks.com> wrote in message <hd98j6$8n4$1@fred.mathworks.com>...
>
> "Richard Johnson" <Richard.Johnson@fsa.gov.uk> wrote in message
> news:hd8rt9$9r6$1@fred.mathworks.com...
> > Does anyone know how to use operators such as 'plus' with elements of a
> > datest?
> >
> > I want to do the following:
> >
> > A is a dataset of size (m,5).
> > B=zeros(1,5)
> >
> > for i =1:m
> > for j=1:5
> > B(1,j)=B(1,j)+A(i,j);
> > end;
> > end;
> >
> > This generates an error an error message because I can't use the operator
> > 'plus' with an element of a dataset.
>
> That's correct, PLUS is not defined for dataset objects. What would it mean
> to add two dataset objects together if they had different numbers of
> observations, different variables, etc? [If that's the type of thing you're
> interested in doing, look at the HORZCAT, VERTCAT, CAT, and JOIN methods for
> dataset arrays.]
>
> > MATLAB seems to want me to refer to each column in dataset A like this:
> >
> > for i=1:m
> > B(1,1)=B(1,1)+A.Var1(i)
> > end
> >
> > But this isn't convenient if I have to perform the same operation on lots
> > of columns.
>
> Use DATASETFUN.
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/stats/dataset.datasetfun.html
>
> > Is there a way I can use the plus operator on a dataset?
>
> See above.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Subject: How can I use plus operator on dataset element?

From: Loren Shure

Date: 9 Nov, 2009 20:45:56

Message: 4 of 4

In article <hd9a16$aqi$1@fred.mathworks.com>, Richard.Johnson@fsa.gov.uk
says...
> Dear Steve Lord,
>
> thanks for your reply. Sorry, but I don't see how to use the datasetfun function. What am I supposed to substitute for 'fun' in this case? 'Plus' doesn't seem to work.
>
> Maybe it would be easier not to use datasets. I don't think they existed when I first learned MATLB. Can I transform a dataset object A into a matrix B such that MATLAB will not complain about references to B(i,j)?
>
> Many thanks,
>
> Richard.

You need to supply a function handle (according to the help) so that
would be @plus, not 'plus'

--
Loren
http://blogs.mathworks.com/loren

Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com