Help with a vector

4 次查看(过去 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.

采纳的回答

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 CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by