フィルターのクリア

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 件のコメント
Krish Desai
Krish Desai 2016 年 9 月 5 日
Your edits to #1 are correct. #2 is correct as is, with (1-n1) as the denominator.

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

採用された回答

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 Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by