Thread Subject: adding elements in structures

Subject: adding elements in structures

From: Juliette Salexa

Date: 23 Jul, 2009 22:10:03

Message: 1 of 9

If I have the structure s:

s=

a: [1 2 3 4 5]
b: [1 2 3 4 5]
c: [1 2 3 4 5]

-------------------------
Is there a way to perform arithmetic operations on these, such as for example:
a(1)+b(1)+c(1) =3

except without writing all that out ?? [I'm asking because my structure has thousands of lines.

Subject: adding elements in structures

From: Nathan

Date: 23 Jul, 2009 22:49:47

Message: 2 of 9

On Jul 23, 3:10 pm, "Juliette Salexa" <juliette.physic...@gmail.com>
wrote:
> If I have the structure s:
>
> s=
>
> a: [1 2 3 4 5]
> b: [1 2 3 4 5]
> c: [1 2 3 4 5]
>
> -------------------------
> Is there a way to perform arithmetic operations on these, such as for example:
> a(1)+b(1)+c(1) =3
>
> except without writing all that out ?? [I'm asking because my structure has thousands of lines.

How about this:
s.a = [1:5]; s.b = s.a; s.c = s.a;
for i=1:length(s.a)
addition(i) = sum(structfun(@sum,(structfun(@(field)field
(i),s,'UniformOutput',false))));
end

It could probably be cleaned up nicer, but this seems to do what
you're example did.

Good luck.
Hope this helped.
-Nathan

Subject: adding elements in structures

From: Phil Goddard

Date: 23 Jul, 2009 23:03:01

Message: 3 of 9

Assuming all fields in the struct have an equi-length vector in them,

>> sum(cell2mat(struct2cell(s)))

Phil.

Subject: adding elements in structures

From: Juliette Salexa

Date: 23 Jul, 2009 23:06:01

Message: 4 of 9

What the heck ??
----------------------------------------------
addition(i) = sum(structfun(@sum,(structfun(@(field)field(i),s,'UniformOutput',false))))
----------------------------------------------

The structfun documentation has ONE example, and it's not even mathematical.
Mathematica's documentation pages have thousands of examples for every command. Each time someone asks a question like the one I just did, the answer should be added to the list of examples on the documentation!

I still haven't been able to figure out what "field" is, particularly:
@(field)field(i)

....
Your idea will probably help me in what I'm trying to do though. Thanks alot!

Subject: adding elements in structures

From: Juliette Salexa

Date: 23 Jul, 2009 23:09:02

Message: 5 of 9

Thank you Phil !!!

I was looking for something like struc2cell but I didn't know the command .... so couldn't look up how to use it!

I knew num2cell, and looked to see if there would be something helpful in the "see also" at the bottom of the documentation for "num2cell" ... but it wasn't there!

MATLAB NEEDS TO IMPROVE ITS DOCUMENTATION!

Subject: adding elements in structures

From: Nathan

Date: 23 Jul, 2009 23:28:38

Message: 6 of 9

On Jul 23, 4:06 pm, "Juliette Salexa" <juliette.physic...@gmail.com>
wrote:
> What the heck ??
> ----------------------------------------------
> addition(i) = sum(structfun(@sum,(structfun(@(field)field(i),s,'UniformOutput',false))))
> ----------------------------------------------
>
> The structfun documentation has ONE example, and it's not even mathematical.  
> Mathematica's documentation pages have thousands of examples for every command.  Each time someone asks a question like the one I just did, the answer should be added to the list of examples on the documentation!
>
> I still haven't been able to figure out what "field" is, particularly:
> @(field)field(i)
>
> ....
> Your idea will probably help me in what I'm trying to do though. Thanks alot!


Yeah. I was trying to check the examples out too, but to no luck. I
found the field example on the CSSM somewhere, and decided I could use
that to help you out.

The field thing returns the element for each substructure at the given
index, I think. (My verbal syntax may be wrong, but I think it gets
the point across).

But yeah. Use the struct2cell. It's way faster : )

Good luck, once again!

-Nathan

Subject: adding elements in structures

From: Steven Lord

Date: 24 Jul, 2009 15:04:09

Message: 7 of 9


"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message
news:h4aqid$lke$1@fred.mathworks.com...
> Thank you Phil !!!
>
> I was looking for something like struc2cell but I didn't know the command
> .... so couldn't look up how to use it!
>
> I knew num2cell, and looked to see if there would be something helpful in
> the "see also" at the bottom of the documentation for "num2cell" ... but
> it wasn't there!
>
> MATLAB NEEDS TO IMPROVE ITS DOCUMENTATION!

If you have a suggestion for how to improve a documentation page, click on
the "Provide feedback about this page" links on the right-hand side of the
page and enter your suggestion into the form that appears. That will send
your suggestion to the documentation staff responsible for that page for
consideration.

--
Steve Lord
slord@mathworks.com

Subject: adding elements in structures

From: dpb

Date: 24 Jul, 2009 17:32:23

Message: 8 of 9

Juliette Salexa wrote:
> Thank you Phil !!!
>
> I was looking for something like struc2cell but I didn't know the
> command .... so couldn't look up how to use it!
>
> I knew num2cell, and looked to see if there would be something
> helpful in the "see also" at the bottom of the documentation for
> "num2cell" ... but it wasn't there!
>
> MATLAB NEEDS TO IMPROVE ITS DOCUMENTATION!

There's always room for improvement; I've a pet peeve that the
documentation has seemingly simply grown in volume following the
original "how to" style of a tutorial rather than "growing up" into a
full specification/documentation of the language. That would be, of
course, a major undertaking...

As for the problem you outlined of the need to know what to look up,
there are two basic routes that are very useful--

 >> lookfor structure
BVPSET Create/alter BVP OPTIONS structure.
ODESET Create/alter ODE OPTIONS structure.
OPTIMSET Create/alter OPTIM OPTIONS structure.
HANDLE2STRUCT Convert Handle Graphics hierarchy to structure array.
STRUCT2HANDLE Create Handle Graphics hierarchy from structure.
  GUIHANDLES Return a structure of handles.
MAKEMENU Create menu structure.
PRINTMENU Print pull-down menu structure to the command window.
CELL2STRUCT Convert cell array to structure array.
...

and, alternatively--

 >> help

HELP topics:

matlab\general - General purpose commands.
matlab\ops - Operators and special characters.
matlab\lang - Programming language constructs.
...
matlab\iofun - File input/output.
matlab\timefun - Time and dates.
matlab\datatypes - Data types and structures.
...

 >> help datatypes

   Data types and structures.

   Data types (classes)
     double - Convert to double precision.
     sparse - Create sparse matrix.
     char - Create character array (string).
     cell - Create cell array.
     struct - Create or convert to structure array.
     ...

   Cell array functions.
     cell - Create cell array.
     cellfun - Functions on cell array contents.
     celldisp - Display cell array contents.
     cellplot - Display graphical depiction of cell array.
     num2cell - Convert numeric array into cell array.
     deal - Deal inputs to outputs.
     cell2struct - Convert cell array into structure array.
     struct2cell - Convert structure array into cell array.
     iscell - True for cell array.

   Structure functions.
     ...

--

Subject: adding elements in structures

From: Matt Fig

Date: 25 Jul, 2009 02:18:03

Message: 9 of 9

"Phil Goddard" <philNOSPAM@goddardconsulting.ca> wrote in message <h4aq75$44$1@fred.mathworks.com>...
> Assuming all fields in the struct have an equi-length vector in them,
>
> >> sum(cell2mat(struct2cell(s)))
>
> Phil.


I would think this is faster since it uses only built-ins (cell2mat is an M-file).

S = struct2cell(s);
T = sum(cat(1,S{:}));

But I don't have a thousand-field structure lying around to test it.

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

Contact us at files@mathworks.com