How to find distance using loop?

조회 수: 2 (최근 30일)
Zarghuna Suhail
Zarghuna Suhail 2022년 9월 6일
댓글: Zarghuna Suhail 2022년 9월 14일
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])

채택된 답변

rumin diao
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
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
Zarghuna Suhail
Zarghuna Suhail 2022년 9월 14일
It worked , thanks alot!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by