Sunday 15 September 2013

MATLAB style for loops in R & loop fails to fill each column in R -



MATLAB style for loops in R & loop fails to fill each column in R -

i in process of converting simplified psychrometric chart matlab code http://www.ohio.edu/mechanical/thermo/applied/chapt.7_11/psychro_chart/psychro.html r, stuck on couple of operations.

1) trying implement next matlab code in r:

# matlab code # both pg , patm defined before in programme phi = 0.1:0.1:0.4, % phi = relative humidity 10% - 40% w = 622*phi*pg./(patm-phi*pg); plot(t,w) end

this effort @ translating above code r:

patm <- 101.325 phi <- as.matrix(seq(0.1, 0.4, 0.1)) pg <- matrix(c(0.61165, 0.65709, 0.70599, 0.75808, 0.81355, 0.87258, 0.93536, 1.00210, 1.07300, 1.14830, 1.22820), nrow = 11, ncol = 1) w <- data.frame(matrix(nrow = nrow(pg), ncol = length(phi))) (i in ncol(w)) { w[i] <- 622 * phi[[i]] * pg/(patm - phi[[i]] * pg) }

instead of filling each column, lastly column has result:

# x1 x2 x3 x4 # 1 na na na 1.505520 # 2 na na na 1.617658 # 3 na na na 1.738379 # 4 na na na 1.867026 # 5 na na na 2.004080 # 6 na na na 2.149996 # 7 na na na 2.305256 # 8 na na na 2.470394 # 9 na na na 2.645922 # 10 na na na 2.832450 # 11 na na na 3.030496 dput(w) structure(list(x1 = c(na, na, na, na, na, na, na, na, na, na, na), x2 = c(na, na, na, na, na, na, na, na, na, na, na), x3 = c(na, na, na, na, na, na, na, na, na, na, na), x4 = structure(c(1.50552046025963, 1.61765774182314, 1.73837871399276, 1.8670263620807, 2.00408001696641, 2.14999576929037, 2.30525601486727, 2.47039440308445, 2.64592183222691, 2.83245044300499, 3.03049575082621), .dim = c(11l, 1l))), .names = c("x1", "x2", "x3", "x4"), row.names = c(na, -11l), class = "data.frame")

how should r code revised desired result?

the next code obtain (desired result).

# x1 x2 x3 x4 # 1 0.3756981 0.7518503 1.128458 1.505520 # 2 0.4036271 0.8077785 1.212455 1.617658 # 3 0.4336856 0.8679764 1.302874 1.738379 # 4 0.4657082 0.9321142 1.399220 1.867026 # 5 0.4998122 1.0004283 1.501850 2.004080 # 6 0.5361091 1.0731432 1.611105 2.149996 # 7 0.5747165 1.1504960 1.727342 2.305256 # 8 0.6157644 1.2327491 1.850958 2.470394 # 9 0.6593768 1.3201530 1.982333 2.645922 # 10 0.7057024 1.4130080 2.121922 2.832450 # 11 0.7548656 1.5115656 2.270107 3.030496

2) can next matlab code done in r using base of operations plot or ggplot2?

# matlab code # t1, tv,, , wg1 defined in programme = 1:7, plot([t1(i),tv0(i)], [wg1(i),0],'g-') end

this how matlab plot phone call in r without loop:

plot(x1, y1, x2, y2)

i want give thanks in advance.

here how port for loop in idiomatic way:

phi <- seq(0.1, 0.4, 0.1) pg <- c(0.61165, 0.65709, 0.70599, 0.75808, 0.81355, 0.87258, 0.93536, 1.00210, 1.07300, 1.14830, 1.22820) p <- rep(phi, each=length(pg)) matrix(622 * p * pg/(patm - p * pg), nrow=length(pg))

no explicit looping needed.

r matlab for-loop

No comments:

Post a Comment