Help with a vector
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lavorizia Vaughn
el 11 de Sept. de 2021
Respondida: Walter Roberson
el 11 de Sept. de 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 comentarios
Respuesta aceptada
Walter Roberson
el 11 de Sept. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!