How to perform calculations using a structure array ?

4 views (last 30 days)
Hello everyone, I have a structure array named model with 26 fields, with S (metabolites as rows and reactions as columns) as one of the fields. For each column of S, I want to check whether the sum is zero. If it is zero, I want to feed the index of the respective column into another field in the same structure, model.reactions and combine all such reactions to an output named transport.
Could someone help me?

Answers (1)

BhaTTa
BhaTTa on 21 Oct 2024
Edited: BhaTTa on 21 Oct 2024
Hey @Priyadharshini Kannan, I assume that 'model' is your structure with a field 'S' which is a matrix and another field 'reactions' which is a cell array. Below I have provided the code where I have initialized the value of S with dummy values
% Define a structure named 'model'
model = struct();
% Create a modified S matrix with a column that sums to zero
model.S = [
-2, 2, 3, 5, -1;
0, 4, 5, 0, -1;
0, 0, 0, -4, -1;
5, -5, -2, -2, 1;
-3, -1, -6, 1, 2
];
disp('Modified S matrix:');
disp(model.S);
zeroSumIndices = [];
numColumns = size(model.S, 2);
for col = 1:numColumns
% Check if the sum of the column is zero
if sum(model.S(:, col)) == 0
% If the sum is zero, append the index to zeroSumIndices
zeroSumIndices(end + 1) = col;
end
end
% Store the indices of zero-sum columns in the model.reactions field
model.reactions = zeroSumIndices;
% Combine all such reactions into an output named 'transport'
transport = model.reactions;
% Display the transport array
disp('Indices of columns with zero sum (transport):');
disp(transport);
Hope it helps.

Community Treasure Hunt

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

Start Hunting!