How to keep a matrix from rewriting every loop iteration?

Hi guys, I'm having a problem with assigning values form a matrix to another matrix using loop functions.
Here is the code:
for t=1:Nw
for i=1:Nh
eN=Am(t:t+1,i:i+1)
nEN=reshape(eN',1,[])
for i1=1:4
KG=[];
KG(nEN((2*i)-1),nEN((2*i)-1))=Ke(2*i1-1,2*i1-1)
KG(nEN(2*i),nEN(2*i)=Ke((2*i1),(2*i1))
end
end
end
'KG' is a matrix with known amount of rows and columns and was initially filled with zeros. 'Ke' is a 8x8 matrix with constant values. 'nEN' is returned as an array of 4 numbers.
The problem is that for every iteration previously calculated values are set to zero and as a result I get a matrix with only two values calculated in the last iteration, whether I need a complete matrix filled fully with values from each iteration. I know I can prevent the matrix from being rewritten every time, but I don't remember how. Can anyone help? Thanks.

1 comentario

Vitaly
Vitaly el 21 de Abr. de 2013
Editada: Vitaly el 21 de Abr. de 2013
Omitting KG = [] before the loop works, thanks to Jan Simon, however the arrays nEN can have values that are the same for some iterations, and I need the corresponding values in Ke to add up in KG if that happens, but now it's just replacing the values if they're the same.
So the question now is how to make the function to add up new values to already existing ones in KG instead of replacing them?

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 21 de Abr. de 2013
What about simply omitting
KG = []
?

5 comentarios

Vitaly
Vitaly el 21 de Abr. de 2013
Editada: Vitaly el 21 de Abr. de 2013
I have it in line 6, or you mean put it in the statements in lines 7-8?
Vitaly
Vitaly el 21 de Abr. de 2013
I've put it before the loop and it seems to be working now...thanks
Vitaly
Vitaly el 21 de Abr. de 2013
the arrays nEN can have values that are the same for some iterations, and I need the corresponding values in Ke to add up in KG if that happens, but now it's just replacing the values if they're the same. do you know how I can deal with this?
Vitaly
Vitaly el 21 de Abr. de 2013
the question now is how to make the function to add up new values to already existing ones in KG instead of replacing them?
KG(index) = KG(index) + newValue

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 21 de Abr. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by