How to find distance using loop?

7 views (last 30 days)
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])

Accepted Answer

rumin diao
rumin diao on 6 Sep 2022
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 Comments
rumin diao
rumin diao on 8 Sep 2022
the d(i,j) you want maybe is:
sqrt(sum((IRS(:,i) - Users(:,j)).^2))
you can have a try
Zarghuna Suhail
Zarghuna Suhail on 14 Sep 2022
It worked , thanks alot!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!