Search code examples
matlabmatrixplotadjacency-matrix

Plot first element of vector in A^x * v in Matlab (matrix powers)


I want to plot A^x * v, where A is a square matrix represent an adjacency matrix in a graph, x is the number of steps, and v is the initial vector.

My goal is to plot the first element of each resulting vector; that is, I want A*v[1], A^2*v[1], A^2*v[1]

I tried

x = 1:1:50 
y = A^x*v
plot(y(1),x)

But got

Error using  ^ 
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.

I understand the error, but don't understand how else to approach this problem, just going off Matlab's plot examples.

Thanks in advance for the help!


Solution

  • If you want an almost one liner:

    x = 1:1:50;
    f = @(z) z(1);
    plot(x, arrayfun(@(n) f(A^n*v), x))
    

    The error in what you tried comes from A^x: x cannot be a vector, it must be a scalar because A is a square matrix. See the help of mpower for more info.