Alireza
Alireza

Reputation: 81

Plot from a matrix in matlab

I want to create a multi-plot from each array of matrix y:

q = [...] % (a 1x6 matrix)
p = [...] % (a 6x6 matrix)
x = [0:1:40];
y = q * p ^ x;

But I get this error:

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

Upvotes: 0

Views: 78

Answers (3)

percusse
percusse

Reputation: 3106

To avoid blow-ups in power computations, do not compute the powers explicitly but instead use intermediate results

y = zeros(41,6);
y(1,:) = q;
for ind = 1:40
    y(ind+1,:) = y(ind,:)*p;
end

Upvotes: 1

Muhammad Usman Saleem
Muhammad Usman Saleem

Reputation: 149

try to fix this line as

for k=1:40
y = q * p ^ k;
end

also you can do this as

for k=1:40
y = q * p ^ x(k);
end

also it will takess power of p as x then multiple q * p ^ x;

Upvotes: 0

User1551892
User1551892

Reputation: 3364

q = [...] ( a 1x6 matrix)
p = [...] ( a 6x6 matrix)
x = [0:1:40];
y = [] ;
for i = 1 : length (x)
    y(i,:,:) = q * p .^ x(i);
end

q * p will generate the matrix of size of 6 x 6. y will be 3 dimensional matrix of size of 41 x 6 x 6.

Upvotes: 0

Related Questions