Which function should I use to fill an ouput matrix based on information in two other matrices

1 vue (au cours des 30 derniers jours)
I want to fill up an Ouput matrix row-wise, by looking into the Index information and Data.
For example, if I have three Matrices
Index = [1 2 1; 5 4 4], Data = [12 16 10; 13 16 18; 12 11 14; 18 19 22; 23 60 17],
Now loop will start from the first row of "Index" and look into the "Data" column "1". The output for say, first two loops should be:
Output = [12 13 12; 60 19 19]
Which function should I use? Histcount, accumarray or any other?

Réponse acceptée

KSSV
KSSV le 7 Mai 2021
Index = [1 2 1; 5 4 4];
Data = [12 16 10; 13 16 18; 12 11 14; 18 19 22; 23 60 17] ;
Output = [12 13 12; 60 19 19] ;
iwant = zeros(size(Index)) ;
for i = 1:size(Index,1)
iwant(i,:) = Data(Index(i,:)',i) ;
end
iwant
  1 commentaire
Sehrish Usman
Sehrish Usman le 7 Mai 2021
Thank you KSSV, I also just figured out that I can just mention the index to get the output like you did.

Connectez-vous pour commenter.

Plus de réponses (1)

DGM
DGM le 7 Mai 2021
Modifié(e) : DGM le 7 Mai 2021
This is probably not the best way to do this, but here goes.
idx = [1 2 1; 5 4 4]
D = [12 16 10; 13 16 18; 12 11 14; 18 19 22; 23 60 17]
didx = sub2ind(size(D),idx,repmat((1:size(idx,1)).',[1 size(idx,2)]));
A = D(didx)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by