Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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?

Respuesta aceptada

the cyclist
the cyclist el 10 de Nov. de 2016
Editada: the cyclist el 10 de Nov. de 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.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by