Include value requirement in array multiplication

1 回表示 (過去 30 日間)
Robert Demyanovich
Robert Demyanovich 2021 年 6 月 19 日
編集済み: Robert Demyanovich 2021 年 6 月 20 日
I currently have the following line of code:
dS=k1*cA(i+1,:).*cB(i+1,:)*dt
dS is the amount of product S resulting from a reaction between A and B, which reaction has a rate constant of k1. cA and cB are the concentrations of A and B respectively and dt is the time step.
Now I would like to specify that a dS value should only be calculated if both the cA cell value and cB cell value which are being multipled are greater than a specific value - in this case 1E-04. If either cA or cB is less than this value, then the result of the multiplication should be zero.
How would I program this requirement in MatLab?

回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 19 日
for ii=1:N
if cA>1e-4 & cB>1e-4
dS=k1*cA(i+1,:).*cB(i+1,:)*dt;
else
dS = 0;
end
end
  1 件のコメント
Robert Demyanovich
Robert Demyanovich 2021 年 6 月 19 日
編集済み: Robert Demyanovich 2021 年 6 月 19 日
First, I assume that the for loop is for the time step, dt. I already have that elsewhere. If not, then shouldn't the columns for the concentrations in line 3 contain ii?
However, I don't really understand the if statement. Since cA and cB are arrays, it looks like the if statement is directing that the dS statement only occur for element values > 1e-4. But how can that happen without a loop of some kind? if ii is supposed to be that loop, then what is N? And if the for loop is that loop, then shouldn't it be?
dS=k1*cA(i+1,ii).*cB(i+1,ii)*dt;
Further, if this is true, what's the advantage of Matlab over visual basic. If ii is needed in the dS statement then this is going to run quite slow, I think.

サインインしてコメントする。


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 19 日
編集済み: Sulaymon Eshkabilov 2021 年 6 月 19 日
...
N = size(cA, 1);
for ii=1:N
if cA>1e-4 & cB>1e-4
dS(ii,:)=k1*cA(ii,:).*cB(ii,:)*dt;
else
dS(ii,:) = 0;
end
end
%%
Alternative and most efficient way is vectorization and logical indexing:
dS=k1*cA.*cB*dt;
IDX = (cA<1e-4 & cB<1e-4); % Logical indexing
dS(IDX,:)=0; % Takes care of both conditions cA<1e-4 & cB<1e-4
  2 件のコメント
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 19 日
Consider the vectorization approach that is much more efficient and fast.
Robert Demyanovich
Robert Demyanovich 2021 年 6 月 20 日
編集済み: Robert Demyanovich 2021 年 6 月 20 日
I should be more clear. This code is executing in an outer loop with dt, and i+1 must remain fixed. I'm only just learning MatLab, but it seems to me if your code is just using cA and cB it will act over all of the time rows in the array which is not desired. I want it to act on all of the columns, which are the incremental distances (dx) for the specific incremental time given by i+1. I tried putting this variation in your suggested code. The code was extremely slow, but went pretty fast for the alternative way. Neither produced the result I should be seeing. I tried:
dS=k1*cA(i+1,:).*cB(i+1,:)*dt;
IDX = (cA(i+1,:)<1e-4 & cB(i+1,:)<1e-4); % Logical indexing
dS(IDX)=0;
In looking at it again, the logic is if cA(i+1,:)<1e-4 or cB(i+1,:)<1e-4. Both conditions may be true, but don't have to be. I can't seem to find how to code "or" operator in Matlab.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by