How to find distance using loop?
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I have to find the distance between IRS and users in 3D, for example from IRS 1 to user1, user2, user 3, user 4 and then from IRS 2 to all the users and so on. I have total 4 users and 8 IRS, I found the distances for each one using norm but now I want to find the distances using a loop so how will the loop work? New to using Matlab and I'm a little bit lost with it. Any answers would be appreciated.
%Distance of IRSs from all the users
IRS1U1=norm([3,1.5,1.5]-[6,8,1])
IRS1U2=norm([3,1.5,1.5] - [6,3,1.5])
IRS1U3=norm([3,1.5,1.5] - [6.5,5.5,1.5])
IRS1U4=norm([3,1.5,1.5] - [9,5.5,1.5])
IRS2U1=norm([6,1,1.5]-[6,8,1])
IRS2U2=norm([6,1,1.5] - [6,3,1.5])
IRS2U3=norm([6,1,1.5] - [6.5,5.5,1.5])
IRS2U4=norm([6,1,1.5] - [9,5.5,1.5])
IRS3U1=norm([8.5,1,1.5]-[6,8,1])
IRS3U2=norm([8.5,1,1.5] - [6,3,1.5])
IRS3U3=norm([8.5,1,1.5] - [6.5,5.5,1.5])
IRS3U4=norm([8.5,1,1.5] - [9,5.5,1.5])
IRS4U1=norm([10.4,2,1.5]-[6,8,1])
IRS4U2=norm([10.4,2,1.5] - [6,3,1.5])
IRS4U3=norm([10.4,2,1.5] - [6.5,5.5,1.5])
IRS4U4=norm([10.4,2,1.5] - [9,5.5,1.5])
IRS5U1=norm([3,10.5,1.5]-[6,8,1])
IRS5U2=norm([3,10.5,1.5] - [6,3,1.5])
IRS5U3=norm([3,10.5,1.5] - [6.5,5.5,1.5])
IRS5U4=norm([3,10.5,1.5] - [9,5.5,1.5])
IRS6U1=norm([6,10.5,1.5]-[6,8,1])
IRS6U2=norm([6,10.5,1.5] - [6,3,1.5])
IRS6U3=norm([6,10.5,1.5] - [6.5,5.5,1.5])
IRS6U4=norm([6,10.5,1.5] - [9,5.5,1.5])
IRS7U1=norm([8.5,10.5,1.5]-[6,8,1])
IRS7U2=norm([8.5,10.5,1.5] - [6,3,1.5])
IRS7U3=norm([8.5,10.5,1.5] - [6.5,5.5,1.5])
IRS7U4=norm([8.5,10.5,1.5] - [9,5.5,1.5])
IRS8U1=norm([10.5,9,1.5]-[6,8,1])
IRS8U2=norm([10.5,9,1.5] - [6,3,1.5])
IRS8U3=norm([10.5,9,1.5] - [6.5,5.5,1.5])
IRS8U4=norm([10.5,9,1.5] - [9,5.5,1.5])
0 个评论
采纳的回答
rumin diao
2022-9-6
you can use two loops:
dist = zeros(8,4); % the matrix to save distances
for i = 1 : 8 % use this loop to get IRS1-IRS8
for j = 1 : 4 % use this loop to get 4 users
%calculate distance and save it to matrix
dist(i,j) = norm();% you can fill in the statement cause im not sure the meaning of arrays you use in 'norm';
end
end
3 个评论
rumin diao
2022-9-8
the d(i,j) you want maybe is:
sqrt(sum((IRS(:,i) - Users(:,j)).^2))
you can have a try
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!