Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diamond.square.terrain #4

Open
jenessalemon opened this issue Nov 12, 2016 · 8 comments
Open

diamond.square.terrain #4

jenessalemon opened this issue Nov 12, 2016 · 8 comments

Comments

@jenessalemon
Copy link
Collaborator

@Wolflab
I am having a hard time with writing a function to go through the matrix applying the diamond and square step sequentially. Here is what I want to do in English:

  1. Apply the diamond and square step to the whole matrix (which I've done.)
  2. Somehow index each quadrant individually and apply the diamond and square step to those.
  3. Then I think I just want to keep getting quadrants of quadrants until the matrix is full. (Which means that I have to have a certain kind of matrix, not JUST an odd one, right?)

So step two is what I really need help with. I think that I know the basics of indexing a matrix (matrix[row, column]) but I don't know how to index a whole block of a matrix. @willpearse?

@willpearse
Copy link
Member

Looking good! You're getting better at writing things out first. Once you've written something out as you do above, try getting to the next level and writing something that looks a bit more like pseudo-code. A good way of telling if you've written pseudo-code is whether or not you use words like for or sapply in what you're writing. You're coming along leaps and bounds, though - well done!

Make sure you're applying the diamond step then the square step to all sub-matrices/quadrants.

Consider whether step 1 and step 2 are really different steps. In one you're applying to a particular sub-matrix (which just happens to be the whole matrix!) and in the second you're looping over a load of sub-matrices.

As far as indexing goes, you're already thinking about the right lines, so let me give you a hint. You are correct in that you know how to get out a single row and a single column (--> a single cell in your matrix). See if you can figure out how to do it using the example of how to do something similar in a vector below:

vector <- c("Will", "Sarah", "Jeni", "Barry")
toupper(vector[2])
toupper(vector[2:3])

...what do you reckon?...

@jenessalemon
Copy link
Collaborator Author

In order to apply diamond before square in every sub-matrix, can we just use a for loop?
Ex: for (i in crazy index)
sapply diamond step
sapply square step
I thought that R will execute the commands in order?

As far as indexing goes, I do know how to get chunks of a vector like that. It is, however, harder to work in 2D! I am not very good at programming so I decided to try a for loop. I'm sure there is a better way.
I wrote this little function to see if I could get the first quadrant and it was a fail.
quad <- function(matrix){
n <- length(matrix)
for (i in 1:median(n)){
4 <- matrix[i : median(n)]
}
}
quad(setup)

This is the error message:
Error in 4 <- matrix[i:median(n)] :
invalid (do_set) left-hand side to assignment
@willpearse I've never seen that before, can you help?

@willpearse
Copy link
Member

Look at what I've written (vector[2:3]), look at what you've written (matrix[row,col]) and see if you can find a way to join both of those things up. I cover this kinds of subsetting in the first lecture.

The thing with errors is to read them slowly and carefully. I'll help you break down this one.

  1. "Error in 4 <- matrix[i:median(n)] : " --> the line 4 <- matrix[i:median(n)] has an error in it. Read the line out loud to yourself. What do you think it does?
  2. "invalid (do_set) left-hand side to assignment". You probably don't know what the bit in parentheses means, but it's in parentheses so it mustn't be that important (else it wouldn't be in parentheses - that's just grammar :D). So the key phrase is "invalid left-hand side to assignment"
  3. Breaking that sentence down, there's clearly something wrong with the left hand side of your code. "assignment" probably refers to something being given a value, which sounds like the <- operator to me. So it's what's to the left of that.

I would imagine that's enough to get it...

@Wolflab
Copy link

Wolflab commented Nov 12, 2016

Will: You beat me to it by seconds. I was about to say that Jenessa can fix this one, especially your item 1) the error message.

@jenessalemon
Copy link
Collaborator Author

Can you index the index? Like as in:
matrix[row[1:median(n)], col[1:median(n)]] ?
If so I might have an idea. I figured out that the error message was because I was trying to assign 4 a value, when I needed to be assigning a cell to be 4. Sorry I am so slow at this.

@willpearse
Copy link
Member

I wouldn't call it indexing the index, but yes you can. Go back to your
handout and look at the first session on matrices.
On Sat, 12 Nov 2016 at 23:20, jenessalemon [email protected] wrote:

Can you index the index? Like as in:
matrix[row[1:median(n)], col[1:median(n)]] ?
If so I might have an idea. I figured out that the error message was
because I was trying to assign 4 a value, when I needed to be assigning a
cell to be 4. Sorry I am so slow at this.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#4 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABLcUoxOPo3-dEKyb456ahDFRQtpa5kyks5q9kmpgaJpZM4Kwhyl
.

@jenessalemon
Copy link
Collaborator Author

Ok, so matrix[1:3, 1:3] grabs the first quadrant! Now it's a matter of trying to apply both steps to all four quadrants, and all four of their sub quadrants, and so on, which I believe I am going to use expand.grid to use.

@jenessalemon
Copy link
Collaborator Author

It's my turn, I'm trying to call you on Skype

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants