"... I have to do all these operations in one line in the max function"
Well, no, not really -- that's a restriction you've placed on yourself. You can certainly do whatever operations you need to build the result.
But, if I understand that you simply want the unique set of elements of some specific columns in an array where the columns are specified by a logical addressing array, then that's simple enough to do generically regardless of how many elements are True in the logical vector--and, you get the single column for free...
unique will return the sorted list as a column vector from the subset array selected from K by S
in general, if there is a reason you need a given orientation within an operation, you can use either the directional catenation functions Matt illustrated above or reshape --
You'll note the only difference is the order of the shape arguments to reshape and the use of the empty brackets as syntax for the number of elements in the first argument if unknown.
The other ML idiom you'll see often altho it takes a separate variable to apply it to is the colon operator by itself in the addressing parens--that is syntax to turn any array of any size/number of dimensions into a single column vector.
the last syntax is often used in passing arguments or in preprocessing inside functions for cases where the orientation of the input may not be known but is critical for the processing steps to follow so just turns the input into a known shape.