How to do while loop on arrays?

Hi there, I'm new to matlab, I'm having problem doing while loop on arrays. Apparently the logic for the while loop is wrong, it should go row by row until E equals to zero...can someone help please? It's kind of urgent. Thanks!
A=5000;
B=4000;
C=3000;
D=2000;
E=1000;
i=1;% row 1
i=[A B C D E]
%---------do this first----------------
%while i(1,5)>0 %the logic here is that value E should be more than zero to proceed
i(1,1)=i(1,5); %beginning balance row 2
i(1,2)=i(1,1)*1.5/12 %interest row 2
i(1,3)=i(1,2)-i(1,3); %principal row 2
i(1,4)=i(1,1)-i(1,4); %ending balance row 2
i=i+1;
i=[i;i(1,1),i(1,2),i(1,3),i(1,4),i(1,5)];
%------formula-------------------------
%‹end

2 Commenti

i=1;% row 1
i=[A B C D E]
I'm not really sure I have much of a clue what you are doing, but do you really want to declare i=1 and then immediately overwrite it with a vector.
Phoebe
Phoebe il 5 Apr 2021
the intention there is to make that particular vector as my first row...i'm not too sure how to write that in matlab

Accedi per commentare.

 Risposta accettata

DGM
DGM il 4 Apr 2021
It looks like you're doing some sort of basic demonstration of exponential behavior, but I'm not really sure how you're trying to go about it. It almost looks like you're using i as your data array while simultaneously thinking of it as an iterator. Are you trying to do something like this?
A=5000;
B=4000;
C=3000;
D=2000;
E=1000;
mdata=[A B C D E]
i=1;
while mdata(i,5)>0 % we need to be looking at the current line, otherwise nothing changes
mdata(1,1)=mdata(1,5); %beginning balance row 2
mdata(1,2)=mdata(1,1)*1.5/12; %interest row 2
mdata(1,3)=mdata(1,2)-mdata(1,3); %principal row 2
mdata(1,4)=mdata(1,1)-mdata(1,4); %ending balance row 2
% but what is column 5?
i=i+1;
mdata=[mdata;mdata(1,1),mdata(1,2),mdata(1,3),mdata(1,4),mdata(1,5)];
end
There's still the question of what column 5 is supposed to be. This line:
mdata(1,1)=mdata(1,5); % beginning balance row 2
implies that col5 is the prior ending balance, but this line:
mdata(1,4)=mdata(1,1)-mdata(1,4); % ending balance row 2
implies that col4 is the prior ending balance instead. Which is it? If column 5 is never updated, then why does the initial dataset have five columns?

8 Commenti

it's the second line which is missing ,because the value will be the same...
mdata(1,2)=mdata(1,2);%payment row 2
%i think i made a mistake on the code lol
mdata(1,1)=mdata(1,5); %beginning balance row 2
mdata(1,2)=mdata(1,2);%payment row 2
mdata(1,3)=mdata(1,1)*1.5/12; %interest row 2
mdata(1,4)=mdata(1,2)-mdata(1,3); %principal row 2
mdata(1,5)=mdata(1,1)-mdata(1,4); %ending balance row 2
DGM
DGM il 5 Apr 2021
Modificato: DGM il 5 Apr 2021
Perhaps something like this?
% these initial conditions need to satisfy the same
% relationships as the rest of the data.
A=5000;
B=1000; % i picked something that would allow more steps
C=A*1.5/12;
D=B-C;
E=A-D;
mdata=[A B C D E];
i=1;
while mdata(i,5)>0
mdata=mdata([1:end,end],:); % extend the array
mdata(i+1,1)=mdata(i,5); %beginning balance
mdata(i+1,2)=mdata(i,2); %payment
mdata(i+1,3)=mdata(i,1)*1.5/12; %interest
mdata(i+1,4)=mdata(i,2)-mdata(i,3); %principal
mdata(i+1,5)=mdata(i,1)-mdata(i,4); %ending balance
i=i+1;
end
mdata
gives:
mdata =
1.0e+03 *
5.0000 1.0000 0.6250 0.3750 4.6250
4.6250 1.0000 0.6250 0.3750 4.6250
4.6250 1.0000 0.5781 0.3750 4.2500
4.2500 1.0000 0.5781 0.4219 4.2500
4.2500 1.0000 0.5312 0.4219 3.8281
3.8281 1.0000 0.5312 0.4688 3.8281
3.8281 1.0000 0.4785 0.4688 3.3594
3.3594 1.0000 0.4785 0.5215 3.3594
3.3594 1.0000 0.4199 0.5215 2.8379
2.8379 1.0000 0.4199 0.5801 2.8379
2.8379 1.0000 0.3547 0.5801 2.2578
2.2578 1.0000 0.3547 0.6453 2.2578
2.2578 1.0000 0.2822 0.6453 1.6125
1.6125 1.0000 0.2822 0.7178 1.6125
1.6125 1.0000 0.2016 0.7178 0.8948
0.8948 1.0000 0.2016 0.7984 0.8948
0.8948 1.0000 0.1118 0.7984 0.0963
0.0963 1.0000 0.1118 0.8882 0.0963
0.0963 1.0000 0.0120 0.8882 -0.7918
DGM
DGM il 5 Apr 2021
Modificato: DGM il 5 Apr 2021
Okay, then you should just use the A,B,C,D,E that you were using if that's what you want. You're not generating any new rows because you're always working on row 1.
A=5000;
B=4000;
C=3000;
D=2000;
E=1000;
mdata=[A B C D E];
i=1;
while mdata(i,5)>0
mdata=mdata([1:end,end],:); % extend the array by one row
% i+1 is this row; i is prior row
mdata(i+1,1)=mdata(i,5); %beginning balance (ending balance from last row)
mdata(i+1,2)=mdata(i,2); %payment (same as last row)
mdata(i+1,3)=mdata(i+1,1)*1.5/12; %interest (based on balance) (150% PY?)
mdata(i+1,4)=mdata(i+1,2)-mdata(i+1,3); %principal payment (from this row)
mdata(i+1,5)=mdata(i+1,1)-mdata(i+1,4); %ending balance (from this row)
% make sure the right row is being used in the RHS of these
i=i+1;
end
Because of the initial conditions you've chosen though, you'll never get more than two lines. Line 1 says you started out with $5000 plus $3000 interest, and you paid $4000. Not only is that an implied 720% PY interest rate, you're saying that your principal payment is $4000 - $3000=$2000, which doesn't add up. Your ending balance of $5000-$2000=$1000 means that next month's payment will easily pay the entire remaining balance plus interest.
Phoebe
Phoebe il 5 Apr 2021
omg thanks alot DGM, i have cracked my brain to figure this thingy out for several days now. You are really helpful. many thanks !!!
Hi DGM, sorry to bother again, one last question, can you explain the logic of this line ...I really want to understand instead of blindly using this line of code because I'm new to programming so yeaxD
I did a bit of research before asking, here's what i can understand for now:
mdata=mdata([1:end,end],:); % extend the array
%[]=brackets, enclosures array elements
%:=colon, generates regularly spaced elements and represents an
%entire row or column (here's =entire column right?)
%this is the confusing part...
%1:end means from (1,1) to (1,end) right?
%and ,end means?
%[1:end,end]=[mdata(1,1),mdata(1,end);
%mdata(1,end),mdata(end,end)]
DGM
DGM il 5 Apr 2021
Modificato: DGM il 6 Apr 2021
The more general way to think of square brackets is as concatenation operators. Colon operators work as you say, but sometimes it's easy to lose track of operator precedence when using them. The way that first index works is like this:
A=1:5; % A is a uniformly spaced vector
B=5; % B is a scalar
[A,B] % horizontally concatenate vector A and scalar B
Of course the end operator only works within the context of the expression. If I'd tried to write it out like this using end, it wouldn't work.
So I'm just saying I want to replicate the last vector element
mdata=mdata([1 2 3 4 5 5],:);
In this case, I'm not actually using the replicated values for anything, so I could've done this.
mdata=[mdata; [0 0 0 0 0]];
I can't say that wouldn't have been a safer way to do it. It's probably more readable. I don't know which is faster.
Whenever you're having issues trying to figure out how to get your indexing expressions to work out right, it's helpful to just reduce them to simple tests.
x=1:10 % make a short linear vector
b=x([1:2:end,2:2:end]) % test an idea
Phoebe
Phoebe il 6 Apr 2021
Looks like I still have a long way to go, thank you so much!

Accedi per commentare.

Più risposte (0)

Prodotti

Richiesto:

il 4 Apr 2021

Modificato:

DGM
il 6 Apr 2021

Community Treasure Hunt

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

Start Hunting!

Translated by