Include value requirement in array multiplication

1 次查看(过去 30 天)
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
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
...
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by