Axe、Ay、Az:[N-by-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):3x3行列。Bを作成する1つの方法は次のとおりです。
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
これは、上記のA(:、:、1)とは異なります[Ax(1,1)Ay(1,1)Az(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.
編集:
私のアプリケーションのためにいくつかの変更を加えた後:gnoviceコードによる
1st code : 19.303310 seconds
2nd code: 23.128920 seconds
3rd code: 13.363585 seconds
ceil、ind2sub ...のような関数呼び出しは、thwループを遅くし、可能であれば回避する必要があるようです。
symIndex
面白かったです!ありがとうございました。