1

I have the 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 I run A(D)=v, A becomes:

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

When I change D entries to another values, I get different configurations of A, for example, if I 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 any one know what this kind of indexing is?

1

1 Answer 1

2

So to make it clearer lets redefine your v as

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

    A =

    80    70    60    50
    40    30    20    10

Lets look at how this works. So firstly when you index A by D, D gets flattened so A(D) = v is the same as A(D(:)) = v (test it!) and

D(:)

ans =

     8
     6
     4
     2
     7
     5
     3
     1

So for breaking it down element by element we're going A(D(1)) = v(1) which after substituting for D(1) and v(1) is A(8) = 10, hence the last element is 10. Lets look a few elements further. A(D(4)) = v(4) become A(2) = 40. but which element is A(2)? Well linear indexing counts down the 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) is in the (2,1) position etc...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.