Indexing in a matrix vs vector
Show older comments
Why can we write A(1,2) for a nx2 matrix, with n>1, and we cannot write i=1; x(i,2) for a vector x = [x1 x2]?
It only accepts x(i+1), but I need to iterate the index i over the rows and not the columns, for any nx2 matrix with n>=1.
Thank you in advance.
Accepted Answer
More Answers (1)
i = 1;
x = [10, 20]
x(i, 2) % Display second column of first (and only) row of a row vector.
I'm not seeing the problem. It works. You must be describing something different than your actual code.
Contrary to what you said, it will not accept x(i+1, 2), which is x(2,2), because there is no second row of x. Remember row is the first index and column is the second index.
I encourage you to use descriptive names like row and col instead of i and j when using indexes in arrays. It will avoid a lot of mistakes.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!