Multiplying structures together based on field names

16 views (last 30 days)
Is there any method to multiply structures together according to their field names? For example,
struct1.a=1;
struct1.b=2;
struct2.a=3;
struct2.b=4;
And the result would be:
struct3.a=3
struct3.b=8
  1 Comment
Stephen23
Stephen23 on 10 Apr 2015
Edited: Stephen23 on 10 Apr 2015
Note that storing sets of data in one variable makes more sense than lots of sequentially-named variables, and simplifies the code significantly over creating and using dynamically named variables. You might like to read this to learn some of the reasons why:
This is what David Young proposes in their second solution, and you should seriously consider using a non-scalar structure to store your data rather than multiple individual variables.

Sign in to comment.

Accepted Answer

David Young
David Young on 10 Apr 2015
Edited: David Young on 10 Apr 2015
A function to multiply two structures field by field. This needs to be copied into a file on your MATLAB path called multiplyStructs.m
function s3 = multiplyStructs(s1, s2)
%MULTIPLYSTRUCTS multiplies structures field by field
% S3 = MULTIPLYSTRUCTS(S1, S2) returns a structure S3 with those fields
% that are common to S1 and S2. The value of each such field is the
% matrix product of the values of the corresponding fields of S1 and S2.
% get common field names
f1 = fieldnames(s1);
fnames = f1(ismember(f1, fieldnames(s2)));
% multiply
for k = 1:length(fnames)
fname = fnames{k};
s3.(fname) = s1.(fname) * s2.(fname); % matrix product
end
end
Test code:
struct1.a = 1;
struct1.b = 2;
struct1.c = 3; % will be ignored as no match
struct1.d = 4;
struct2.a = 3;
struct2.b = 4;
struct2.d = 10;
struct3 = multiplyStructs(struct1, struct2)
An alternative way, which works only if all the structures have the same fields, but which generalises more readily to multiple structures, is to use this function:
function s = structProd(svec)
%STRUCTPROD returns the field-by-field products of a structure array
% S = STRUCTPROD(SARR) takes a structure array and returns a scalar
% structure with the same fields. The value of each field of S is the
% product of the values of the corresponding fields of SARR, which must
% all be numerical scalars.
%
% See also: prod
fnames = fieldnames(svec);
for k = 1:length(fnames)
fname = fnames{k};
s.(fname) = prod([svec.(fname)]);
end
end
and then put the structures into an array (which indeed might be the best way to store them to start with. Here's the test code:
struct1.a = 1;
struct1.b = 2;
struct1.d = 4;
struct2.a = 3;
struct2.b = 4;
struct2.d = 10;
struct3.a = -1;
struct3.b = -1;
struct3.d = -1;
struct4.a = 4;
struct4.b = 1;
struct4.d = 2;
struct5 = structProd([struct1 struct2 struct3 struct4])
  2 Comments
David Young
David Young on 10 Apr 2015
Note that my second method is equivalent to Stephen Cobeldick's approach - the difference is just that it uses an explicit loop rather than cellfun to iterate over field names.
Oliz
Oliz on 10 Apr 2015
Thank you very much for the detailed answer. Your first solution solves my problem :)
It is generic and easily modified for similar purposes.

Sign in to comment.

More Answers (1)

Jonathan Campelli
Jonathan Campelli on 9 Apr 2015
Hello Oliz,
I've played around with your structures, and I have multiplied struct1.a through struct2.b in the following way:
struct1.a=1
struct1.b=2
struct2.a=3
struct2.b=4
struct3.a=struct2.a*struct1.a %The multiplication operator provides the right solution.
struct3.b=struct2.b*struct1.b
Once run, the script yields the following results:
struct1 =
a: 1
struct1 =
a: 1
b: 2
struct2 =
a: 3
struct2 =
a: 3
b: 4
struct3 =
a: 3
struct3 =
a: 3
b: 8
Keep having fun with these things.
Best regards,
Jonathan Campelli
  3 Comments
Oliz
Oliz on 9 Apr 2015
Thanks for this, I saw this earlier when searching through the forum. Unfortunately, I don't think it applies to my case since I don't want to multiply each field in my structure by a constant, but rather dynamically based on values inside another structure sharing the same field name.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!