フィルターのクリア

Can I use a for loop to access different pairs in a matrix?

7 ビュー (過去 30 日間)
darksideofthemoon101
darksideofthemoon101 2011 年 4 月 4 日
I want to use a matrix as the input of a for loop, and reference different pairs of indices in the matrix as the variable for each iteration.
Say I have a matrix that looks like
1 4
2 5
3 6
I want the loop to choose [1 4],[1 5] then [1 6], then go back to the first column and choose [2 4], [2,5] and so on.
Is this possible?

採用された回答

Walter Roberson
Walter Roberson 2011 年 4 月 4 日
for idxpair = [1 1 1 2 2 2; 4:6 4:6]
That is, when "for" is given an array it iterates over the columns.
Normally though, you would use
for i1 = 1:2
for i2 = 4:6
...
end
end

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 4 月 4 日
for ii = 1:3
for jj = 4:6
do_stuff_with(ii,jj);
end
end
What operation are you doing with the two variables. It's possible you could use BSXFUN to do it all with one shot.
  2 件のコメント
darksideofthemoon101
darksideofthemoon101 2011 年 4 月 4 日
I have a for loop that I feed a number of variables. I'm wanting to look at a broad range of data from the variables, so I was looking for something slightly more elegant than a series of nested loops.
What does BSXFUN do?
Sean de Wolski
Sean de Wolski 2011 年 4 月 4 日
doc bsxfun

サインインしてコメントする。

カテゴリ

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