本文介绍了如何在计算中使用小数设置条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个具有相同尺寸的栅格(值是带有 5 个小数的浮点数).下面给出的代码从两个栅格 r 和 r1 中生成一个文件.如果 r 较大,则放蓝色,否则放红色.

Having two rasters (values are floats with 5 decimals) with the same dimensions.The code given below makes one file out of two rasters r and r1. If r is bigger,put blue ,otherwise put red.

这段代码运行良好,但我被要求添加另一个条件.此代码的工作原理:

This code worked well but I was asked to add another condition.How this code works:

如果 r 为 0.229 且 r1 为 0.228,则 r 更大(注意小数点后第三位).我需要的是指定前两位小数,例如:

If r is 0.229 and r1 is 0.228 then r is bigger(notice third decimal).what I need is to specify the first two decimals for example:

     r= 0.228   r1=0.224   put  yellow colour(they are rather similar)
     r= 0.238   r1=0.224   put  blue colour(r is bigger)
     r= 0.128   r1=0.224   put  red colour(r is lower)

1- 读取第一个文件 r

1- to read the first file r

     conn <- file("C:\\corr.bin","rb")
     corr<- readBin(conn, numeric(), size=4,  n=1440*720, signed=TRUE)

2-读取第二个文件r1:

2- to read the second file r1:

    conne <- file("C:\\cor06.bin","rb")
    over<-readBin(conne, numeric(), size=4,  n=1440*720, signed=TRUE)

计算:

 r <-raster(t(matrix((data=corr), ncol=720, nrow=1440)))
 r1 <- raster(t(matrix((data=over), ncol=720, nrow=1440)))
 m <- r > r1 #Compare the two rasters
 image( m , col = c("blue" , "red" ) )

推荐答案

您现在想要根据一个大于另一个,或者两者之间的差异小于 0.01 来创建颜色.您可以简单地这样做:

You now want to create colours based on if one is greater than the other, or the difference between the two is less than 0.01. You can do this simply like this:

# Example data
r <- raster( system.file("external/test.grd", package="raster") )
r1 <- r * rnorm( ncell(r))

# Make new raster
m <- raster( r )

# 3 = Yellow , 2 = "Red" , 1 = "Blue"
values( m ) <- ifelse( abs( r[] - r1[] ) <= 0.01 , 3 , ifelse( r[] > r1[] , 1 , 2 ) )
image( m , col = c( 1 = "Blue" , 2 = "Red" , 3 = "Yellow" ) )

您可以在 col 中使用 value = "colour" 参数样式,确保将颜色分配到正确的级别.

You can use the value = "colour" style of argument in col ensuring colours are assigned to the right levels.

这篇关于如何在计算中使用小数设置条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:23