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 件のコメント
Stephen23
2016 年 9 月 5 日
Creating variables names in a loop is a really bad idea, because it creates slow buggy code:
採用された回答
その他の回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!