简体   繁体   中英

Minimum Cost Distance in Matrix

I have got this matrix below

k
   [,1]  [,2]  [,3] ,4][,5] [,6]
 [1,] 1    4    9   16   25   36
 [2,] 1    3    7   13   21   31
 [3,] 2    2    5   10   17   26
 [4,] 4    2    4    8   14   22
 [5,] 7    3    3    6   11   18
 [6,] 11    5    3    5    9   15

and I want to loop through starting from k[1,1] and ending at k[6,6] . My looping criteria is based on min(k[i,j+1], k[i+1,j], k[i+1, j+1]) and the answer I hope to get is something like 1+1+2+2+3+3+5+9+15 = 41 (travelling through the minimum path)

So pretty much it calculates the minimum starting from k[1,1] and then continues downwards till k[6,6]

warpingDist = function(x, y, z){
mincal = numeric(length(k))
m = nrow(k)
n = ncol(k)
i=1
j=1
mincal = which(k == min(k[i, j+1], k[i+1, j], k[i+1, j+1]), arr.ind = TRUE)
indx = data.frame(mincal)
i= indx$row
j= indx$col
if(i != m || j!=n)
{
warpingDist(k[i, j+1], k[i+1, j], k[i+1, j+1])
}

warpSum = sum(mincal)
return(warpSum)
}
value = apply(k, c(1,2), warpingDist)
value

When I run this code it displays the below:

Error: object 'value' not found

Not sure why this is happening...

As you don't provide a minimal reproducible example, I can only guess:

warpingDist = function(x, y, z, k){
# browser() # This is a good option to activate, if you run your script in RStudio
...
return(warpSum)
}

# your code
k <- whatever it is
result <- warpingDist(x, y, z, k)

I hope that helps.

Am glad, I was finally able to solve the problem...The code runs fast as well

Problem: To find the minimum cost for a matrix. For clarity, let's assume I have the matrix given below:

[1,]    1    4    6    7    8    9    0
[2,]   10   12    1    3   11    2    0
[3,]   11   12    2    8   17    1    0
[4,]   20    1   18    4   28    1    0
[5,]    5   20   80    6    9    3    0

My goal is to add the minimum path distance starting from kata[1,1] first row to the last row K[5,4]. So effectively, I want to have something like 1 + 4 + 1 + 2 + 4 + 6 + 9 + 3.

Below is the R code which I have used to implement this. It implements two functions:

# Function that  calculates minimum of three values. Returns the Value.

minFUN <- function(Data, a, b){
d =  (min(Data[a, b+1], Data[a+1, b], Data[a+1, b+1]))
return(d)
       }
# Function that calculates the index of the minimum value, from which the
# The next iteration begins 
NextRC <- function(Data, a, b){
 d = min(Data[a, b+1], Data[a+1, b], Data[a+1, b+1])
     if(d == Data[a, b+1]){
         c = cbind(a, b+1)
         }else
 if(d == Data[a+1, b]){
         c = cbind(a+1, b)
    } else
 if(d == Data[a+1, b+1]){
         c = cbind(a+1, b+1)
        }

return(c)
}

Je <- c()
NewRow = 1
NewCol = 1
# Warping Function that uses both functions above to loop through the dataset


WarpDist <- function(Data, a = NewRow, b = NewCol){
    for(i in 1:4) {
       Je[i] = minFUN(Data, a, b)
   # Next Start Point
       NSP = NextRC(Data, a,b)
       NewRow = as.numeric(NSP[1,1])
   NewCol = as.numeric(NSP[1,2])    
       a = NewRow
   b = NewCol
}
return(Je)
}

Value=WarpDist(Data = Data, a = NewRow, b = NewCol)

warpo = Data[1,1] + sum(Value)

w = sqrt(warpo)

The result is the minimum path from the first row to the last row

 Value
[1] 4 1 2 4 6

The result omits 9 and 3 because its already on the last row.

Time:

Time difference of 0.08833408 secs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM