3
$\begingroup$

Say I have the following matrix

$\left( \begin{array}{cccccc} 1. & 0 & 0 & 0 & 0 & 0 \\ 0 & 0.707107 & 0 & 0 & 0 & 0.707107 \\ 0 & 0.707107 & 0 & 0 & 0 & 0.707107 \\ 0 & 0 & 0.707107 & 0 & 0.707107 & 0 \\ 0 & 0 & 0.707107 & 0 & 0.707107 & 0 \\ 0 & 0 & 0 & 1. & 0 & 0 \\ \end{array} \right)$

In code this would be

{{1., 0, 0, 0, 0, 0}, {0, 0.707107, 0, 0, 0, 0.707107}, {0, 0.707107, 
  0, 0, 0, 0.707107}, {0, 0, 0.707107, 0, 0.707107, 0}, {0, 0, 
  0.707107, 0, 0.707107, 0}, {0, 0, 0, 1., 0, 0}}

Now, what I'm looking for is a way to create a vector of what the index is of the maximum of each row. So for the first row this would be 1, but for the second two and six are both the maximum. The same issue occurs on the third row. What I'd like is that the vector generated by this matrix would be {1,2,6,3,5,4} (although {1,6,2,5,3,4} also works). In words, it should look for the index of the maximal item of each row, but if a maximum has already been found at that index, it should use the index of the other, degenerate, maximum.

Now, personally, I think this is probably not possible. I can't think of where to even start with this, but I figured I might as well get a second opinion on the feasibility. The way I'm doing it now is just creating the vector manually from the above matrix, but this is very tedious as I need to generate many of these matrices for different parameter values.

Perhaps not important to the question, but some background: what I'm doing is taking eigenvectors of a translation invariant system from real space to k-space, and then looking for the k-value that maximizes the transform. This in turn allows me to construct the dispersion curve of my system. Now, due to the translation invariance and having equal parameter values of all components of my system, there are degeneracies, which is expected physically. However, later I investigate what happens when the translation invariance is broken at different sites, which lifts some of the degeneracies while leaving others in place.

$\endgroup$
3
  • 2
    $\begingroup$ Does DeleteDuplicates[Flatten[Position[#, Max[#]] & /@ matrix]] suit your needs? $\endgroup$ Jun 7, 2015 at 16:31
  • $\begingroup$ Does indeed seem to do the trick! $\endgroup$
    – user129412
    Jun 7, 2015 at 16:46
  • $\begingroup$ Okay, would you mind answering your question, please? $\endgroup$ Jun 7, 2015 at 17:21

1 Answer 1

3
$\begingroup$

As commented by Guess who it is, this can be done using the expression

DeleteDuplicates[Flatten[Position[#, Max[#]] & /@ matrix]]

This first finds the index of all maxima (including degenerate ones) and then removes any duplicate entries. This is indeed what I was looking for in my original question.

$\endgroup$
1
  • $\begingroup$ Thank you for taking the time to write up the solution to your own problem! (+1) $\endgroup$
    – MarcoB
    Jun 9, 2015 at 0:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.