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 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