-1

I have a matrix that holds location information by coordinates(e.g. (1,1)) of a 38*35 matrix.

##(1,1) (1,2) (1,3) (1,4) .... (1,38)
##(2,1) ....
##....
##(35,38) 

I have created each point into a vector array, so that (1,1)=1, (1,2)=2, ... (2,1)=39, (2,2)=40, .... (35, 38)=1330 and so on.

#1
#2
#3
#4
#5
#6
#7
#8
...
#1330

The plan is to put in prediction data point P and find out the Euclydian distance between P and the actual data X. It is no problem until both points are on the same row (the distance would equal the subtraction of the two numbers), but if the two data I'm comparing fall on different rows, it becomes a problem.

For instance, " would actually mean (2,2) and I cannot subtract anymore.

I've been trying creating if statements, but it seems to be getting complicated.

#original data: x, predicted data:p
if(p<39){
 distance <- abs(p-x)
 } else if(x<77){
 distance <- (sqrt(1)^2+((p-38)^2)
 }
Eslie
  • 23
  • 3

1 Answers1

0

Try giving us a reproducible example so we can better help you, by using the function dput() on your dataset and uploading the output onto the forum. Check out this link: How to make a great R reproducible example

It doesn't seem like your loops are doing what you want.. Can you try running this:

if(p<39){
 distance <- abs(p-x)
} else if(x<77){
 distance <- (sqrt(1)^2+((p-38)^2)
}

Your loop doesn't actually change the value of distance, rather just prints statements. Also, what if p>39 and x>77? If this is possible you would need a final else statement saying what you would do in this instance.

Then again, if you would post a reproducible example I would be of more help!

h3ab74
  • 318
  • 3
  • 16