Subtract all values from one structure from another structure
27 次查看(过去 30 天)
显示 更早的评论
I have two structures with various different sections of a dataset. Both structures are identically sized - they are set up exactly the same, with each array within the structure the same length as the corresponding array in the other structure. Is there any way to subtract one structure from another, to create a new strucure, so the values in each corresponding array are subtracting from each other. I have not been able to figure out a way of doing this - can it be done? Or will I have to extract these values from the structure first?
e.g.
%% create the structure
data.x=(0:5);
data.y=(0:3:33);
data1.x=(0:2:10);
data1.y=(0:6:66)
Here, I would want the following to be done:
data.x - data1.x = data2.x
data.y - data1.y = data2.y
Is this possible? Or do I need to take a different approach?
0 个评论
采纳的回答
Rik
2023-3-17
The only way I can think of is looping through the fields:
%% create the structure
data.x=(0:5);
data.y=(0:3:33);
data1.x=(0:2:10);
data1.y=(0:6:66);
data2 = data;
fields = fieldnames(data);
for n=1:numel(fields)
data2.(fields{n}) = data.(fields{n}) - data1.(fields{n});
end
disp(data2)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!