如何显示4幅图像的所有可能组合?

时间:2017-04-21 13:50:26

标签: matlab combinatorics matlab-guide

我试图向一些学生解释MatLab中的概率,并希望首先在现实生活中展示概率。我有四个图像,两个红球,一个蓝色和一个黄色,所有4个在Matlab中作为图像(1)/(2)/(3)和(4)。如何制作2×2网格并显示4幅图像的所有可能的布局?

谢谢!

1 个答案:

答案 0 :(得分:0)

这意味着4! (= 24)安排。

%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
相关问题