Exceed matix dimensions when loading images in random order.

Hi,
I am relatively new to matlab but have been writing a code for a psych. experiment. I am trying to load in 72 positive images and 72 negative images in a random order to be used for 36 trials for 4 blocks. I was hoping that each 6 trials would contain 3 of each type. The images have been loaded into a mat file with no problems but when I try to generate a random order for the presentation of these I am given an error message that it exceeds the matrix dimensions.
Here is my code:
% Generate a randomised selection of images ensuring an equal proportion of each category (positive vs. negative) i.e. Each set of 6 trials
% contains 3 positive and 3 negative images in a randomised order.
% The images are loaded into the image array as follows:1-72 Positive, 17-32 Negative
Positive = 1:72;
Negative = 73:144;
Positive = Positive(randperm(72))';
Negative = Negative(randperm(72))';
d.AEimageOrder = zeros(36,4);
for block = 1:4
upper = block*4;
lower = upper-3;
imageSelection = [Positive(lower:upper); Negative(lower:upper)];
imageSelection = imageSelection(randperm(36));
d.AEimageOrder(:,block) = imageSelection;
end
This is the error message that I am receiving:
Index exceeds matrix dimensions. imageSelection = imageSelection(randperm(36));
Any help would be greatly appreciated!

Answers (1)

There is nothing that can potentially contain images in your code. You only have vectors of integers.
I have no idea what you're trying to do with the loop, but it is clear that it's not going to work. Since lower = upper-3, the vectors Positive(lower:upper) and Negative(lower:upper) are always going to be 4 elements each, so the concatenation is going to be 8 elements. On the next line, you ask to shuffle 36 elements, that's 28 too many.
Also note, that the maximum value for upper is 4x4 = 16, so you're never going to be using indices 17:72 of the shuffled Positive and Negative vectors. I'm sure that's not what you intended.
It would help if you explained the purpose of the loop. Maybe with a concrete example of input and desired output.

This question is closed.

Asked:

on 22 Oct 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!