# Saving values from a for loop into an existing matrix

2 visualizaciones (últimos 30 días)
Allen Love el 13 de Feb. de 2020
Comentada: Allen Love el 17 de Feb. de 2020
Hello,
I am trying to define the boundary conditions in a FEM truss problem. I am having trouble saving what I did to the matrix. For example if BC(1) = 0 I want to make the first row equal to 0 then make the value in that row along the diagnol equal to 1 and save it to the original matrix.
here is my code:
%number of nodes / number of degrees of freedom
num_nd = 4;
num_dof = 2;
% Boundary Conditions
n_u = ones(num_nd, num_dof);
n_u(1,1) = 0; % Boundary conditions for node 1 x
n_u(1,2) = 0; % Boundary conditions for node 1 y
n_u(3,1) = 0; % Boundary condition for node 3 y
% Global matrix
kg = [1.3536 .3536 -1 0 0 0 -.3536 -.3536;
.3536 1.3536 0 0 0 -1 -.3536 -.3536;
-1 0 1 0 0 0 0 0; 0 0 0 1 0 0 0 -1;
0 0 0 0 1 0 -1 0; 0 -1 0 0 0 1 0 0;
-.3536 -.3536 0 0 -1 0 1.3536 .3536;
-.3536 -.3536 0 -1 0 0 .3536 1.3536];
%Apply boundary conditions
for i = 1:num_nd
if n_u(i) == 0
kg(i,:) = 0;
if kg(i,:) == 0
kg(i,i) = 1;
end
end
end
% What I want kg to look like
% kg = [1 0 0 0 0 0 0 0;
% 0 1 0 0 0 0 0 0;
% -1 0 1 0 0 0 0 0;
% 0 0 0 1 0 0 0 -1;
% 0 0 0 0 1 0 0 0;
% 0 -1 0 0 0 1 0 0;
% -.3536 -.3536 0 0 -1 0 1.3536 .3536;
% -.3536 -.3536 0 -1 0 0 .3536 1.3536];
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

Spencer Chen el 13 de Feb. de 2020
for i = num_nd ...
This code does not give you a proper for-loop.
I'll leave you pondering on the problem yourself.
Blessings,
Spencer
##### 2 comentariosMostrar NingunoOcultar Ninguno
Allen Love el 14 de Feb. de 2020
even for i = 1:num_nd I still get the original matrix. I understand for loops in the basic sense. My question is how to save values to an existing matrix.
Allen Love el 17 de Feb. de 2020
Any ideas beyond the for loop syntax?

Iniciar sesión para comentar.

### Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by