Matlab:我怎样才能减少这个程序的执行时间?

时间:2014-07-15 19:49:48

标签: matlab

如何减少此程序的执行时间:

h=50;
w=10000;
output=zeros(h,w);
i=1;
while i<=w
    temp=randsample(numel(prob),h,true,prob);
    check=all(any(ismember(vec,temp)));
    if check~=0
        output(:,i)=temp;
        i=i+1;
    end
end

vec8 x 100矩阵。

该程序的执行时间为30小时!!

这是我question的解决方案(h=40和vec a 4x20矩阵)。

我的vec

vec(:,1)=[6 77 2 91 25 25 25 25];
vec(:,2)=[94 14 9 11 11 11 11 11];
vec(:,3)=[6 2 71 52 40 40 40 40];
vec(:,4)=[54 3 93 67 99 84 84 84];
vec(:,5)=[76 90 13 67 65 65 65 65];
vec(:,6)=[76 4 90 65 64 19 19 19];
vec(:,7)=[20 61 4 94 24 24 24 24];
vec(:,8)=[7 8 81 76 76 76 76 76];
vec(:,9)=[76 41 11 90 94 39 39 39];
vec(:,10)=[61 13 54 38 94 94 94 94];
vec(:,11)=[7 8 52 93 19 19 19 19];
vec(:,12)=[18 11 76 78 19 50 94 94];
vec(:,13)=[41 67 92 93 79 3 3 3];
vec(:,14)=[72 6 10 77 33 75 25 25];
vec(:,15)=[17 15 94 18 50 50 50 50];
vec(:,16)=[7 8 71 72 75 75 75 75];
vec(:,17)=[36 35 79 24 24 24 24 24];
vec(:,18)=[36 35 64 19 19 19 19 19];
vec(:,19)=[36 35 99 3 94 94 94 94];
vec(:,20)=[21 11 55 50 18 18 18 18];
vec(:,21)=[37 22 41 67 18 50 50 50];
vec(:,22)=[7 37 81 19 64 64 64 64];
vec(:,23)=[8 38 37 81 80 91 91 91];
vec(:,24)=[40 75 6 33 33 33 33 33];
vec(:,25)=[82 54 13 13 13 13 13 13];
vec(:,26)=[33 2 6 40 40 40 40 40];
vec(:,27)=[15 18 82 65 65 65 65 65];
vec(:,28)=[82 99 67 54 88 3 87 87];
vec(:,29)=[93 67 24 50 99 59 84 84];
vec(:,30)=[61 80 81 91 38 20 20 20];
vec(:,31)=[58 80 81 20 38 52 52 52];
vec(:,32)=[12 11 82 98 19 34 34 34];
vec(:,33)=[85 86 82 64 19 19 19 19];
vec(:,34)=[39 35 37 82 94 94 94 94];
vec(:,35)=[31 71 13 2 2 2 2 2];
vec(:,36)=[6 89 2 91 81 78 98 25];
vec(:,37)=[26 27 18 18 18 18 18 18];
vec(:,38)=[30 39 93 54 54 54 54 54];
vec(:,39)=[28 67 87 87 87 87 87 87];
vec(:,40)=[27 28 29 32 32 32 32 32];
vec(:,41)=[29 34 82 82 82 82 82 82];
vec(:,42)=[13 27 93 93 93 93 93 93];
vec(:,43)=[78 25 28 91 91 91 91 91];
vec(:,44)=[7 8 81 18 18 18 18 18];
vec(:,45)=[7 8 81 93 93 93 93 93];
vec(:,46)=[45 46 99 22 22 22 22 22];
vec(:,47)=[94 21 12 34 34 34 34 34];
vec(:,48)=[24 15 28 59 59 59 59 59];
vec(:,49)=[16 41 11 55 92 29 29 29];
vec(:,50)=[78 33 40 75 25 25 25 25];
vec(:,51)=[25 6 2 33 3 3 3 3];
vec(:,52)=[80 81 32 7 52 52 52 52];
vec(:,53)=[58 67 38 61 61 61 61 61];
vec(:,54)=[43 29 21 21 21 21 21 21];
vec(:,55)=[7 77 102 102 102 102 102 102];
vec(:,56)=[8 52 68 2 2 2 2 2];
vec(:,57)=[45 44 101 101 101 101 101 101];
vec(:,58)=[103 19 4 4 4 4 4 4];
vec(:,59)=[27 45 61 61 61 61 61 61];
vec(:,60)=[46 71 78 75 75 75 75 75];
vec(:,61)=[103 19 58 80 80 80 80 80];
vec(:,62)=[27 45 76 87 87 87 87 87];
vec(:,63)=[23 91 91 91 91 91 91 91];
vec(:,64)=[27 45 64 64 64 64 64 64];
vec(:,65)=[44 26 5 5 5 5 5 5];
vec(:,66)=[46 71 2 89 89 89 89 89];
vec(:,67)=[20 32 64 19 19 19 19 19];
vec(:,68)=[46 31 87 72 72 72 72 72];
vec(:,69)=[43 64 65 19 19 19 19 19];
vec(:,70)=[43 5 65 19 19 19 19 19];
vec(:,71)=[101 91 31 31 31 31 31 31];
vec(:,72)=[33 81 80 25 25 25 25 25];
vec(:,73)=[33 102 68 25 25 25 25 25];
vec(:,74)=[2 71 102 102 102 102 102 102];
vec(:,75)=[101 2 6 68 68 68 68 68];
vec(:,76)=[70 42 68 102 102 102 102 102];
vec(:,77)=[68 7 52 31 31 31 31 31];
vec(:,78)=[58 81 45 20 20 20 20 20];
vec(:,79)=[44 26 82 82 82 82 82 82];
vec(:,80)=[86 14 24 82 82 82 82 82];
vec(:,81)=[54 11 44 43 94 94 94 94];
vec(:,82)=[37 27 4 4 4 4 4 4];
vec(:,83)=[37 4 92 25 25 25 25 25];
vec(:,84)=[98 93 45 31 31 31 31 31];
vec(:,85)=[88 15 26 76 87 87 87 87];
vec(:,86)=[99 93 45 59 31 31 31 31];
vec(:,87)=[88 91 76 54 41 84 94 50];
vec(:,88)=[62 30 37 37 37 37 37 37];
vec(:,89)=[62 30 24 24 24 24 24 24];
vec(:,90)=[14 41 88 98 98 98 98 98];
vec(:,91)=[95 82 3 3 3 3 3 3];
vec(:,92)=[98 95 62 34 100 93 93 93];
vec(:,93)=[88 82 41 50 50 50 50 50];
vec(:,94)=[88 37 82 90 90 90 90 90];
vec(:,95)=[17 79 52 6 71 71 71 71];
vec(:,96)=[46 4 12 11 11 11 11 11];
vec(:,97)=[105 19 14 45 45 45 45 45];
vec(:,98)=[1 97 60 60 60 60 60 60];
vec(:,99)=[104 48 97 22 22 22 22 22];
vec(:,100)=[96 79 15 18 18 18 18 18];

1 个答案:

答案 0 :(得分:0)

试试这段代码:

h=50;
w=10000;
output = reshape( randsample( numel(prob), h*w, true, prob ), [h w] );
check=zeros(w,1);
for i=1:w
    check(i)=all(any(ismember(vec,output(:,i))));
end

ind=find(check==0);
j=1;
while j<=length(ind)
    temp=randsample(numel(prob),h,true,prob);
    check_2=all(any(ismember(vec,temp)));
    if check_2~=0
        output(:,ind(j))=temp;
        j=j+1;
    end
end

而不是生成所有列的while循环,这次我先创建矩阵,然后尝试替换未通过约束的列。我尝试时已在~4 seconds完成此代码。