How to do while loop on arrays?

2 views (last 30 days)
Phoebe
Phoebe on 4 Apr 2021
Edited: DGM on 6 Apr 2021
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 Comments
David Fletcher
David Fletcher on 4 Apr 2021
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 on 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

Sign in to comment.

Accepted Answer

DGM
DGM on 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 Comments
DGM
DGM on 5 Apr 2021
Edited: DGM on 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 on 6 Apr 2021
Looks like I still have a long way to go, thank you so much!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!