如何在Matlab中获得增量功率矩阵 [英] how to get an incremental power matrix in matlab

查看:132
本文介绍了如何在Matlab中获得增量功率矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Matlab中计算以下矩阵:

I wanted to compute the following matrix in Matlab:

    g=[I
       A 
       .
       .
       .
      A^N]      

我在Matlab中使用了以下程序:

I used the following program in Matlab:

A=[2 3;4 1];
s=A;
for n=1:1:50
   s(n)=A.^n;
end
g=[eye(1,1),s];

我遇到以下错误:

在分配A(I) = B中,BI中的元素数必须相同.
s_x_calcu_v1(第5行)
中的错误 s(n)=A.^n;

In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in s_x_calcu_v1 (line 5)
s(n)=A.^n;

推荐答案

问题是您试图将矩阵分配给单个元素.在Matlab中,调用s(n)意味着您获得s的第n个元素,而不管s的尺寸如何.您可以使用三维矩阵

The problem is that you are trying to assign a matrix to a single element. In matlab calling s(n) mean you get the nth element of s, regardless of the dimensions of s. You can use a three dimensional matrix

N = 50;
A=[2 3;4 1];

[nx,ny] = size(A);
s(nx,ny,N) = 0; %makes s a nx x ny x N matrix
for n=1:1:N
    s(:,:,n)=A.^n; %Colon to select all elements of that dimension
end
g=cat(3, eye(size(A)) ,s); %Add the I matrix of same size as A

或矢量化版本

s = bsxfun(@power, A(:), 1:N);
s = reshape(s,2,2,N);
g = cat(3, eye(size(A)) ,s);

以及使用cumprod

s = repmat(A(:), [1 N]);
s = cumprod(s,2);
s = reshape(s,2,2,N);
g = cat(3, eye(size(A)) ,s);

这篇关于如何在Matlab中获得增量功率矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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