- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有 a1 a2 a3。它们是常数。我有一个矩阵A,我要做的是得到a1*A,a2*A,a3*A三个矩阵。然后我想将它们转移到对角 block 矩阵中。对于三个常数的情况,这很容易。我可以让 b1 = a1*A, b2=a2*A, b3=a3*A,然后在 matlab 中使用 blkdiag(b1, b2, b3)。
如果我有 n 个常量,a1 ... an。我怎么能在没有任何循环的情况下做到这一点?我知道这可以通过 kronecker 产品来完成,但这非常耗时,你需要做很多不必要的 0 * 常量。
谢谢。
最佳答案
这可能是 bsxfun(@plus
的一种方法 有助于 linear indexing
以函数格式编码 -
function out = bsxfun_linidx(A,a)
%// Get sizes
[A_nrows,A_ncols] = size(A);
N_a = numel(a);
%// Linear indexing offsets between 2 columns in a block & between 2 blocks
off1 = A_nrows*N_a;
off2 = off1*A_ncols+A_nrows;
%// Get the matrix multiplication results
vals = bsxfun(@times,A,permute(a,[1 3 2])); %// OR vals = A(:)*a_arr;
%// Get linear indices for the first block
block1_idx = bsxfun(@plus,[1:A_nrows]',[0:A_ncols-1]*off1); %//'
%// Initialize output array base on fast pre-allocation inspired by -
%// http://undocumentedmatlab.com/blog/preallocation-performance
out(A_nrows*N_a,A_ncols*N_a) = 0;
%// Get linear indices for all blocks and place vals in out indexed by them
out(bsxfun(@plus,block1_idx(:),(0:N_a-1)*off2)) = vals;
return;
使用方法:要使用上面列出的函数代码,假设您有a1
、a2
、a3
, ...., an
存储在向量 a
中,然后执行类似这样的操作 out = bsxfun_linidx(A,a)
在 out
中获得所需的输出。
本部分将此答案中列出的方法与其他答案中列出的其他两种运行时性能方法进行比较或基准测试。
其他的答案被转换为函数形式,像这样-
function B = bsxfun_blkdiag(A,a)
B = bsxfun(@times, A, reshape(a,1,1,[])); %// step 1: compute products as a 3D array
B = mat2cell(B,size(A,1),size(A,2),ones(1,numel(a))); %// step 2: convert to cell array
B = blkdiag(B{:}); %// step 3: call blkdiag with comma-separated list from cell array
和,
function out = kron_diag(A,a_arr)
out = kron(diag(a_arr),A);
为了比较,测试了A
和a
的四种大小组合,分别是-
A
为 500 x 500
和 a
为 1 x 10
A
为 200 x 200
和 a
为 1 x 50
A
为 100 x 100
和 a
为 1 x 100
A
为 50 x 50
和 a
为 1 x 200
下面列出了使用的基准测试代码 -
%// Datasizes
N_a = [10 50 100 200];
N_A = [500 200 100 50];
timeall = zeros(3,numel(N_a)); %// Array to store runtimes
for iter = 1:numel(N_a)
%// Create random inputs
a = randi(9,1,N_a(iter));
A = rand(N_A(iter),N_A(iter));
%// Time the approaches
func1 = @() kron_diag(A,a);
timeall(1,iter) = timeit(func1); clear func1
func2 = @() bsxfun_blkdiag(A,a);
timeall(2,iter) = timeit(func2); clear func2
func3 = @() bsxfun_linidx(A,a);
timeall(3,iter) = timeit(func3); clear func3
end
%// Plot runtimes against size of A
figure,hold on,grid on
plot(N_A,timeall(1,:),'-ro'),
plot(N_A,timeall(2,:),'-kx'),
plot(N_A,timeall(3,:),'-b+'),
legend('KRON + DIAG','BSXFUN + BLKDIAG','BSXFUN + LINEAR INDEXING'),
xlabel('Datasize (Size of A) ->'),ylabel('Runtimes (sec)'),title('Runtime Plot')
%// Plot runtimes against size of a
figure,hold on,grid on
plot(N_a,timeall(1,:),'-ro'),
plot(N_a,timeall(2,:),'-kx'),
plot(N_a,timeall(3,:),'-b+'),
legend('KRON + DIAG','BSXFUN + BLKDIAG','BSXFUN + LINEAR INDEXING'),
xlabel('Datasize (Size of a) ->'),ylabel('Runtimes (sec)'),title('Runtime Plot')
在我这边获得的运行时图是 -
结论:如您所见,可以研究基于bsxfun
的方法之一,具体取决于您处理的数据大小!
关于matlab - 矩阵的多个常数并将它们转换为matlab中的 block 对角矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28652033/
我真的不知道这个问题以前是否有人问过(我真的找不到) 所以,我正在学习如何创建基本的颜色切换游戏(随机颜色球下降,你需要旋转轮子与相同颜色的球碰撞) 通过这种轮换,我遇到了一个非常大的问题。我需要以某
我必须找到具有 M 对角线和 M << N 的对称方 NxN 矩阵的行列式.有没有比LU分解矩阵更快的方法? 最佳答案 是的,带(ed)矩阵有特殊的方法可以解决复杂度为 O(N*M^2) 的消元问题。
我有一个列数和行数相等的二维 numpy 数组。我想将它们排列成一个更大的阵列,对角线上有较小的阵列。应该可以指定起始矩阵在对角线上的频率。例如: a = numpy.array([[5, 7],
我是一名优秀的程序员,十分优秀!