1樓:
試了兩種方法,一種向量化用的比較多:
function
b =permsu1
(a)aUnique
=unique(a
);aCounts
=histcounts(a
,[aUnique
(:);
inf]);
numLeft
=numel(a
);b=zeros
(numLeft,1
,class(a
));fori=
1:numel
(aUnique)p
=nchoosek(1
:numLeft
,aCounts(i
)).'
;numLeft
=numLeft
-aCounts(i
);[t,
~]=find(~
b);t=
reshape(t
,'',size(b
,2));b
=repelem(b
,1,size(p
,2));b
(reshape(t
(p(:)+(0
:size(t
,1):
numel(t
)-1)),
size(p
,1),'')+
(0:numel(a
):numel(b
)-1))
=aUnique(i
);end
一種就用迴圈來做:
function
b =permsu2
(a)n
=numel(a
);m=n
;numB=1
;c=histcounts(a
,[unique(a
(:));
inf]);
fori=1
:numel(c
)numB
=numB
*nchoosek(m
,c(i
));m=m
-c(i
);endb=
zeros
(numel(a
),numB
,'like',a
);t=sort(a
(:));
b(:,1)
=t;forj=
2:size(b
,2)fori1=
2:n,
ift(i1
-1) i1), break ,end ,endu= t(i1); ifi1 >10i2 =bsearch(t ,i1-1 ,u);else fori2=1 :n,if t(i2)
,end ,end endt(i1 )=t( i2);t( i2)=u ;fork= 1:i1/ 2u=t (i1-k );t(i1 -k)= t(k); t(k) =u;end fori=1 :n,b (i,j )=t( i);endend function r =bsearch (arr, r, a)l= 1;while l =floor((l +r)/ 2);ifarr(m ) =m;elsel= m+1; endend 對比效能迴圈的比較快: >>a= int8([1 ,2,3 ,3,3 ,4,4 ,4,4 ,5,5 ,5,5 ,5]);>> tic,b= permsu1(a );toc Elapsed time is0.910029 seconds .>> tic,c= permsu2(a );toc Elapsed time is0.156000 seconds .迴圈的方法用 Coder 自動編譯後也更快: >>tic,c= permsu2_mex(a );toc Elapsed time is0.076423 seconds. i.ZARD 給定邊長a,b,c,標明點順序。立方體示意圖 法1 使用plot3函式 暴力解法 直接將8個點順序連線 會出現畫線重複 close all clc a 0.5 b 1 c 1.5 figure 8個頂點 D 0 0 0 a 0 0 0 b 0 0 0 c a b c 0 b c a 0... 不含 123 且不含 456 的 ToString Range 1 6 Permutations StringJoin Select StringContainsQ 123 StringContainsQ 456 674 種 不含 123 或不含 456 的 ToString Range 1,6 P... iamxuxiao 網頁如何呼叫matlab的程式 這個問題出發點就有缺陷。既然是網頁,你的服務人群應該就不止你一個,如果你用MATLAB,這說明你的計算是有一定複雜度的,需要一定的時間去響應。那麼問題來了,你提供的服務不能scale,因為後端啟動一個deployed matlab的時間是秒級別的。... 瞳孔的顏色 演算法 按照度分佈給定79個節點的度,運用迴圈演算法把每一個節點的度的數目列出來,然後隨機的把他們連線起來。步驟 比如節點i的度是1那麼節點i存在1條與外界連線的線頭,隨機的與其他節點相連,所有79個節點的過程都類似,但是節點自身不能連,兩條節點之間不能用重邊。程式 clc clear ... function find arr elseif n i else return result function mock n var ar 1,0,2,2.1,2.2,2.4,6,7,8,99,100,101,102 varbest 3,2,1 var worst new Array 100 fi...如何用matlab畫立方體
如何用Matlab求出讓123和456交錯出現的所有可能性?
網頁如何呼叫matlab的程式實現相應功能?
如何用matlab根據度分佈畫出網路圖
如何用JS實現這道演算法題?