Calculating Distances with MATLAB's For Statement

  • MATLAB
  • Thread starter hgphtgi
  • Start date
  • Tags
    Matlab
In summary, a for loop in MATLAB is a control structure used for repeating a section of code a specific number of times. It can be used for both numeric and non-numeric data and is written using the syntax "for index = values, statements, end." It is similar to a while loop, but is used when the number of iterations is known. For loops can also be nested in MATLAB, allowing for more complex operations on data.
  • #1
hgphtgi
12
0
Hello Guys

Can you please tell me whether this code is correct or not??

this code use to calculate all distances from A to J points on the map ( has longitude and latitude ), the starting point coordinates ( Lat and Long ) is 3.682043 and 101.523710 and the destination is 2.227804 103.35091

for p1=3.682043:-0.5:2.227804
for p2=101.523710:0.5:103.735091


a5= 2.227804;
b5=103.35091;

[dDgree] = distance(p1,p2,a5,b5); %% this function used to find a distance between two coordinates

end
end
 

Attachments

  • 2untitled.JPG
    2untitled.JPG
    21.6 KB · Views: 394
Physics news on Phys.org
  • #2
I've never seen a "distance" function- did you write it yourself?
 

What is a for loop in MATLAB?

A for loop in MATLAB is a control structure that allows a section of code to be repeated a specified number of times. It is useful for performing repetitive tasks or iterating through data structures such as arrays.

How do I use a for loop in MATLAB?

To use a for loop in MATLAB, you need to specify the number of iterations and the code to be executed in each iteration. The general syntax is for index = values, statements, end. The index variable is used to control the loop and the values can be a vector or array of numbers.

Can I use a for loop for non-numeric data in MATLAB?

Yes, a for loop can be used for non-numeric data in MATLAB. The values in the loop can be any data type, including strings, characters, or even cells or structures. The index variable will simply iterate through the values in the same order as they are listed.

What is the difference between a for loop and a while loop in MATLAB?

A for loop and a while loop are both control structures in MATLAB used for repetition. The main difference is that a for loop is used when the number of iterations is known, while a while loop is used when the number of iterations is not known and the loop continues until a certain condition is met.

Can I nest for loops in MATLAB?

Yes, it is possible to nest for loops in MATLAB, meaning that one for loop can be placed inside another for loop. This is useful for performing operations on multidimensional data or for iterating through nested data structures.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
Replies
13
Views
3K
Replies
7
Views
825
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Mechanical Engineering
Replies
2
Views
714
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top