How can I change multiple variable name within a loop, while assigning those variables as matrix values?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Krish Desai
il 4 Set 2016
Commentato: Stephen23
il 5 Set 2016
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 Commenti
Stephen23
il 5 Set 2016
Creating variables names in a loop is a really bad idea, because it creates slow buggy code:
Risposta accettata
Più risposte (1)
John BG
il 5 Set 2016
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 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!