필터 지우기
필터 지우기

I am trying to change specific values of a matrix with a new ones.

조회 수: 4 (최근 30일)
%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
%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
% 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
%And see what happen to the matrix, this is also a portion of the matrix but you can see columns 3 and 4
% 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
Walter Roberson
Walter Roberson 2021년 11월 29일
do not use format short, use format longg
Steven Shaaya
Steven Shaaya 2021년 11월 29일
편집: Steven Shaaya 2021년 11월 29일
I did but it does not help. As you can see the number is getting very large for ND but the value of compound is correct.
Also when I start my "for loop from y=10:10
I get the correct value
but when the loop start from 1 to 10 then I get very large value

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 11월 29일
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.
  댓글 수: 1
Steven Shaaya
Steven Shaaya 2021년 11월 29일
Thank you for your help, I did work but I still don't understand why in my code 1 it was not saving the values and it was multiplying by 70 each time, but in my Code 2 it was multiplying by the previous value

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by