MATLAB: Swapping elements in matlab swapping elements How would I swap elements randomly in matlab? E.G I have a matrix M=Randi(3,10); and I want to swap its elements/cells around randomly. Best Answer M=randi(3,10);M can then be linear indexed by the set [1,30] since there are 30 elements. So you could get a RANDom PERMnutation of that by using RANDPERM.idx = randperm(30)ThenMnew = M([idx]);and shape up MnewMnew = reshape(Mnew,3,10) Related SolutionsMATLAB: Rearrange matrix elements randomly A_new = reshape(A(randperm(numel(A))), size(A)) MATLAB: How to sort a vector in a random manner in MATLAB To sort the elements of a vector randomly you can use the RANDPERM() function.RANDPERM(n) returns a random permutation of the integers 1:n.a = [1 2 3 4 5];a_rand = a(randperm(length(a))); Related QuestionSelect some elements from a given matrixValue swappingHow to generate an unsorted permutation of integers in MATLAB 7.7 (R2008b)Swap matrix row randomlyHow to find the nine biggest numbers
Best Answer