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 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