How to replace general matrix elements in an equation with array elements?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
the cyclist
2016-11-10
编辑:the cyclist
2016-11-10
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!