I am trying to change specific values of a matrix with a new ones.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Steven Shaaya
el 29 de Nov. de 2021
Comentada: Steven Shaaya
el 29 de Nov. de 2021
%This is the matrix I am working with
%please do not worry about changing mpc to f in my code because my original code has many matrix and here I am calling only 'bus' matrix
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/816894/image.png)
%code 1
f.bus()
format short
for y=1:10
y
compound=(1+0.1)^(y-1)
ND=f.bus(:,3:4)*compound
end
%this is the portion of result for the above code 1
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/816899/image.png)
% Now when I try to substitute those values in the matrix by the following code 2
%code 2, I am just modifying code 1
f.bus()
format short
for y=1:10
y
compound=(1+0.1)^(y-1)
ND=f.bus(:,3:4)*compound
f.bus(:,3:4)= ND
f.bus()
end
%see what happen to y =10 for example
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/816904/image.png)
%And see what happen to the matrix, this is also a portion of the matrix but you can see columns 3 and 4
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/816909/image.png)
% Any idea why that is happening
%we can see that my ND values has changed too.
%please note, my original matrix was not affacted by my code 1 and I did "clear all" and "clc" but I did not help at all
2 comentarios
Respuesta aceptada
Walter Roberson
el 29 de Nov. de 2021
Choose one approach:
- take interest rate plus one, to the power of the number of periods, and multiply by the initial value to get the expanded value; Or
- initialize current value to initial value. At each period multiply the current value by interest rate plus one, and store that back as the new current value.
You were doing both, calculating interest to the periods and updating the current value each period.
Interest to the periods is easy to vectorize, but keeping a current value is easier to simulate interest and repayment mixed.
Más respuestas (0)
Ver también
Categorías
Más información sobre Code Generation 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!