Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shuffle method for matrix #25526

Closed
sdmcallister opened this issue Jan 12, 2018 · 4 comments
Closed

shuffle method for matrix #25526

sdmcallister opened this issue Jan 12, 2018 · 4 comments

Comments

@sdmcallister
Copy link

I am wondering if it makes sense to add the following method?

m = [1 2 3;4 5 6]

Base.Random.shuffle(m) = reshape(shuffle(m[:]),size(m,1),size(m,2))

julia> shuffle(m)
2x3 Array{Int64,2}:
 3  6  2
 4  1  5

Thanks -Steve

@StefanKarpinski
Copy link
Member

Having both shuffle! and shuffle for arrays of arbitrary dimension makes sense to me. Of course, implementation for some matrix types are either tricky to do efficiently (e.g. sparse matrices – you don't want to select and insert O(m*n) zeros), or impossible (e.g. diagonal, banded, etc.).

@mcognetta
Copy link
Contributor

Assuming you want to preserve structural properties of the matrix (banded, sparse [this is sort of meaningless here though], symmetric,...), I don't see why these should be impossible.

For banded matrices, we know the bandwidth so we can just treat this as a random shuffle of band arrays concatenated together, the shuffle for symmetric and upper-diagonal matrices also seems to be the same although we have to take care with elements in the main diagonal. I do see how sparse matrices would be tricky to implement efficiently though.

Anyway, I think this should be doable for any (reasonable) agreed upon definition of shuffling for the special matrix types.

@StefanKarpinski
Copy link
Member

StefanKarpinski commented Feb 6, 2018

I think shuffling the data only in the structural nonzeros is a somewhat surprising behavior for these generic functions. Sure, that's the only thing you can do for some matrix types, but I think that operation deserves its own function, maybe sufflenz!(A) which would degenerate to shuffle! when A is dense, but would do quite different things on structured matrices. The shuffle! function should raise an error if called on a matrix type that cannot arbitrarily move nonzeros around. Note that an efficient shuffle! for SparseMatrixCSC is entirely possible but will want to shuffle values and resample indices in various ways.

@laborg
Copy link
Contributor

laborg commented Feb 21, 2022

shuffle[!] has been added for multidimensional arrays in #22226
If really needed, the idea of shufflenz should probably be tracked in a specific issue.

@laborg laborg closed this as completed Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants