How to replace general matrix elements in an equation with array elements?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Abhishek Pandey
il 10 Nov 2016
Modificato: the cyclist
il 10 Nov 2016
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 Commenti
Risposta accettata
the cyclist
il 10 Nov 2016
Modificato: the cyclist
il 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!