How can I fix the error "Index exceeds matrix dimension"

In my FEM program, I want to build a matrix bcdof[1;2;7;8;67;68;69;70;71;72] from matrix bcdof1=[1;4;34;35;36] but the program say that "Index exceeds matrix dimension". Can anyone help me, here is my code:
bcdof1=[1;4;34;35;36];
bcdof=zeros(1,2*length(bcdof1));
for i=1:length(bcdof1)
bcdof(1,(2*i-1))=2*bcdof1(1,i)-1;
bcdof(1,(2*i))=2*bcdof1(1,i);
end

2 comentarios

You have to explain the calculation
the calculation was in my code, each element in matrix bcdof have a corresponding value in the right hand side.

Iniciar sesión para comentar.

 Respuesta aceptada

Dennis
Dennis el 22 de Oct. de 2018
Editada: Dennis el 22 de Oct. de 2018
bcdof1 is a column vector you need to index bcdof1(i,1) instead of bcdof(1,i)
bcdof1=[1;4;34;35;36];
bcdof=zeros(1,2*length(bcdof1));
for i=1:length(bcdof1)
bcdof(1,(2*i-1))=2*bcdof1(i,1)-1;
bcdof(1,(2*i))=2*bcdof1(i,1);
end

Más respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 22 de Oct. de 2018
Editada: KALYAN ACHARJYA el 22 de Oct. de 2018
bcdof1=[1;4;34;35;36];
bcdof=zeros(1,2*length(bcdof1));
for i=1:1 %length(bcdof1)
bcdof(1,2*i-1)=2*bcdof1(1,i)-1;
bcdof(1,2*i)=2*bcdof1(1,i);
end
Your matrix is size 5x1, there is a single column. What you are doing a here using "for loop" from 1 to 5.
when i=1, it is ok, but when i=1, in your following expression
bcdof(1,2*i-1)=2*bcdof1(1,i)-1;
becomes
bcdof(1,3)=2*bcdof1(1,2)-1;
That means bcdof(1,3) and bcdof1(1,2) data is not available in matrix.
As your following data having only
bcdof1=[1;4;34;35;36];
That means
bcdof1(1,1)=1;
bcdof1(2,1)=4;
bcdof1(3,1)=34;
Please note the differences
bcdof1=[1;4;34;35;36]
bcdof1 =
1
4
34
35
36
>> bcdof1=[1,4,34,35,36]
bcdof1 =
1 4 34 35 36

3 comentarios

bcdof1=[1;4;34;35;36];
bcdof=zeros(1,2*length(bcdof1));
for i=1: 1 %length(bcdof1) for sinle 1 its works
bcdof(1,2*i-1)=2*bcdof1(1,i)-1;
bcdof(1,2*i)=2*bcdof1(1,i);
end
Another one if you change the column vector to row vector then it works
bcdof1=[1,4,34,35,36]; % Change the data column to row matrix
bcdof=zeros(1,2*length(bcdof1));
for i=1:1%length(bcdof1)
bcdof(1,2*i-1)=2*bcdof1(1,i)-1;
bcdof(1,2*i)=2*bcdof1(1,i);
end
Ductho Le
Ductho Le el 22 de Oct. de 2018
Editada: Ductho Le el 22 de Oct. de 2018
thank you..i have seen that problem.
KALYAN ACHARJYA
KALYAN ACHARJYA el 22 de Oct. de 2018
Editada: KALYAN ACHARJYA el 22 de Oct. de 2018
Third way to avoid the error is- % Index position change from bcdof1(1,i) to bcdof1(i,1)
bcdof1=[1;4;34;35;36];
bcdof=zeros(1,2*length(bcdof1));
for i=1:1%length(bcdof1)
bcdof(1,2*i-1)=2*bcdof1(i,1)-1;
bcdof(1,2*i)=2*bcdof1(i,1);
end

Iniciar sesión para comentar.

Categorías

Preguntada:

el 22 de Oct. de 2018

Editada:

el 22 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by