矩阵的幂 [英] Powers of a matrix

查看:103
本文介绍了矩阵的幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方矩阵A(nxn).我想将此矩阵的k次幂创建为nxnxk多维矩阵(不是逐个元素,而是矩阵的实际幂),即得到[A^0 A^1 A^2..A^k].这是用于矩阵盒的各种vandermonde.

I have a square matrix A (nxn). I would like to create a series of k powers of this matrix into an nxnxk multidimensional matrix (Not element-wise but actual powers of the matrix), i.e.getting [A^0 A^1 A^2..A^k]. It's sort of a varied vandermonde for matrix case.

我可以通过循环来做到这一点,但是这很烦人而且很慢.我尝试使用bsxfun,但是没有运气,因为我可能在这里错过了一些东西.

I am able to do it with loops but it is annoying and slow. I tried using bsxfun but no luck since I am probably missing something here.

这是我做的一个简单循环:

Here is a simple loop that I did:

for j=1:1:100 
    final(:,:,j)=A^(j-1); 
end

推荐答案

您正在尝试执行累积版本 /ref/mpower.html"rel =" nofollow> mpower ,其向量为k值.

You are trying to perform cummulative version of mpower with a vector of k values.

可悲的是, bsxfun 尚未发展为可以处理这样的情况.因此,我目前最好的建议是拥有一个运行中的存储,该存储会在每次迭代中累积矩阵乘积,以供下一次迭代使用.

Sadly, bsxfun hasn't evolved yet to handle such a case. So, the best I could suggest at this point would be having a running storage that accumulates the matrix-product at each iteration to be used at the next one.

您的原始循环代码看起来像这样-

Your original loop code looked something like this -

final = zeros([size(A),100]);
for j=1:1:100 
    final(:,:,j)=A^(j-1); 
end

因此,根据建议,修改后的循环代码应为-

So, with the suggestion, the modified loopy code would be -

final = zeros([size(A),100]);
matprod = A^0;
final(:,:,1) = matprod;
for j=2:1:100 
    matprod = A*matprod;
    final(:,:,j)= matprod;
end

基准化-

%// Input
A = randi(9,200,200);

disp('---------- Original loop code -----------------')
tic
final = zeros([size(A),100]);
for j=1:1:100 
    final(:,:,j)=A^(j-1); 
end
toc

disp('---------- Modified loop code -----------------')
tic
final2 = zeros([size(A),100]);
matprod = A^0;
final2(:,:,1) = matprod;
for j=2:1:100 
    matprod = A*matprod;
    final2(:,:,j)= matprod;
end
toc

运行时-

---------- Original loop code -----------------
Elapsed time is 1.255266 seconds.
---------- Modified loop code -----------------
Elapsed time is 0.205227 seconds.

这篇关于矩阵的幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆