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!