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 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
dpb
2020-11-4
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.
0 个评论
Steven Lord
2020-11-4
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]
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)
z = M(4, 5)
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!