北太天元的perms給出所有排列的函數(shù)幫助我確認(rèn)高中數(shù)學(xué)教輔答案的錯(cuò)誤

% 7 個(gè)人進(jìn)行了編號(hào),1號(hào)和2號(hào)是新冠患者,3,4,5,6,7是健康的
%
a = 1:7;
V= perms(a);
[n,~] = size(V);
% 恰好第5次把兩個(gè)患者都檢測(cè)出來(lái)的情況進(jìn)行計(jì)數(shù)
s5 =0;
for i=1:n
???if (V(i,5) == 1 || V(i,5) == 2 ) && V(i,6) > 2 && V(i,7) >2
??????% 1或者2號(hào)排在第5位,第6位和第7位是健康者,這就是一個(gè)恰好第5次檢測(cè)出兩個(gè)患者的情況
??????s5 = s5+1;
???end
end
% 得出恰好第五次檢測(cè)出兩個(gè)患者的概率
s5/factoral(7)
% 把一個(gè)向量的所有排列都給出來(lái)
% 例 perms ([1, 2, 3])
%??3??2??1
%??3??1??2
%??2??3??1
%??2??1??3
%??1??3??2
%??1??2??3
%
function A = perms (v)
?if (nargin < 1)
???help perms
??????return
?end
?v = v(:).';
?if (isnumeric (v) || ischar (v))
???v = sort (v, "ascend");
?end
?n = numel (v);
?if (n < 4)???% 對(duì)于n比較小時(shí),單獨(dú)處理
???switch (n)
?????case 0
???????A = reshape (v, 1, 0);
?????case 1
???????A = v;
?????case 2
???????A = [v([2 1]);v];
?????case 3
???????A = v([3 2 1; 3 1 2; 2 3 1; 2 1 3; 1 3 2; 1 2 3]);
???end
?else
???v = v(end:-1:1);
???n = n-1;
???idx = zeros (factorial (n), n);
???idx(1:6, n-2:n) = [1, 2, 3;1, 3, 2;2, 1, 3;2, 3, 1;3, 1, 2;3, 2, 1]+(n-3);
???f = 2;???% jump-start for efficiency with medium n
???for j = 3:n-1
?????b = 1:n;
?????f = f*j;
?????perm = idx(1:f, n-(j-1):n);
?????????tmp = n-j:n;
?????????tmpA = tmp(ones(f,1),:);
?????idx(1:(j+1)*f, n-j) = tmpA(:);
?????for i=0:j
???????b(i+n-j) = b(i+n-j) - 1;
???????idx((1:f)+i*f, n-(j-1):n) = b(perm);
?????end
???end
???n = n + 1;
???f = f*(n-1);
??????tmpC = v(1);
???A = tmpC(ones (factorial (n), n));
??????tmpD = v(ones(f,1),:);
???A(:,1) = tmpD(:);
???for i = 1:n
?????b = v([1:i-1 i+1:n]);
?????A((1:f)+(i-1)*f, 2:end) = b(idx);
???end
?end
end