MATLAB Answers

Nested For Loop to run through a single matrix at two different rate

2 views (last 30 days)
Devon Leukes
Devon Leukes on 26 Jan 2019
I have a nx23 matrix which I am looping through to get two separate variables X and Y (each of them being an nx1 column vector) but X and Y can never be the same column in the matrix. I'm using a nested For Loop since the looping has to happen at different rates, i.e. When X is the 1st column of the matrix, then the value of Y has to loop through the remaining 22 columns of the matrix (columns 2:23). Once Y has looped through all the columns, the value of X changes to the 2nd column of the matrix, and Y again loops through the remaining 22 columns. However, Y now has to start at column 1, skip column 2 (Y not equal to X), and continue through the rest of the columns. This continues until X equals the last column of the matrix.
The problem I have is that when Y=X and Y skips to the next column, the value of Y is repeated in the next loop. So when k=1, then the value of i = 2,2,3......23. When k=2, the value of i = 1,2,3,3,4......23, etc.
How do I prevent the repitition?
%I've only pasted the relevant section of code.
for k=1:23 % this is the loop to get X
X = Institution_data(1:784,k);
for i=1:23 % this is the loop to get Y
if (i==k) % X and Y cannot be the same value
Y = Institution_data(1:784,i);
i % I've only put this here to track the value of i as the code runs

Answers (0)

Community Treasure Hunt

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

Start Hunting!