How can I change multiple variable name within a loop, while assigning those variables as matrix values?

4 次查看(过去 30 天)
I have a n x 3 matrix called dataMat. I also have n amount of processes that this code will eventually be able to handle. Each process has a n value (n), pressure (p) and volume (v). I want to be able to calculate the work done using 1 of two formulas-formula chosen based on if n=1 or not. My code is below, I don't know how to change the variable names while pulling data from the matrix at the same time.
n=size(dataMat,1);
for i=1:n
Cyc(i)n=dataMat(n,1);
Cyc(i)p=dataMat(n,2);
Cyc(i)v= dataMat(2,3);
end
formulas: if n=1 W=(P1)(V1)ln(V2/V1)
if not W=(P2*V2-P1*V1)/1-n1
  3 个评论

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2016-9-4

更多回答(1 个)

John BG
John BG 2016-9-5
the initial extraction does not need a for loop:
Cyc_n=dataMat(:,1);
Cyc_p=dataMat(:,2);
Cyc_v=dataMat(:,3);
Since it looks like the syntax of the formulas needs improving, I guess your are trying to differentiate,
W=zeros(1,length(Cyc_v))
for k=2:1:length(Cyc_n)
W(k-1)=Cyc_p(k-1)*Cyc_v(k-1)*log(Cyc_v(k)/Cyc_v(k-1))
end
or
for k=2:1:length(Cyc_n)
W(k-1)=Cyc_p(k)*Cyc_v(k)-Cyc_p(k-1)*Cyc_v(k-1)/(Cyc_n(k)-Cyc_n(k));
end
Krish, if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by