【问题标题】:How to plot multiple lines in a single chart with a function using nested For loop?如何使用嵌套 For 循环在单个图表中绘制多条线?
【发布时间】:2019-09-18 16:41:40
【问题描述】:

我想使用嵌套的 for 循环在单个图表中绘制多条线

h <- c(1,20,50,100,200,400)
d <- seq(20, 420, by=20)

我想计算不同 h 和 d 值的重量 w

# the formula is w =1/(1+(d^2/h^2))

for (i in 1:h) {
  for(j in 1:d) { 
    w <- 1/((1+j^2)/i^2)
    }
  }

我不知道该怎么做

我想要一个以 d 为 x 轴、w 为 y 轴的图表

【问题讨论】:

    标签: r for-loop


    【解决方案1】:

    我们可以使用outer

    m1 <- outer(h, d, FUN = function(x, y) 1/((1 + y^2)/x^2))
    matplot(t(m1), type = "l")
    

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2023-04-08
      • 2017-01-26
      相关资源
      最近更新 更多