Structfun with multiple inputs

Hello!
I hope your day is going well. I have an issue I am unable to succinctly describe, so I will instead demonstrate it.
Let's say I have two structures:
A.a = zeros(10, 10, 10) + 1; % 3d all 1s
A.b = zeros(10, 10, 10) + 2; % 3d all 2s
A.c = zeros(5, 5) + 3; % 2d all 3s
B.a = zeros(10, 10, 10) + 1; % 3d all 1s
B.b = zeros(10, 10, 10) + 1; % 3d all 1s
B.c = zeros(5, 5) + 1; % 2d all 1s.
As can be seen, A and B have the same size for each respective array in their structure. Ex. A.a is the same size as B.a. However, A.a and B.c are not the same size (A.a being 3d and B.c being 2d).
I would like to subtract each array with these two structures from each other. If I were doing only one array, it would look something like this:
C = bsxfun(@minus, A.a, B.a);
This returns a 3d array with all zeros since A.a and B.a are exactly equal.
I would like to do this for A.a - B.a, A.b - B.b, A.c - B.c, etc. In this example I have only 3 arrays in the strucutre for the sake of brevity and simplicity, but in my actual I have several arrays in each structure. I understand I could write a For Loop which iterates through each name in the structure, but I feel that using the Structfun and Bsxfun I could do this in significantly less code in a much less complicated way. However, I am unsure how to use Structfun such that I have two structures as inputs. Could you help me figure this out?
Any suggestions would be greatly appreciated. Thank you!

5 Commenti

"...I am unsure how to use Structfun such that I have two structures as inputs"
On occasion I have also wanted to process two (or more) structures with the same fieldnames, and thought that it would be very handy if STRUCTFUN supported this (as far as I am aware, it only supports one structure input).
Probably using a loop is the simplest, clearest, and most efficient solution.
Matt J
Matt J il 28 Giu 2021
Modificato: Matt J il 28 Giu 2021
@Alec Huynh I do not understand the purpose of bsxfun here. You say that each corresponding field of A and B are arrays of the same size, which means that it should be sufficient to do,
C.a = A.a -B.a; %and similarly for other fields
Also, you have marked that your Matlab release is R2016b, which means that even if scalar expansion was necessary, it would be done implicitly without bsxfun,
A.a=[1 2 3; 4 5 6]*10;
B.a=[1,1,1];
ans=A.a-B.a
ans = 2×3
9 19 29 39 49 59
@Stephen CobeldickTake a look at Nikhil's solution. It doesn't use structfun at all, but it was pretty clever work around I thought.
@Matt J I used bsxfun in this example because I was trying to get it into a structfun format which I am not terribly familiar with and then scale up from there. In other words, I did it because I don't know what I'm doing :).
Stephen23
Stephen23 il 29 Giu 2021
Modificato: Stephen23 il 29 Giu 2021
@Alec Huynh: sure converting to cell works, but is a fragile solution (it loses the fieldnames), is more complex than if STRUCTFUN actually accepted any number of structures, and adds some overhead (creating cell header in memory).
Note that overloading MINUS is not required, you can just run that code on those structures.
@Stephen Cobeldick Yes you're correct that it is more complex and does add overhead through cells. I may be misunderstanding what you mean, but when I ran the code it actually preserved the fields names!

Accedi per commentare.

 Risposta accettata

Hi Alec,
You can overload the minus function for structures and then return a structure from it.
Implement the function below and save it in a seperate file
function C = minus(A,B)
C = cellfun(@minus,struct2cell(A),struct2cell(B),'UniformOutput',false); %# Apply plus cell-wise
C = cell2struct(C,fieldnames(A));
end
A.a = zeros(10, 10, 10) + 1; % 3d all 1s
A.b = zeros(10, 10, 10) + 2; % 3d all 2s
A.c = zeros(5, 5) + 3; % 2d all 3s
B.a = zeros(10, 10, 10) + 1; % 3d all 1s
B.b = zeros(10, 10, 10) + 1; % 3d all 1s
B.c = zeros(5, 5) + 1; % 2d all 1s.
C = A - B %should give you the expected result
Thanks,
Nikhil

2 Commenti

Thank you! That worked perfectly for me.
You're welcome

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by