Re: [討論] 矩陣設值問題已回收
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
07/18 20:02, 2F
→
07/18 22:47, , 3F
07/18 22:47, 3F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
0
6
MATLAB 近期熱門文章
PTT數位生活區 即時熱門文章