how to programme transition matrix with matlab
Show older comments
In dynamical system ( two dimensional ) we have transition matrix for a given A0, A1 and A2 ( are all matrices )

how we can programme this matrix with matlab ?
1 Comment
Amon Festo Manirakiza
on 13 Jan 2021
good question
Answers (3)
Image Analyst
on 26 Dec 2020
Edited: Image Analyst
on 26 Dec 2020
It looks like they're starting with i and j of zero so you need to skip the first row. Did you try eye() and a simple for loop?
n = 7;
A0 = 1 * rand(n)
A1 = 10 * rand(n)
A2 = 100 * rand(n)
T = eye(n)
for i = 2 : n
for j = 2 : n
T(i, j) = ...
A0 * T(i-1, j-1) + ...
A1 * T(i, j-1) + ...
A2 * T(i-1, j);
end
end
T
Note that it fails because we're taking matrices (the right hand side of the equation) and trying to stuff a matrix into a single element (the element at row i and column j), which you can't do. I think you need to explain it better.
And regarding your tags, what does this have to do with image analysis or processing?
15 Comments
azertazert azertazertazertazert
on 26 Dec 2020
Image Analyst
on 26 Dec 2020
Well you said that all were matrices so A0, A1, and A2 are matrices. And T(i,j) is one element out of a matrix T, so it's a scalar. Thus A1 * T(i, j) is a matrix times a scalar, which is a matrix. That's the middle term of your sum, and the other terms are also matrixes, so the sum of all 3 is a matrix. Now you're trying to stuff that matrix into a single element at T(I, j). That won't work. Are you sure that "A0, A1 and A2 ( are all matrices )"??? If the A were scalars, everything would work out fine.
azertazert azertazertazertazert
on 27 Dec 2020
Image Analyst
on 27 Dec 2020
Then what I said stands. What is on the right hand side of the equals sign is a matrix, and you can't stuff a whole matrix into one element of a different matrix, unless that different matrix is a cell array.
azertazert azertazertazertazert
on 27 Dec 2020
Image Analyst
on 27 Dec 2020
It would work if you replaced the A by A(i,j), like
n = 7;
A0 = 1 * rand(n)
A1 = 10 * rand(n)
A2 = 100 * rand(n)
T = eye(n)
for i = 2 : n
for j = 2 : n
T(i, j) = ...
A0(i, j) * T(i-1, j-1) + ...
A1(i, j) * T(i, j-1) + ...
A2(i, j) * T(i-1, j);
end
end
T
Do you think that's what you want?
azertazert azertazertazertazert
on 27 Dec 2020
Image Analyst
on 28 Dec 2020
I'm not sure what "ares*" means.
If T is the name of a variable that is a matrix, then T(i,j) is one single number - a scalar, NOT a matrix.
Perhaps if you gave some more context by showing the paper above and below the equations it might explain more.
azertazert azertazertazertazert
on 29 Dec 2020
azertazert azertazertazertazert
on 29 Dec 2020
azertazert azertazertazertazert
on 29 Dec 2020
azertazert azertazertazertazert
on 31 Dec 2020
wassim bidi
on 17 Jan 2021
Excuse me ,i have a problem i can't ask
so, how do i solve this problem?
Error using plot
Vectors must be the same lengths
clc
clear
sys1 = tf(1,[0.1 1])
t=0:0.1:10;
step(sys1)
y=step(sys1);
plot(t,y)
Image Analyst
on 17 Jan 2021
wassim, yes you CAN ask, and you just did. However you asked in the wrong place. You asked in azertazert's discussion instead of starting your own. Once you start your own, people will be able to tell you why t is not the same length as y, like ask you why you didn't define t as linspace(0, 10, length(y)).
Kent Millard
on 19 Jan 2021
@wassim bidi Hi Wassim. If you're still interested in asking your question, please click the 'Ask' link beneath the blue bar or use this link to start a new question thread. Best - Kent
n = 7;
A0 = 1 * rand(n);
A1 = 10 * rand(n);
A2 = 100 * rand(n);
T(1,1:n) = {eye(n)};
T(1:n,1) = {eye(n)};
for i = 2 : n
for j = 2 : n
T{i, j} = ...
A0 * T{i-1, j-1} + ...
A1 * T{i, j-1} + ...
A2 * T{i-1, j};
end
end
T
32 Comments
azertazert azertazertazertazert
on 31 Dec 2020
Image Analyst
on 31 Dec 2020
You forgot to give the ENTIRE error message, which includes line numbers and the actual line of code.
The code Walter posted runs without error. I just tried it. How did you alter it?
azertazert azertazertazertazert
on 31 Dec 2020
Edited: azertazert azertazertazertazert
on 31 Dec 2020
Image Analyst
on 31 Dec 2020
Put it in a script/editor window, not on the command line.
Also put
clear all
because evidently you have another variable called T that is a double so doing
T(1, 1:n) = {eye(n)};
won't work because you're trying to put a cell into a double. Clearing everything will get rid of your prior T and let it work.
azertazert azertazertazertazert
on 31 Dec 2020
Image Analyst
on 31 Dec 2020
I can tell you how to do that, but I'd rather you read the FAQ and learn how to do that:
azertazert azertazertazertazert
on 31 Dec 2020
Image Analyst
on 31 Dec 2020
OK, but you should have read the FAQ. If you had, you would know that you can simply use braces to extract the contents of a cell. This is how most MATLAB programmers would do it:
cellContents = T{1,2};
rather than call the cell2mat() function. The FAQ does not even mention cell2mat().
azertazert azertazertazertazert
on 1 Jan 2021
Image Analyst
on 1 Jan 2021
Of course. Because if A is a matrix and you're multiplying it by some matrix inside the cell of T, the number of columns of A must match the number of rows of whatever is inside that cell of T. Evidently it doesn't match.
azertazert azertazertazertazert
on 1 Jan 2021
Image Analyst
on 1 Jan 2021
That's what we've been trying to tell you. The original equation does not make sense if the A are matrices. The only suggestion I had to fix it was to index A with (i, j) so that you are using JUST ONE ELEMENT of A instead of the whole matrix.
azertazert azertazertazertazert
on 1 Jan 2021
If you want to iterate i and j to 25, then you must initialize T(1:1:25) and T(1:25,1)
n=2
maxiter = 25;
A0 = [0.5 1 ;2 3];
A1 = [4 1 ;0.8 -2];
A2 = [3.4 1.2 ;-0.4 1];
T(1,1:maxiter) = {eye(n)};
T(2:maxiter,1) = {eye(n)};
for i = 2 : maxiter
for j = 2 : maxiter
T{i, j} = ...
A0 * T{i-1, j-1} + ...
A1 * T{i, j-1} + ...
A2 * T{i-1, j};
end
end
T
azertazert azertazertazertazert
on 1 Jan 2021
azertazert azertazertazertazert
on 1 Jan 2021
Walter Roberson
on 1 Jan 2021
subplot(2,1,1)
fsurf(x(1), 0.9*[-1 1 -1 1])
xlabel('t1')
ylabel('t2')
title('first x')
subplot(2,1,2)
fsurf(x(2), 0.9*[-1 1 -1 1])
xlabel('t1')
ylabel('t2')
title('second x')
azertazert azertazertazertazert
on 2 Jan 2021
Edited: azertazert azertazertazertazert
on 2 Jan 2021
Image Analyst
on 2 Jan 2021
fsurf() was introduced in R2016a. Do you have a version older than that?
azertazert azertazertazertazert
on 2 Jan 2021
Edited: azertazert azertazertazertazert
on 2 Jan 2021
azertazert azertazertazertazert
on 2 Jan 2021
Edited: azertazert azertazertazertazert
on 2 Jan 2021
Walter Roberson
on 2 Jan 2021
The online version you plotted x(1), the ezsurf you plotted x(2). The plots are different
azertazert azertazertazertazert
on 2 Jan 2021
Walter Roberson
on 3 Jan 2021
I did identify a problem in the initialization but I have not had time to write it up yet... been dealing with hardware problems
maxiter = 25;
A0 = [-0.1 0 ;0.1 -0.05];
A1 = [-0.01 0.1; 0.1 -0.05];
A2 = [-0.05 0;0.1 -0.01];
%corrected initialization
T{1,1} = eye(2);
for i = 2 : maxiter
T{i,1} = A2*T{i-1,1};
end
for j = 2 : maxiter
T{1,j} = A1*T{1,j-1};
end
%do the work
for i = 2 : maxiter
for j = 2 : maxiter
T{i, j} = ...
A0 * T{i-1, j-1} + ...
A1 * T{i, j-1} + ...
A2 * T{i-1, j};
end
end
x=[0 0]';
B=[0.1 0.1]';
syms t1 t2
alpha= 0.7;
beta=0.9;
for i = 2 : maxiter
for j = 2 : maxiter
x=x+T{i-1,j-1}*B*((t1^(i*alpha))/gamma(i*alpha+1))*((t2^(j*beta))/gamma(j*beta+1));
end
end
fsurf(x(1), [0 20 0 20])
xlabel('t1')
ylabel('t2')
title('first x')
fsurf(x(2), [0 20 0 20])
xlabel('t1')
ylabel('t2')
title('second x')
azertazert azertazertazertazert
on 3 Jan 2021
Paul
on 3 Jan 2021
Can you post a screen capture of the equation for x from the source, like you did for T(i,j)?
Here is an implementation of T as a recursive function, to ensure that the proper thing is being calculated.
Note that in this implementation, what is to be passed to T is the zero-based indices exactly as in the definition of
.
But please be sure to check the bounds of iteration of i and j, keeping in mind that for this purpose it is fine to pass 0 to T. In particular, check what the powers of t1 should be in the variable ti,
and the powers of t2 should be in the variable tj,
and how those power relate to the (zero based) indices to pass into T.
A0 = [-0.1 0 ;0.1 -0.05];
A1 = [-0.01 0.1; 0.1 -0.05];
A2 = [-0.05 0;0.1 -0.01];
maxiter = 25;
x=[0 0]';
B=[0.1 0.1]';
syms t1 t2
alpha = 0.7;
beta = 0.9;
for i = 1 : maxiter
ti = B * ((t1^(i*alpha))/gamma(i*alpha+1));
for j = 1 : maxiter
tb = T(i-1, j-1, A0, A1, A2);
tj = ((t2^(j*beta))/gamma(j*beta+1));
x = x + tb * ti * tj;
end
end
fsurf(x(1), [0 20 0 20])
xlabel('t1')
ylabel('t2')
title('first x')
fsurf(x(2), [0 20 0 20])
xlabel('t1')
ylabel('t2')
title('second x')
function Tij = T(i, j, A0, A1, A2)
persistent tij Z
if isempty(tij);
tij = {eye(size(A0))};
Z = zeros(size(A0));
end
if i < 0 || j < 0
Tij = Z;
elseif i + 1 <= size(tij,1) && j+1 <= size(tij,2) && ~isempty(tij{i+1,j+1})
%i, j, size(tij)
Tij = tij{i+1,j+1};
else
ta0 = T(i-1, j-1, A0, A1, A2);
ta1 = T(i, j-1, A0, A1, A2);
ta2 = T(i-1, j, A0, A1, A2);
Tij = A0 * ta0 + A1 * ta1 + A2 * ta2;
tij{i+1, j+1} = Tij;
end
end
azertazert azertazertazertazert
on 3 Jan 2021
azertazert azertazertazertazert
on 3 Jan 2021
I was right in my previous concern: you do want to start the powers with 0.
A0 = [-0.1 0 ;0.1 -0.05];
A1 = [-0.01 0.1; 0.1 -0.05];
A2 = [-0.05 0;0.1 -0.01];
maxiter = 25;
x=[0 0]';
B=[0.1 0.1]';
syms t1 t2
alpha = 0.7;
beta = 0.9;
for i = 0 : maxiter
ti = B * ((t1^(i*alpha))/gamma(i*alpha+1));
for j = 0 : maxiter
tb = T(i-1, j-1, A0, A1, A2);
tj = ((t2^(j*beta))/gamma(j*beta+1));
x = x + tb * ti * tj;
end
end
fsurf(x(1), [0 20 0 20])
xlabel('t1')
ylabel('t2')
title('first x')
fsurf(x(2), [0 20 0 20])
xlabel('t1')
ylabel('t2')
title('second x')
function Tij = T(i, j, A0, A1, A2)
persistent tij Z
if isempty(tij);
tij = {eye(size(A0))};
Z = zeros(size(A0));
end
if i < 0 || j < 0
Tij = Z;
elseif i + 1 <= size(tij,1) && j+1 <= size(tij,2) && ~isempty(tij{i+1,j+1})
%i, j, size(tij)
Tij = tij{i+1,j+1};
else
ta0 = T(i-1, j-1, A0, A1, A2);
ta1 = T(i, j-1, A0, A1, A2);
ta2 = T(i-1, j, A0, A1, A2);
Tij = A0 * ta0 + A1 * ta1 + A2 * ta2;
tij{i+1, j+1} = Tij;
end
end
azertazert azertazertazertazert
on 3 Jan 2021
azertazert azertazertazertazert
on 3 Jan 2021
0 votes
1 Comment
azertazert azertazertazertazert
on 6 Jan 2021
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


























