Search code examples
rmatrixfractals

Overlaying a matrix to diamond square algorithm output


This is a part repeat of a question I asked a couple of days ago, but as has been pointed out I did so extremely poorly, so I'm sorry for that, I'm still learning how to minimalise everything. So I'll ask the two parts separately as it might make it easier for others to find the answers, and to actually answer in the first place (I hope).

I've used a diamond square algorithm (DSQA) I found from this website, which has the following output image DSQA Output

What I need to do is overlay a matrix to this output, which will then be populated with "species" - creating an ecosystem with species occupying different levels of the "terrain". Eventually I'd like to create a range for the "species", but for now I'd just like to know how to overlay the matrix in such a way that the species would populate different levels (e.g. a species at a "high/orange" location would have different co-ordinates(?) to one at a "lower/green"

The matrix I create looks something like this:

#Create Species Vector
species.v<-letters[1:5]
species.v<-as.character(species.v)
#Check species Vector
species.v
#Immigration Vector
immigration.lower<-letters[1:26]
immigration.vec<-toupper(immigration.lower)
immigration.vec

#Matrix creation (Random)
orig.neutral<- matrix(sample(species.v,25,replace=TRUE),
                      nrow=5,
                      ncol=5)

#Neutral Matrix
neutral.v0<-orig.neutral

#Create dice roll for replacement
dice.vector<-c(1:10)
dice.vector

#For loop and Ifs for replacement/immigration/speciation
for (i in 1:100) {{dice.roll<-sample(dice.vector,1)}###For Loop with IF functions
  if(dice.roll <= 7) {
    neutral.v0[sample(length(neutral.v0),1)]<-as.character(sample(neutral.v0,1))
  } else if (dice.roll > 7 & dice.roll < 10){
    neutral.v0[sample(length(neutral.v0),1)]<-as.character(sample(immigration.vec,1))
  } else if (dice.roll == 10){
    elIdx = sample(length(neutral.v0),1) #index of a randomly selected element
    neutral.v0[elIdx] = paste(neutral.v0[elIdx], "2", sep="")
  }}

The replacement and such is all part of a future ecosystem code, and will eventually have a check for the species input into the matrix being in the correct "range" of the DSQA output.

But what I need to know is how to overlay/merge/create this matrix with the DSQA output, so that matrix is part of the output. There don't need to be any limiting ranges at present, I just can't conceptualise how to merge these two separate pieces of code into one thing that I can work on.

So in the example, my matrix is only 5x5, but I have no idea how to create/specify the size of the DSQA Output, let alone ensure my matrix is part of it/effected by it. I don't know if I've got too high a density DSQA output for a simple 5x5 matrix maybe? The actual matrix I'm using in my project is 1000x1000 but that's unnecessarily large for an example, as was pointed out by @gregor, as I just need to know the concept of how to do this, which I can then apply to my ridiculously sized matrix/DSQA output.

I'm still not sure I've explained it well, but any help would be appreciated.


Solution

  • I found out what I was doing wrong, which was instead of taking the output of the diamond square algorithm as a matrix of values, I was trying to think about overlaying a different matrix over the top. The long and short was that I only have to refer to the diamond square algorithm output matrix, not directly use it as a "base" for a second higher matrix.