How to perform calculations using a structure array ?
4 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
BhaTTa
2024-10-21
编辑:BhaTTa
2024-10-21
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!