Indexing a value from other cell arrays into a new column

1 回表示 (過去 30 日間)
Aaron Devanathan
Aaron Devanathan 2021 年 4 月 8 日
コメント済み: Aaron Devanathan 2021 年 4 月 9 日
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
Aaron Devanathan
Aaron Devanathan 2021 年 4 月 9 日
Thank you! I really appreciate your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by