Selecting random cells in a matrix without repetition

3 views (last 30 days)
Lorson Blair
Lorson Blair on 1 Oct 2021
Commented: Lorson Blair on 1 Oct 2021
Good day guys. This may be a simple question, but I cannot seem to find an answer on here for it. I am trying to select random cells in a matrix without repeating the cells. For example. I have a 10x10 matrix, and I'm trying to select 20 cells randomly without repeating the cells. Any help will be appreciated.

Accepted Answer

dpb on 1 Oct 2021
Edited: dpb on 1 Oct 2021
Stupid auto-complete put the silly parens in instead of letting me when/where story and I'm stickin' to it!!! :)
As far as the comment re: positions, just wrap the indices when generate them if those are the desired return values instead of the values.
In some ways this is less useful than the linear indices; you'll need arrayfun or similar construct to pull the elements that way as MATLAB will expand the two vectors to all combinations of each instead of just accessing the two vector elements pairwise.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!