Problem to display output

2 views (last 30 days)
Deck Zhan Sim
Deck Zhan Sim on 13 Dec 2021
Answered: Rik on 13 Dec 2021
Hi, I have a problem to display the output. Can someone help me to solve this? Thanks a lots!
Below here are the codes:
for i = 1:3
trans_matrix = trans_matrix + diag(ones(N-i,1),i);
end
for n = 1: length(ladder_Start)
b = trans_matrix(:,ladder_End(n));
trans_matrix(:,ladder_End(n)) = trans_matrix(:,ladder_Start(n)) + b;
trans_matrix(:,ladder_Start(n)) = zeros(1,N);
b = [];
end
for n = 1: length(snake_Head)
b = trans_matrix(:,snake_Tail(n));
trans_matrix(:,snake_Tail(n)) = trans_matrix(:,snake_Head(n)) + b;
trans_matrix(:,snake_Head(n)) = zeros(1,N);
b = [];
end
b = trans_matrix;
trans_matrix = (1/3)*b;
b = [];
ITERATIONS = 200;
d=zeros(1,ITERATIONS);
for i = 1:200
b = trans_matrix^i;
d(i) = b(N+1,1);
end
b=[];
At the command window there it shows the error of this.
  2 Comments
Deck Zhan Sim
Deck Zhan Sim on 13 Dec 2021
trans_matrix = zeros(25,25);
ladder_Start = [3, 9, 17];
ladder_End = [7, 18, 24];
snake_Head = [16, 20, 23];
snake_Tail = [5, 12, 19];
N = 25;
Mathieu NOE Hi, this is the initilaized code that you mention

Sign in to comment.

Answers (1)

Rik
Rik on 13 Dec 2021
You're trying to index b. So let's explore the values involved:
%rest of your code
for i = 1:200
b = trans_matrix^i;
disp(b)
disp(N+1)
d(i) = b(N+1,1);
end
Columns 1 through 22 0 0.3333 0 0.3333 0 0 0.3333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3333 0.3333 0 0.3333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 0 0 0 0 0 0 0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23 through 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3333 0 0 0.3333 0 0 0.3333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3333 0 0 0.3333 0.3333 0 0.3333 0.3333 0 0 0.3333 0 0 0
26
Index in position 1 exceeds array bounds. Index must not exceed 25.
So you have a 25x25 matrix, and you try to get the first value from the 26th row. It doesn't exist, so Matlab throws the error.
Since you didn't include any comments in your code and used obfuscating names like b and d as variable names, it is impossible to understand what is happening or what the intended function was. It is therefore extremely difficult to suggest a change in your code.
This is about as self-documenting before minifying it as afterwards:
v000=zeros(25,25);v001=[3, 9, 17];v002=[7, 18, 24];v003=[16, 20, 23];v004=...
[5, 12, 19];v005=25;for v006=1:3,v000=v000 + diag(ones(v005-v006,1),v006);
end,for v007=1: length(v001),v008=v000(:,v002(v007));v000(:,v002(v007))=...
v000(:,v001(v007)) + v008;v000(:,v001(v007))=zeros(1,v005);v008=[];end,for ...
v007=1: length(v003),v008=v000(:,v004(v007));v000(:,v004(v007))=v000(:,...
v003(v007)) + v008;v000(:,v003(v007))=zeros(1,v005);v008=[];end,v008=v000;
v000=(1/3)*v008;v008=[];v009=200;v010=zeros(1,v009);for v006=1:200,v008=...
v000^v006;v010(v006)=v008(v005+1,1);end,v008=[];

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by