How to store values in a loop?

조회 수: 3 (최근 30일)
Boni_Pl
Boni_Pl 2020년 1월 23일
댓글: Boni_Pl 2020년 1월 24일
Hello
for i1=1:size(sen,1)
for j1=1:size(r2,2)
dist_de_grid1(j1)=sqrt((sen(i1,1)-r2(1,j1)).^2+(sen(i1,2)-c2(1,j1)).^2);
min1=min(dist_de_grid1);
[d1,d2]=find(dist_de_grid1==min1);
d11=x_grid(d2,1);d12=y_grid(d2,1);
end
end
In this loop how to store d11 and d12 values. size of d11 and d12 is same as size of i1. please help.

채택된 답변

Jesus Sanchez
Jesus Sanchez 2020년 1월 23일
Just fill them like they are vectors. MATLAB can add new "cells" to each vector on the fly, or you can pre-define them for speed. As you said the length, I predefined them in this code, but if you comment it, it should be fine.
d11 = zeros(size(sen,1),1);
d12 = zeros(size(sen,1),1);
for i1=1:size(sen,1)
for j1=1:size(r2,2)
dist_de_grid1(j1)=sqrt((sen(i1,1)-r2(1,j1)).^2+(sen(i1,2)-c2(1,j1)).^2);
min1=min(dist_de_grid1);
[d1,d2]=find(dist_de_grid1==min1);
d11(i1)=x_grid(d2,1);
d12(1i)=y_grid(d2,1);
end
end
  댓글 수: 1
Boni_Pl
Boni_Pl 2020년 1월 24일
Thanks for the help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by