Evaluating a matrix at different time steps

Dear all, I have a matrix which is a transition matrix. I need to calculate the time evolution of the associated probability distribution. The matrix is raised to 't', the time variable and I want to evaluate the matrix at different time steps. Is it possible in Matlab? The code to generate the square matrix is given below which is working well for a given value if t.
if true
N=3;
w=0.2;
t=1;
G=zeros(N);
for j=1:N %rows
for n=1:N %columns
for k=1:N %summation
G(j,n)=G(j,n)+1/N*(((1-w)+w*cos((2*pi*k)/N)).^t*cos((2*pi*(j-n)*k)/N));
end
end
end
disp(G)
end

 Réponse acceptée

Star Strider
Star Strider le 29 Mar 2017
Modifié(e) : Andrei Bobrov le 29 Mar 2017
Add a separate loop for ‘t’, and a third dimension to your ‘G’ matrix:
N=3;
w=0.2;
tv = linspace(0, 10, 10); % Define Time Vector
G=zeros(N,N,length(tv)); % Preallocate Here
for t = 1:length(tv) % ‘Time’ Loop
for j=1:N %rows
for n=1:N %columns
for k=1:N %summation
G(j,n,t)=G(j,n,t)+1/N*(((1-w)+w*cos((2*pi*k)/N)).^tv(t)*cos((2*pi*(j-n)*k)/N));
end
end
end
% disp(G)
end
Define the vector of times you want to evaluate your matrix in ‘tv’. The rest of the code will then work. Each ‘page’ (third dimension elements) of ‘G’ will be ‘G’ at the corresponding times.
Also, move the preallocation step to be before the loop! Otherwise, it resets all previous values of ‘G’ to zero.

6 commentaires

That's great! It's working. Thank you very much.:-)
Vipin  Padinjarath
Vipin Padinjarath le 29 Mar 2017
Modifié(e) : Vipin Padinjarath le 29 Mar 2017
But I have a doubt. When I set the value of t and evaluate the code (my old code) the values of the elements of the matrix are different from the corresponding matrix this new code generates. Why is that so? I am sorry, I am quite new to matlab.
My pleasure!
I see the problem. I forgot to include the ‘t’ dimension on the right-hand side reference to ‘G’. This should work correctly:
G(j,n,t)=G(j,n,t)+1/N*(((1-w)+w*cos((2*pi*k)/N)).^tv(t)*cos((2*pi*(j-n)*k)/N));
↑ ← ADDED ‘t’ HERE
(My apologies for the error.)
Star Strider
Star Strider le 29 Mar 2017
My pleasure!
It works perfectly now!! Cheers!Thanks again!!
Star Strider
Star Strider le 29 Mar 2017
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 29 Mar 2017
Modifié(e) : Andrei Bobrov le 29 Mar 2017
R2016b and later
N = 3;
w = 0.2;
k = 1:N;
k = reshape(k,1,1,1,[]);
jj = (1:N)';
n = 1:N;
t = (0:10);
t = reshape(t,1,1,[]);
g = ((1-w+w*cos(2*pi*k/N)).^t.*cos(2*pi*(jj-n).*k/N))/N;
G = sum(g,4);
R2016a and earlier
N = 3;
w = 0.2;
k = 1:N;
k = reshape(k,1,1,1,[]);
jj = (1:N)';
n = 1:N;
t = (0:10);
t = reshape(t,1,1,[]);
g0 = cos(bsxfun(@times,bsxfun(@minus,jj,n),2*pi*k)/N);
g1 = bsxfun(@power,1-w+w*cos(2*pi*k/N),t);
g = bsxfun(@times,g0,g1)/N;
G = sum(g,4);

1 commentaire

Thank you Andrei Bobrov. But for my standards, this is too much. I don't understand much of it. But will learn for sure. Thanks again.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by