Matrix indexed by another matrix in matlab? -


i have following matrices:

a=zeros(2,4); d=[ 1 2;     3 4;     5 6;     7 8];  v=rand(1,8); 

for example:

v= [0.8147    0.9058    0.1270    0.9134    0.6324    0.0975    0.2785    0.5469] 

now when run a(d)=v, a becomes:

a=[0.8147    0.9058    0.1270    0.9134;    0.6324    0.0975    0.2785    0.5469] 

when change d entries values, different configurations of a, example, if put:

d=[ 8 7;     6 5;     4 3;     2 1]; 

then a becomes:

a=[0.5469    0.2785    0.0975    0.6324;    0.9134    0.1270    0.9058    0.8147] 

does 1 know kind of indexing is?

so make clearer lets redefine v

v = 10:10:80 

(i.e. v = [10 20 30 40 50 60 70 80];)

now when

 d=[8 7;     6 5;      4 3;      2 1]; 

then

a(d)=v      =      80    70    60    50     40    30    20    10 

lets @ how works. firstly when index a d, d gets flattened a(d) = v same a(d(:)) = v (test it!) ,

d(:)  ans =       8      6      4      2      7      5      3      1 

so breaking down element element we're going a(d(1)) = v(1) after substituting d(1) , v(1) a(8) = 10, hence last element 10. lets few elements further. a(d(4)) = v(4) become a(2) = 40. element a(2)? linear indexing counts down rows first (column major ordering) i.e.

a(1) == a(1,1) a(2) == a(2,1) a(3) == a(1,2) a(4) == a(2,2) a(5) == a(1,3) a(6) == a(2,3) etc... 

so a(2) in (2,1) position etc...


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -