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

1 回表示 (過去 30 日間)
Devon Leukes
Devon Leukes 2019 年 1 月 26 日
回答済み: Chirag Sarda 2023 年 6 月 28 日
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
i=i+1;
end
Y = Institution_data(1:784,i);
i % I've only put this here to track the value of i as the code runs
end
end

回答 (1 件)

Chirag Sarda
Chirag Sarda 2023 年 6 月 28 日
You can use continue. To know how to use that you can refer this

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by