MATLAB >> Array shuffle

by Yumnam Kirani Singh » Wed, 19 Nov 2008 11:59:31 GMT

It is not clear in what way or order you want to shuffle the elements in a matrix. I do not know what you are willing to say by the phrase " numbers inside the matrix are only suffled". What are the numbers inside the matrix and what are outside the matrix?

MATLAB >> Array shuffle

by Hani H » Wed, 19 Nov 2008 15:16:06 GMT


If i wanted to shuffle a matrix (i.e.: m=[1 2 3 4 8 9]), how can i do it such that the numbers inside the matrix are only shuffled? (i.e.: result of shuffling m=[2 3 1 8 4 9])?

MATLAB >> Array shuffle

by Hani H » Wed, 19 Nov 2008 17:25:02 GMT

What i mean is i want to shuffle the elements inside the matrix randomly. I know the function randperm takes only a scalar quantity to generate a random matrix with the length of the input, but what if the numbers are not in series and not continuous (i.e.: m=[1 2 5 6 19])?

Appreciate your help Singh :)

MATLAB >> Array shuffle

by Wolfgang Schwanghart » Wed, 19 Nov 2008 17:42:01 GMT

Well, your array A has n elements, so you use randperm to randomly index your array A.

ix = randperm(n);
Ashuffled = A(ix);

HTH,
Wolfgang

MATLAB >> Array shuffle

by Hani H » Wed, 19 Nov 2008 19:56:02 GMT

Wolfgang...Cheers...Worked...:)
Thanks a million..its A(randperm(length(A)))

MATLAB >> Array shuffle

by Steven Lord » Wed, 19 Nov 2008 22:38:17 GMT


That's assuming A is a vector. For a matrix or N-D array, you'll want
A(randperm(numel(A)) instead.


A = reshape(1:12, 3, 4);

% B only contains the first 4 elements from A
% since length(A) == max(size(A)) == 4
B = A(randperm(length(A)))

% C contains all 12 of A's elements
C = A(randperm(numel(A)))


--
Steve Lord
XXXX@XXXXX.COM

Similar Threads

1. Shuffling imaginary compenents of an image

Hi!

I'm a bit stuck on code and i was hoping for some help, I'm writing a simple script (at least it was meant tobe simple!) to shuffle the imaginary components of an image and then reconstruct it. The problem at the moment is that when i ifft2 the image it doesn't look as expected (a layer of the image seems to be inverted and that's it). I think i need to apply the same shuffle to each quadrant of the fourier image but as i'm a bit new to matlab i'm not sure how to do this. Any help would be welcomed

Best
Josh

%% script so far

I = imread('orig_pic.jpg');
F = fftshift(fft2(I));
shuffle_pic = shuffle(imag(F)); %here i'm using a shuffle code i found on the web
J = complex(real(F),shuffle_pic);
K = ifft2(J);
subplot(1,2,1), image(J), title('orig image);
subplot(1,2,2),image(K), title('shuffle image');

%%shuffle function

function x = shuffle(x)
%SHUFFLE  Shuffle an array of values.
%   x = shuffle(x) shuffles the vector x in random order.

%   Since this function changes the vector x,
%   it creates a copy of it.

n = size(x);
m = n(1)*n(2)*n(3);

for index = 1:m

    % a random number between i and n
    r = index + floor((m-index+1) * rand());
    
    % swap elements index and r
    x([index r]) = x([r index]);
end

2. shuffle a matrix? - MATLAB

3. randomly shuffled vector

Hi,

      I have a vector of very long length (more than 100,000). Is there a 
quick way to randomly shuffled all of the elements in the vector. Thank you 
very much.

Jesse J.


4. Shuffling a Matrix - MATLAB

5. A fun puzzle: the prefect shuffle

The object of the puzzle is to come up with the shortest expression
that does a perfect shuffle on a vector.

So, if the given vector is x wokout the shortest algorithm to implement
ps(), where:

>> x = [1:5 10:10:50];
>> ps(x)   % the perfect shuffle

ans =

     1    10     2    20     3    30     4    40     5    50

The length of x can of course be odd.

/reza.

6. Shuffled Autocorrelation?? - MATLAB

7. Shuffle matrices

"Brett Shoelson" < XXXX@XXXXX.COM > wrote in
news:w%Dac.12$ XXXX@XXXXX.COM : 

> 
> "Equinox1248" < XXXX@XXXXX.COM > wrote in message
> news: XXXX@XXXXX.COM ...
>> Is there a simple way to shuffle the matrices without going through
>> for loops? What I want to do is as follows
>>
>> example:
>> x = [1 2
>>      3 4]
>>
>> y = [5 6
>>      7 8]
>>
>> z = [ 1 2
>>       5 6
>>       3 4
>>       7 8]
>>
>> thank you in advance
> 
> Interesting little problem with many solutions...
> The easiest, I think, is to first construct a single matrix, then
> alternate the indices:
> 
>>> z = [x;y];
>>> z = [z(1:2:end,:);z(2:2:end,:)]
> 
> z =
> 
>      1     2
>      5     6
>      3     4
>      7     8
> 
> Cheers,
> Brett
> 
> 

Indeed an interesting little problem. Another solution:

z = reshape([x' ; y'],2,4)' ;

Jos

-- 
Email: jnvdg AT arcor DOT de

8. MatLab 7 shuffle command - MATLAB