How to replace general matrix elements in an equation with array elements?
3 views (last 30 days)
Show older comments
I have a matrix A(i,j) and have mapped its elements into an array X(k) column-wise. i and j are restricted till n but k increases linearly till n^2. Specifically,for example,k=1:5 for j=1 and i=1:5 and then k=6:10 for j=2 and i=1:5. I have an algebraic equation containing A(i,j). How can i replace A(i,j) with corresponding X(k) for all i,j?
0 Comments
Accepted Answer
the cyclist
on 10 Nov 2016
Edited: the cyclist
on 10 Nov 2016
This is maybe gonna blow your mind a little ... but you can index into a matrix with a single index. This is called "linear indexing". For example
>> M = magic(3)
M =
8 1 6
3 5 7
4 9 2
>> M(6)
ans =
9
Notice that M(6) is the sixth element, when you count down column-wise.
So, if you simply do ...
A(k) = X(k)
you will assign the element you want to A.
For more complicated procedures, you might need to resort to converting subscripts to linear indices, or vice versa, using sub2ind or ind2sub.
Because you want to do this for the entire matrix, you might just want to reshape the vector into a matrix. See reshape.
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays 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!