3
$\begingroup$

I have generated a matrix as follows:

   Range[41, 50], Range[51, 60], Range[61, 70], Range[71, 80], 
   Range[81, 90], Range[91, 100]]) // MatrixForm

I was wondering how I could select ten items from this matrix so that no two numbers are from the same row or the same column. Thank you.

$\endgroup$
1
  • 3
    $\begingroup$ Your matrix has only 6 rows. Therefore, it is not possible to choose 10 numbers that have no row in common. On the other hand, if you have a 10x10 matrix, the 10 diagonal elements have no row or column in common. $\endgroup$ Oct 7, 2022 at 20:35

3 Answers 3

3
$\begingroup$

Your data isn't well formed, so I made an assumption:

theMatrix = ArrayReshape[Range[100], {10, 10}]

Choosing any item from each row satisfies the unique row constraint. Satisfying the column constraint means that, as we choose items from each row, we must use each index in the range 1..n exactly once. So, we can create the column indices like this:

theIndices = RandomSample[Range@10, 10]

Now, we just need to take the items theMatrix[[1,theIndices[[1]]]], theMatrix[[2,theIndices[[2]]], etc. One straightforward way to do that would be with MapThread:

MapThread[Take, {theMatrix, List /@ theIndices}]

You could also build up the 2-d indices first and use Extract:

thePositions = Transpose[{Range@10, RandomSample[Range@10, 10]}];
Extract[theMatrix, thePositions]

I don't know what format you want your result in.

NOTE

user293787's answer reminded me that you don't need to specify the second argument in RandomSample if you want a random permutation of the list. So, all of my RandomSample[Range@10, 10] above could be replaced with RandomSample[Range@10].

$\endgroup$
2
$\begingroup$

Without constructing the matrix first, one could use:

10*Range[0,9]+RandomSample[Range[10]]
$\endgroup$
0
$\begingroup$
SeedRandom[1];
arr = RandomChoice[Alphabet[], {10, 10}];

g1 = arr // Grid

$$ \begin{array}{cccccccccc} \text{f} & \text{a} & \text{h} & \text{a} & \text{c} & \text{d} & \text{a} & \text{v} & \text{a} & \text{q} \\ \text{x} & \text{o} & \text{d} & \text{i} & \text{t} & \text{f} & \text{s} & \text{q} & \text{m} & \text{a} \\ \text{t} & \text{e} & \text{w} & \text{v} & \text{h} & \text{d} & \text{a} & \text{e} & \text{u} & \text{y} \\ \text{d} & \text{f} & \text{m} & \text{t} & \text{v} & \text{i} & \text{v} & \text{y} & \text{l} & \text{z} \\ \text{z} & \text{c} & \text{d} & \text{k} & \text{e} & \text{c} & \text{x} & \text{r} & \text{l} & \text{p} \\ \text{g} & \text{m} & \text{k} & \text{c} & \text{w} & \text{k} & \text{e} & \text{s} & \text{m} & \text{o} \\ \text{z} & \text{m} & \text{e} & \text{l} & \text{m} & \text{c} & \text{f} & \text{u} & \text{g} & \text{k} \\ \text{y} & \text{f} & \text{h} & \text{s} & \text{f} & \text{c} & \text{k} & \text{o} & \text{z} & \text{v} \\ \text{e} & \text{p} & \text{r} & \text{e} & \text{w} & \text{i} & \text{g} & \text{y} & \text{d} & \text{u} \\ \text{n} & \text{d} & \text{b} & \text{r} & \text{u} & \text{d} & \text{l} & \text{z} & \text{i} & \text{f} \\ \end{array} $$

First generate the indices using distinct numbers:

r1 = RandomSample[Range[10]]
r2 = RandomSample[Range[10]]
idx = Transpose[{r1, r2}]

{{3,9},{7,1},{9,4},{1,7},{5,8},{2,5},{6,2},{4,3},{10,6},{8,10}}

To visualize:

cdata = ColorData["Rainbow"][#/10] & /@ Range[10]
vis = Thread[idx -> Thread[List[Bold, 20, cdata]]]
g2 = Grid[arr, ItemStyle -> {Automatic, Automatic, vis}]

enter image description here

To select:

Extract[arr, idx]

{"u", "z", "e", "a", "r", "t", "m", "m", "d", "v"}

$\endgroup$

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.