MATLAB nested loop

You can post your problem related to MATLAB Projects here. We will try our best to help you out.
Post Reply
Charles
Posts: 20
Joined: Sat Feb 18, 2017 7:12 am

MATLAB nested loop

Post by Charles » Mon Mar 06, 2017 12:20 pm

Code: Select all

function[output]=tridiag(d,l,r)
A=zeros(3);
for i=1:5 
    A(i,i)=d(i);
    for j=2:5
        A(j,(i-1))=l(j);
        for k=1:4
            A(k,(i+5))=r(k);
        end
    end
end
A
end
on this part where it says,

Code: Select all

for j=2:5
    A(j,(i-1))=l(j);
I want the variable "i" to execute from 1 ~ 4. and not 5. That's why I put i-1 there, but obviously it won't work.

How can I make the nested j-loop to repeat i=1:4, instead of i=1:5? Same question goes for k-loop too.

+update: So I want the outside i-loop to perform from 1:5, but for the inner j-loop, i want i=1:4.

Post Reply