AX,AY,AZ的創建矩陣:[N逐N]一種快速和有效的方式從一系列產品
B = AA(二進制)
這意味着:
B(i,j)= [Ax(i,j);Ay(i,j);Az(i,j)]*[Ax(i,j) Ay(i,j) Az(i,j)]
B(i,j):3×3矩陣。 一到構建體B的方法是:
N=2;
Ax=rand(N); Ay=rand(N); Az=rand(N); %# [N-by-N]
t=1;
F=zeros(3,3,N^2);
for i=1:N
for j=1:N
F(:,:,t)= [Ax(i,j);Ay(i,j);Az(i,j)]*[Ax(i,j) Ay(i,j) Az(i,j)];
t=t+1; %# t is just a counter
end
end
%# then we can write
B = mat2cell(F,3,3,ones(N^2,1));
B = reshape(B,N,N)';
B = cell2mat(B);
是否有當N很大更快的方法。
編輯:
感謝您的回答。 (更快) 讓我們把: N = 2; Ax = [1 2; 3 4]; Ay = [5 6; 7 8]; Az = [9 10; 11 12];
B =
1 5 9 4 12 20
5 25 45 12 36 60
9 45 81 20 60 100
9 21 33 16 32 48
21 49 77 32 64 96
33 77 121 48 96 144
運行:
???錯誤使用==> mtimes 內部矩陣維度必須一致。
如果我寫:P = Ai*Aj;
然後
B =
7 19 31 15 43 71
23 67 111 31 91 151
39 115 191 47 139 231
10 22 34 22 50 78
34 78 122 46 106 166
58 134 210 70 162 254
即從上方 甲不同模式(:,:,1)從[AX(1,1)Ay的(1,1)了Az deffer(1, 1)]
編輯:
N=100;
Me :Elapsed time is 1.614244 seconds.
gnovice :Elapsed time is 0.056575 seconds.
N=200;
Me :Elapsed time is 6.044628 seconds.
gnovice :Elapsed time is 0.182455 seconds.
N=400;
Me :Elapsed time is 23.775540 seconds.
gnovice :Elapsed time is 0.756682 seconds.
Fast!
rwong: B was not the same.
編輯:
經過一些修改我的applicati於: 通過gnovice碼
1st code : 19.303310 seconds
2nd code: 23.128920 seconds
3rd code: 13.363585 seconds
似乎任何函數調用像小區,ind2sub ......讓THW循環緩慢,768,16儘可能避免。
symIndex
很有趣!謝謝。
非常感謝gnovice.This很出色!在B是對稱的情況下,我們說算法是否可以在線性索引中計算矩陣的一半? – Abolfazl 2011-06-07 08:45:01