How to add the rows in a for loop

I have the following code,
labels = 1:100;
element_positions = zeros(length(labels-10),4)
for i = [1:9,11:19,21:29,31:39,41:49,51:59,61:69,71:79,81:89];
element_positions = [nodes(i,:),nodes(i+1,:);
nodes(i,:),nodes(i+10,:);
nodes(i,:),nodes(i+11,:);
nodes(i+10,:),nodes(i+1,:)]
end
I want to make an element matrix in which I couple the nodes to a full matrix, instead of computing 9 indepentendt matrices, how do i make a for loop that adds them?
I already tried
labels = 1:100;
element_positions = zeros(length(labels-10),4)
for i = [1:9,11:19,21:29,31:39,41:49,51:59,61:69,71:79,81:89];
element_positions(i,:) = [nodes(i,:),nodes(i+1,:);
nodes(i,:),nodes(i+10,:);
nodes(i,:),nodes(i+11,:);
nodes(i+10,:),nodes(i+1,:)]
end
but then it get the errror: Unable to perform assignment because the size of the left side is 1-by-4 and the size of the right side is 4-by-4.
Thanks in advance,
Frank

2 comentarios

Luna
Luna el 16 de Nov. de 2018
What is the nodes?
nodes is a 100 x 2 double matrix
m = n = 9
xpos = [0:n 0:n 0:n 0:n 0:n 0:n 0:n 0:n 0:n 0:n]';
ypos = reshape( repmat( [0:n], m+1,1 ), 1, [] )';
nodes = [xpos, ypos];

Iniciar sesión para comentar.

Respuestas (2)

KSSV
KSSV el 16 de Nov. de 2018
Should be something like this to solve error.
labels = 1:100;
element_positions = zeros(length(labels-10),4)
for i = [1:9,11:19,21:29,31:39,41:49,51:59,61:69,71:79,81:89];
element_positions(i,:) = [nodes(i,1),nodes(i+1,2);
nodes(i,2),nodes(i+10,3);
nodes(i,3),nodes(i+11,4);
nodes(i+10,4),nodes(i+1,1)]
end

3 comentarios

Frank Oosterveld
Frank Oosterveld el 16 de Nov. de 2018
This doesn't work since the nodes matrix has only 2 columns.
I want to end up with a matrix of 4 columns and 100 rows.
KSSV
KSSV el 16 de Nov. de 2018
What is your domain? What FEM element you are using?
Frank Oosterveld
Frank Oosterveld el 16 de Nov. de 2018
This is what i eventually want to make, therefore I need the connection_matrix, which I try to obtain by the proposed for-loop.
FEM.PNG

Iniciar sesión para comentar.

madhan ravi
madhan ravi el 16 de Nov. de 2018
labels = 1:100;
element_positions = cell(1,length(labels-10)); %preallocation
ctr=1;
for i = [1:9,11:19,21:29,31:39,41:49,51:59,61:69,71:79,81:89];
element_positions{ctr} = [nodes(i,1),nodes(i+1,2);
nodes(i,2),nodes(i+10,3);
nodes(i,3),nodes(i+11,4);
nodes(i+10,4),nodes(i+1,1)]
ctr=ctr+1;
end
celldisp(element_positions)
[element_positions{:}] %double matrix

3 comentarios

Frank Oosterveld
Frank Oosterveld el 16 de Nov. de 2018
Thanks for your fast answer,
Only thing is that this exceeds my node matrix. I want to end up with a matrix with 4 columns, starting from a input matrix of 2 columns, then i want to end up with a matrix as;
for i = 1
[xpos_node1, ypos_node1, xpos_node2, ypos_node2;
xpos_node1,ypos_node1, xpos_node_11, ypos_node 11;
xpos_node1, ypos_node1, xpos_node_12, ypos_node_12;
xpos_node11, ypos_node11, xpos_node_2, ypos_node_2]
etcetera.
madhan ravi
madhan ravi el 16 de Nov. de 2018
what’s the size of your matrix when you try my answer?
Frank Oosterveld
Frank Oosterveld el 16 de Nov. de 2018
It won't work since you are asking the 3th and 4th columnelement of a matrix with 2 columns

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 16 de Nov. de 2018

Comentada:

el 16 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by