0

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?

5
  • 1
    For better performance it might be better to use a view or to code it up explicitly with a for loop Commented Oct 17, 2017 at 15:00
  • You think so? Worth testing in practice. Commented Oct 17, 2017 at 15:01
  • A[i,:] creates a new vector. Right? That's wasteful. Commented Oct 17, 2017 at 15:02
  • 1
    Sure but sometimes small views aren't fast either. Let me try it out. Commented Oct 17, 2017 at 15:02
  • You're right, the view is always faster. I can never work out what the actual rules are when slicing is faster, I thought that was for small arrays. Commented Oct 17, 2017 at 15:06

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.