我想在for循环中增加A矩阵的功率但是会出错 "输入必须是标量或方形矩阵"
function [S] = myst()
st_1 =[0.1490 0 0.1723 0.1786 0.2015 0.1387 0.1600]';
for A = [0.5 0 0 0 0 0.5 0.2;0 0 0 0 0 0 0 ; 0 0 0 0.4 0.4 0 0;0 0 1 0.2 0.2 0 0;0 0 0 0.4 0.2 0 0.4;0 0 0 0 0 0.5 0;0.5 0 0 0 0.2 0 0.4]
for n = 1:1:77
A_n = A^n; % this A matrix is changing its index after every loop,
%i don't know why index of A_n is changing?
S = (A_n)*st_1;
if S(1,1) == st_1(1,1)
disp(n);
break
end
end
end
disp(A);
end
答案 0 :(得分:1)
当编写像for A=[1,2,3;4,5,6;7,8,9],disp(A);end
这样的for循环时,你正在迭代A的单个列。当你打算处理完整矩阵时,只需删除外部for循环:
function [S] = myst()
st_1 =[0.1490 0 0.1723 0.1786 0.2015 0.1387 0.1600]';
A = [0.5 0 0 0 0 0.5 0.2;0 0 0 0 0 0 0 ; 0 0 0 0.4 0.4 0 0;0 0 1 0.2 0.2 0 0;0 0 0 0.4 0.2 0 0.4;0 0 0 0 0 0.5 0;0.5 0 0 0 0.2 0 0.4]
for n = 1:1:77
A_n = A^n; % this A matrix is changing its index after every loop,
%i don't know why index of A_n is changing?
S = (A_n)*st_1;
if S(1,1) == st_1(1,1)
disp(n);
break
end
end
disp(A);
end