Indexing a value from other cell arrays into a new column

1 view (last 30 days)
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!

Accepted Answer

David Hill
David Hill on 8 Apr 2021
for k=1:size(distances,1)
a=flip(indeces(distances(k,1),:));
distances(k,3)=arrays(a(1),a(2));
end
  5 Comments
David Hill
David Hill on 9 Apr 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

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!