How to exchange elements random in the same row of the matrix list

jospercomp shared this question 3 years ago
Answered

How to exchange elements random in the same row of the matrix list given the number of exchanges for each of the rows in the list.

Maybe need nested sequence, randombetween, length and element command, but I dont understand how swap/exchange.

Best Answer
photo

Thank you. I solved the problem by adding the shuffle command

==

Sequence(Shuffle(Sequence(If(i ≤ cantV, 1, 0), i, 1, n, 1)), j, 1, n, 1)

1d7e9d682f3c48f2a12f5df06a0b169d

Comments (2)

photo
2

A tricky solution with the help of sorting.

l1

for 10 rows, 4 indices (column number) per row are determined with a random number

l2

The 4 indices from l1 are randomly mixed.

l3

Forms the indices in a sequence from 1 to 10. If, however, the current index occurs in the sequence in a certain position of l1, so the index from l2 is taken over with the same position.

l3 is the help list for sorting. It can be interpreted as follows:

The first value (in one line) of m1 receives as new position the first value from l3.

The second value from m1 receives as new position the second value from l3

etc.

I have changed the values in m1 so that they are not confused with the indices.

The result is formed in m2.

-------------------------------

Alternative:

Realize the whole thing as a script.

there is the command SetValue( <List>, <Number>, <Object> )

However, this would require the matrix to be converted into a one-dimensional list

and there is no decent iteration in GGB script. Substitute for this is Execute(< with iteration generated commands>)

In the end it probably will be even more tricky than solution above

photo
1

Thank you. I solved the problem by adding the shuffle command

==

Sequence(Shuffle(Sequence(If(i ≤ cantV, 1, 0), i, 1, n, 1)), j, 1, n, 1)

1d7e9d682f3c48f2a12f5df06a0b169d

© 2023 International GeoGebra Institute