Real quick one, passing through a matrix backwards or just flip it.

3 ビュー (過去 30 日間)
Scragmore
Scragmore 2011 年 10 月 27 日
During a for or while loop I would like to start at the back of the matrix and cycle to the beginning. Or do I just flip it using a(end:-1:1,:) or flipud(a). I understand flipud but can you briefly explain the syntax for a(end:-1:1,:).
Thanks AD

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 10 月 27 日
a=rand(8,9);
b=a(5:end,5:end)
"end" here is the length of that dimension.
1:end is the same as [1,2,3,...,end]
end:-1:1 is the opposite. It is the same as [end, end-1, end-2, end-3, ... 1].

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 10 月 27 日
Cycling the loop backwards
for K=size(A,1):-1:1
can be quite efficient, and seldom requires more memory than working forwards (indeed, moving forwards can be less efficient if you have not preallocated a matrix.)

カテゴリ

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