can anyone please explain X0(1:2) where X0=[0,0,0]' , i wrote X0(2:2) and gave answer 0 i tht it shoudl give error . what i understand is X0(1:2) is 1 row 2 element. please correct me

2 vues (au cours des 30 derniers jours)
can anyone please explain X0(1:2) where X0=[0,0,0]' , i wrote X0(2:2) and gave answer 0 i tht it shoudl give error . what i understand is X0(1:2) is 1 row 2 element. please correct me
also i made a small 2x2 matrix P and
p =
2 3
4 5
>> p(1:2)
ans =
2 4
>> p(2:2)
ans =
4

Réponses (2)

dpb
dpb le 4 Nov 2020
Linear addressing. Arrays are stored in column-major order.
See https://www.mathworks.com/help/matlab/math/array-indexing.html for discussion of various addressing modes/syntax.

Steven Lord
Steven Lord le 4 Nov 2020
v = 2:2 is the vector formed by starting at 2 and going to 2 by steps of 1. That vector is equivalent to writing v = [2] or v = 2.
The expression A(v) where v is a numeric vector performs linear indexing. See the "Indexing with a Single Index" section on this documentation page or this blog post for more infomation on linear indexing.
v = 1:10;
x = v.^2;
y = x([4 5]) % Elements 4 and 5 of x. From the way we created x this is [16, 25]
y = 1×2
16 25
If you had an array and wanted to get the element in the fourth row, fifth column using 4 and 5 that would be subscripted indexing. That documentation page to which I linked above has a section ("Indexing with Element Positions") on subscripted indexing as well.
M = magic(6)
M = 6×6
35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11
z = M(4, 5)
z = 10

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by