Help with a vector

5 ビュー (過去 30 日間)
Lavorizia Vaughn
Lavorizia Vaughn 2021 年 9 月 11 日
回答済み: Walter Roberson 2021 年 9 月 11 日
Hello. My prof gave this code in class but I dont really understand how X(2:1:-1, 3:-1:1) produced ans2 = [6, 5, 4; 3, 2, 1]. His code is as follows:
X=[1,2,3;4,5,6]
ans1= [1, 2, 3; 4, 5, 6]
X(2:-1:1, 3:-1:1)
ans2= [6, 5, 4; 3, 2, 1]
Can someone please explain to me how ans2 is computed? Thank you.

採用された回答

Walter Roberson
Walter Roberson 2021 年 9 月 11 日
X=[1,2,3;4,5,6]
X(2:-1:1, 3:-1:1)
2:-1:1 is the list of integers [2 1]
3:-1:1 is the list of integers [3 2 1]
So that is a call to index
X([2 1], [3 2 1])
When you index with vectors or arrays, MATLAB produces an output for each combination of entries. So it is going to produce an output for X(2, [3 2 1]) and an output for X(1, [3 2 1]) . In turn, X(2, [3 2 1]) would produce an output for X(2, 3), X(2,2), X(2,1)
So the output would be
[X(2,3), X(2, 2), X(2,1)
X(1,3), X(1, 2), X(1,1)]

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by