Re: [討論] 矩陣設值問題已回收

看板MATLAB作者 (即將發霉)時間15年前 (2011/07/18 16:22), 編輯推噓2(201)
留言3則, 3人參與, 最新討論串2/2 (看更多)
1+5*N:4+5*N N=10000, repeat 10^3 case 1, mem allocation, index, and fill value m=zeros(1,4*(N+1)); for i=0:N m(4*i+1:4*i+4)=1+5*i:4+5*i; end 18.675 case 2, lazy allocation...using cell m=cell(N+1,1); for i=0:N m{i+1}=1+5*i:4+5*i; end m=horzcat(m{:}); 74.038 case 3, exclude values by modulation (this is inefficient if target vector is too big such as 10^8, and producing memory leak) m=1:1+5*N+3; m(mod(m,5)==0)=[]; 2.049 case 4, exclude without mod (this is inefficient if target vector is too big such as 10^8, and producing memory leak) m=1:1+5*N+3; m(5:5:5*N)=[]; 0.404 case 5, fastest m=zeros(4,N+1); m(1,:)=1:5:1+5*N; m(2,:)=2:5:2+5*N; m(3,:)=3:5:3+5*N; m(4,:)=4:5:4+5*N; m=m(:)'; 0.216 N= 2*10^7 case 3: 6.174 case 4: 3.193 case 5: 2.158 N= 10^8 case 3: NaN case 4: NaN case 5: 11.040 ※ 編輯: laifei 來自: 140.109.19.95 (07/18 17:12)

07/18 17:27, , 1F
太強了!!
07/18 17:27, 1F

07/18 20:02, , 2F
case 5 改成 m(:,1),最後不要',會快一些。
07/18 20:02, 2F

07/18 22:47, , 3F
remove transpose is incorrect, vectorizing is columwise
07/18 22:47, 3F
文章代碼(AID): #1E8-qvwD (MATLAB)
文章代碼(AID): #1E8-qvwD (MATLAB)