how to programme transition matrix with matlab
古いコメントを表示
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 件のコメント
Amon Festo Manirakiza
2021 年 1 月 13 日
good question
回答 (3 件)
Image Analyst
2020 年 12 月 26 日
編集済み: Image Analyst
2020 年 12 月 26 日
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 件のコメント
azertazert azertazertazertazert
2020 年 12 月 26 日
Image Analyst
2020 年 12 月 26 日
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
2020 年 12 月 27 日
Image Analyst
2020 年 12 月 27 日
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
2020 年 12 月 27 日
Image Analyst
2020 年 12 月 27 日
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
2020 年 12 月 27 日
Image Analyst
2020 年 12 月 28 日
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
2020 年 12 月 29 日
azertazert azertazertazertazert
2020 年 12 月 29 日
azertazert azertazertazertazert
2020 年 12 月 29 日
azertazert azertazertazertazert
2020 年 12 月 31 日
wassim bidi
2021 年 1 月 17 日
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
2021 年 1 月 17 日
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
2021 年 1 月 19 日
@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 件のコメント
azertazert azertazertazertazert
2020 年 12 月 31 日
Image Analyst
2020 年 12 月 31 日
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
2020 年 12 月 31 日
編集済み: azertazert azertazertazertazert
2020 年 12 月 31 日
Image Analyst
2020 年 12 月 31 日
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
2020 年 12 月 31 日
Image Analyst
2020 年 12 月 31 日
I can tell you how to do that, but I'd rather you read the FAQ and learn how to do that:
azertazert azertazertazertazert
2020 年 12 月 31 日
Image Analyst
2020 年 12 月 31 日
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
2021 年 1 月 1 日
Image Analyst
2021 年 1 月 1 日
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
2021 年 1 月 1 日
Image Analyst
2021 年 1 月 1 日
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
2021 年 1 月 1 日
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
2021 年 1 月 1 日
azertazert azertazertazertazert
2021 年 1 月 1 日
Walter Roberson
2021 年 1 月 1 日
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
2021 年 1 月 2 日
編集済み: azertazert azertazertazertazert
2021 年 1 月 2 日
Image Analyst
2021 年 1 月 2 日
fsurf() was introduced in R2016a. Do you have a version older than that?
azertazert azertazertazertazert
2021 年 1 月 2 日
編集済み: azertazert azertazertazertazert
2021 年 1 月 2 日
azertazert azertazertazertazert
2021 年 1 月 2 日
編集済み: azertazert azertazertazertazert
2021 年 1 月 2 日
Walter Roberson
2021 年 1 月 2 日
The online version you plotted x(1), the ezsurf you plotted x(2). The plots are different
azertazert azertazertazertazert
2021 年 1 月 2 日
Walter Roberson
2021 年 1 月 3 日
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
2021 年 1 月 3 日
Paul
2021 年 1 月 3 日
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
2021 年 1 月 3 日
azertazert azertazertazertazert
2021 年 1 月 3 日
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
2021 年 1 月 3 日
azertazert azertazertazertazert
2021 年 1 月 3 日
0 投票
1 件のコメント
azertazert azertazertazertazert
2021 年 1 月 6 日
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



























