-1

I am trying to explain probability in MatLab to some students and want to first demonstrate probability in real life examples. I have four images, two red balls, one blue and a yellow, all 4 in Matlab as image(1)/(2)/(3) and (4). How can I make 2 by 2 grids and display all possible arrangments of the 4 images?

Thank You!

Beembo
  • 307
  • 2
  • 13

1 Answers1

0

That means 4! (=24) arrangements.

%I just generate some images to show the example
images{1}=ones(5,5,3);images{2}=ones(5,5,3);images{2}(:,:,1)=0;
images{3}=ones(5,5,3);images{3}(:,:,2)=0;images{4}=ones(5,5,3);images{4}(:,:,3)=0;
%code starts here
m=[1 1 2 3];
v=perms(m);
%remove doubles
for ct = size(v,1):-1:2
    if any(sum((v(1:ct-1,:)-v(ct,:))==0,2)==4)
        v(ct,:)=[];
    end
end
%plot in 2x2 figure
for ct = 1:size(v,1)
    figure(ct)
    for i = 1:4
        subplot(2,2,i);imagesc(images{v(ct,i)});
    end
end
%to close all images type "close all" in the console
Gelliant
  • 1,835
  • 1
  • 11
  • 23