フィルターのクリア

How to replace general matrix elements in an equation with array elements?

1 回表示 (過去 30 日間)
Abhishek Pandey
Abhishek Pandey 2016 年 11 月 10 日
編集済み: the cyclist 2016 年 11 月 10 日
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?

採用された回答

the cyclist
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 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by