Indice wise reference of indice from another matrix

1 Ansicht (letzte 30 Tage)
Valerian Cinçon
Valerian Cinçon am 25 Mär. 2019
Kommentiert: Valerian Cinçon am 25 Mär. 2019
I search a better writing (without for loop) than the following functionning code.
the code write on the 3rd dimmension of A_delayed wisely to an indice stored in a separate matrice.
For each row,colum of A the 3rd dimmension indice can be different.
for i10=1:row_A
for i11=1:colum_A
A_delayed(i10,i11,matrice_delay(i10,i11))=A(i10,i11);
end
end
Thank in advance.
  4 Kommentare
madhan ravi
madhan ravi am 25 Mär. 2019
Illustration needed with a simple example.
Valerian Cinçon
Valerian Cinçon am 25 Mär. 2019
>> A= [0 1 ; 1 1];
matrice_delay = [1 1 ; 2 3];
A_delayed = zeros (2,2,3);
for i10=1:2
for i11=1:2
A_delayed(i10,i11,matrice_delay(i10,i11))=A(i10,i11);
end
end
>> A_delayed
A_delayed(:,:,1) =
0 1
0 0
A_delayed(:,:,2) =
0 0
1 0
A_delayed(:,:,3) =
0 0
0 1

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 25 Mär. 2019
Bearbeitet: Stephen23 am 25 Mär. 2019
Use ndgrid and sub2ind:
>> A = [0,1;1,1];
>> D = [1,1;2,3];
>> B = zeros(2,2,3);
>> S = size(B);
>> [R,C] = ndgrid(1:S(1),1:S(2));
>> B(sub2ind(S,R,C,D)) = A
B(:,:,1) =
0 1
0 0
B(:,:,2) =
0 0
1 0
B(:,:,3) =
0 0
0 1
  1 Kommentar
Valerian Cinçon
Valerian Cinçon am 25 Mär. 2019
Dear Stephen,
Thank you for your wonderful contribution.
Best regards,
Valérian,

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by