Help with a vector
5 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!