Indexing a value from other cell arrays into a new column

1 vue (au cours des 30 derniers jours)
Aaron Devanathan
Aaron Devanathan le 8 Avr 2021
Hi everyone. I am trying to do some multiple arrays indexing in MATLAB. I've attached a workspace that contains these matrices to this question ("indexing.mat").
What I would like to do is to read each of the rows of the first column of the 'distances' matrix. Each of the values refers to that specific row of the 'indeces' matrix that contains an (x,y) pair in two columns. I would then like to abstract the value in the 'arrays' matrix based on that (x,y) pair and place it in the third column of the 'distances' matrix in that same row.
For example: row 12, first column of 'distances' is 411. Row 411 in 'indeces' is (30,92). That pair corresponds to 105.91090 in 'arrays'. I'd then like to place that value in row 12, third column of 'distances'.
Is there a way to automate this? I can try to do it manually but it's becoming labor-intensive. Thank you for your insight!

Réponse acceptée

David Hill
David Hill le 8 Avr 2021
for k=1:size(distances,1)
a=flip(indeces(distances(k,1),:));
distances(k,3)=arrays(a(1),a(2));
end
  5 commentaires
David Hill
David Hill le 9 Avr 2021
[a,b]=find(arrays);
for k=1:size(distances,1)
c=flip(indeces(distances(k,1),:));
if isequal(arrays(c(1),c(2)),0)
r=[a,b]-[c(1),c(2)];
R=hypot(r(:,1),r(:,2));
[~,idx]=min(R);
distances(k,3)=arrays(a(idx),b(idx));
else
distances(k,3)=arrays(c(1),c(2));
end
end
Aaron Devanathan
Aaron Devanathan le 9 Avr 2021
Thank you! I really appreciate your help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by