MATLAB Answers

How can I randomize the order of a matrix of images

2 views (last 30 days)
Beyza Kural
Beyza Kural on 2 Aug 2020
Commented: Beyza Kural on 2 Aug 2020
I have a 2x2 matrix (four images total, same size) that I need to randomize each time the code is ran. I tried using randperm but I can't seem to get the randomized numbers (1-4) to be indexed to the image file names.


Beyza Kural
Beyza Kural on 2 Aug 2020
IMAGESTORE = {queen,stark,hound,imp} %names of the images (queen,stark,hound,imp)
col= size(IMAGESTORE,2)
random = randperm(col)
new = IMAGESTORE(:,random)
the variable new gives me the dimensions on the images, not the actual images.

Sign in to comment.

Answers (1)

madhan ravi
madhan ravi on 2 Aug 2020
Edited: madhan ravi on 2 Aug 2020
Filenames = compose('image%d.png', 1:4);
Filenames(randperm(numel(Filenames))) % ?


Show 4 older comments
Image Analyst
Image Analyst on 2 Aug 2020
random is a built-in function. I'd call it something different, like sortOrder:
IMAGESTORE = {queen,stark,hound,imp} %names of the images (queen,stark,hound,imp)
numImages = length(IMAGESTORE)
sortOrder = randperm(numImages) % 1 to 4 in a scrambled order.
for k = 1 : numImages
% Get image array from the cell array.
thisImage = IMAGESTORE{sortOrder(k)};
% Display image.
% Now do other stuff with thisImage.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by