Creating arrays that depend on each other

2 ビュー (過去 30 日間)
John Doe
John Doe 2021 年 1 月 9 日
回答済み: Deepak 2024 年 8 月 23 日
Hello everyone,
I have a question, I want to create two arrays that depend on each other. Loss and H are already predefind, I want to create in and out, the first value of out depends on in and the second value of in depends on the first value of in.
Should this be created in if statments or just in different steps like:
in(1,1) = 20;
out(1,1) = in(1) - loss(1);
in(2,1) = out(1) - loss(2);
etc.
I feel like there is a better way to do this, but I'm now sure what.
Any help would be appreciated.
Thank you!
This is the original arrays and how they depend on each other
in = [20; out(1) - loss(2);out(2) - loss(3); out(3) - loss(4); out(4) - Ploss(5); out(5) - loss(6); out(6) - loss(7)]
out = [in(1) - loss(1); in(2)+ H(2); in(3)+ H(3); in(4)+ H (4); in(5)+ H(5); in(6)+ H(5)]

回答 (1 件)

Deepak
Deepak 2024 年 8 月 23 日
Hi @John Doe, from my understanding, you have two predefined arrays, named “Loss” and “H”. Now, you want to create two arrays, named “in” and “out”, which depends on each other and on predefined arrays.
To do this task, the most efficient way is to use loop control statements in MATLAB. We can iterate over the array, then calculate the values at each index of “in” and “out” arrays. This way, we do not need to calculate the entire array with custom if statements.
Here is the MATLAB code that addresses this task:
in(1) = 20;
% Compute 'in' and 'out' iteratively
for i = 1:n
if i == 1
out(i) = in(i) - Loss(i);
else
in(i) = out(i-1) - Loss(i);
out(i) = in(i) + H(i);
end
end
Attaching the documentation of loop control statements in MATLAB for reference:
I hope this resolve the issue.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by