Help with a vector
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Lavorizia Vaughn
il 11 Set 2021
Risposto: Walter Roberson
il 11 Set 2021
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 Commenti
Risposta accettata
Walter Roberson
il 11 Set 2021
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!