Indexing a value from other cell arrays into a new column

1 次查看(过去 30 天)
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!

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by