James Burgess
James Burgess

Reputation: 497

Slicing Multidimensional Array by a variable

I am writing a method which accepts a two dimensional array of doubles and an int row number as parameters and returns the highest value of the elements in the given row.

it looks like this:

function getHighestInRow(A, i)
        return(maximum(A[:i,:]))
end

the issue i am having is when i slice the array with

A[:i,:]

I get an argument error because the :i makes i get treated differently.

the code works in the other direction with

A[:,i,:]

Is there a way to escape the colon? so that i gets treated as a variable after a colon?

Upvotes: 0

Views: 94

Answers (1)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

You're doing something strange with the colon. In this case you're using the symbol :i not the value of i. Just getHighestInRow(A,i) = maximum(A[i,:]) should work.

Edit: As Dan Getz said in the comment on the question, getHighestInRow(A,i) = maximum(@view A[i,:]) is more efficient, though, as the slicing will allocate a temporary unnecessary array.

Upvotes: 3

Related Questions