我有一个2x2矩阵,我想自己乘10次,每次乘法后存储结果。使用for循环可以很容易地做到这一点,但是我想将它矢量化为消除for循环。我的方法是获得我的2x2矩阵a,并将其提升到带有元素1:10的向量b。答案应该是复制类型的2x2x10矩阵。
a^b(1)
a^b(2)
.
.
.
a^b(10)为了澄清我并不是这样做的,我需要实际的矩阵乘法,并且不喜欢使用for循环。谢谢你能帮我的忙。
发布于 2016-02-29 14:43:26
这是你的密码。我使用cellfun来完成这个任务,并且在代码的每一行之后都有注释。它可以计算和存储任意矩阵m的自乘的第n阶.如果你有什么问题,可以随便问。
function m_powerCell = power_store(m, n) %m is your input matrix, n is the highest power you want to reach
n_mat = [1:n]; %set a vector for indexing each power result in cell
n_cell = mat2cell(n_mat,1,ones(1,n)); %set a cell for each of the power
m_powerCell = cellfun(@(x)power(m, x), n_cell, 'uni', 0); %compute the power of the matrix
end
%this code will return a cell to you, each element is a matrix, you can
%read each of the matrix by m_powerCell{x}, x represents the xth order https://stackoverflow.com/questions/35696354
复制相似问题