How do I sort automatic variables in matlab?

ok, I have a question first the context:
I have a series of data that are cartecianas coordinates, I have these coordinates in an excel, with their respective x (longitude) and y (latitude). So far no problem is imported into matlab with the command T = readtable ('name.xlsx').
I need to find how much distance there is between coordinate (i use a simple ecuation sqrt ) 1 and each of the coordinates that follow. then the coordinate 2 and those that follow, then the coordinate 3 and those that continue to N. for which I have the code:
clear all
close all
clc
tic
T=readtable('mini2.xlsx')
numeroDeElementos=length(T.lon)
plot(T.lon,T.lat,'ko');
n=numeroDeElementos;
for i=1:n
F(i)=(sqrt((T.lon(i)-T.lon(1))^2+((T.lat(i)-T.lat(1))^2)))*100000; %obtengo automaticamente los datos de distancia desde punto 1
end
The problem is that now I have the distance between point 1 and the following, but I don't know how to make matlab generate automatic variables up to Length (n), that is, the variable 1 would be the F that goes from the coordinate 1 measuring to each one . I need matlab to generate a 2F, 3F, 4F .... NF variable.
the number of coordinates varies in each file that is why it is necessary to use Length, but I cannot obtain the following distances.
The idea at the end is to have a matrix or an excel or tables, where is the length from position 1 to each one, then position 2 to each 1 of them and so on until N (n given by Length)
The general idea is: I need a code that automatically generates as many variables as I need or that introduces the response columns in a matrix.
The distance from each coordinate point must be measured, to each coordinate point.
for example this data have only 13 point but, i can have 10000 point or more
Thanks

2 Comments

"I need matlab to generate a 2F, 3F, 4F .... NF variable."
I doubt that you "need" MATLAB to generate dynamic variable names.
Most likely that would be a very complex and inefficient approach:

Sign in to comment.

 Accepted Answer

You shouldn't generate variable names dynamically. You should use an array instead. That way you can use all the normal Matlab tools and you won't be forced to generate the variable name every time you want to use it.
In this case there is a function in the Statistics and Machine Learning Toolbox: pdist. That should do exactly what you want.
%load the data from the comment on the other answer
websave('mini2.xlsx','https://www.mathworks.com/matlabcentral/answers/uploaded_files/691498/mini2.xlsx');
T=readtable('mini2.xlsx');
X=[T.lon T.lat];
D=squareform(pdist(X));
D
D = 13×13
0 0.0046 0.0054 0.0052 0.0043 0.0077 0.0084 0.0060 0.0037 0.0058 0.0034 0.0016 0.0005 0.0046 0 0.0039 0.0039 0.0003 0.0031 0.0038 0.0020 0.0014 0.0017 0.0018 0.0031 0.0044 0.0054 0.0039 0 0.0002 0.0039 0.0050 0.0056 0.0030 0.0029 0.0031 0.0028 0.0040 0.0049 0.0052 0.0039 0.0002 0 0.0038 0.0051 0.0058 0.0031 0.0028 0.0031 0.0027 0.0038 0.0047 0.0043 0.0003 0.0039 0.0038 0 0.0034 0.0041 0.0022 0.0013 0.0019 0.0016 0.0029 0.0042 0.0077 0.0031 0.0050 0.0051 0.0034 0 0.0007 0.0021 0.0041 0.0021 0.0045 0.0061 0.0075 0.0084 0.0038 0.0056 0.0058 0.0041 0.0007 0 0.0028 0.0048 0.0028 0.0052 0.0068 0.0082 0.0060 0.0020 0.0030 0.0031 0.0022 0.0021 0.0028 0 0.0023 0.0003 0.0026 0.0044 0.0057 0.0037 0.0014 0.0029 0.0028 0.0013 0.0041 0.0048 0.0023 0 0.0021 0.0004 0.0021 0.0034 0.0058 0.0017 0.0031 0.0031 0.0019 0.0021 0.0028 0.0003 0.0021 0 0.0025 0.0042 0.0055

5 Comments

Thanks, I had thought of using D = pdist2 (X, Y, Distance), but I did not know how to make it work with the list that I extracted from the excel file.
also I do not know how to generate the recursive succession, in which the first list of numbers is calculated between position 1 and 2, 1 and 3, etc. and then restart the same with position 2, then with position 3 until n
Rik
Rik on 21 Jul 2021
Edited: Rik on 21 Jul 2021
Scratch that, you need pdist, instead of pdist2. I'll edit my answer.
Thank you very much for your time.
I'm thankful
i have more question
gracias, merci
You're welcome. If my answer solved your question, please consider marking it as accepted answer.
If you have other questions, feel free to post them and post a link here or @ mention me in a comment.
@Rik ahahha i have new qustion on: https://la.mathworks.com/matlabcentral/answers/1435772-count-pixels-within-a-specified-range

Sign in to comment.

More Answers (1)

You have N points, , n=1, ..., N. You want to compute the distance between any two points. So there are pairs altogeter, which can be represented by a square matrix D of size . Along the diagonal, the entries should be 0. The distance matrix is also symetric. In theory, you just need to store the upper(or lower) triangular matrix (exluding diagonal). If memory is a big concern, you can consider to use cell array to store the lower(or upper) trianglular matrix:
n = 6;
p = randn(n, 2);
for i=1:n-1
for j=i+1:n
d{i}(j-i) = norm(p(i,:)-p(j,:));
end
end
(d)
d = 1×5 cell array
{[1.7054 1.8645 1.2762 1.2106 0.6200]} {[2.7648 2.9329 2.9061 2.3235]} {[2.4279 2.1906 1.9004]} {[0.2549 0.7000]} {[0.5943]}
size(d{2})
ans = 1×2
1 4
If memory is not an issue ( wasting half of the elements of d1), then the following is easire to manage:
d1 = zeros(n, n);
for i=1:n-1
for j=i+1:n
d1(i, j) = norm(p(i,:)-p(j,:));
end
end
d1
d1 = 6×6
0 1.7054 1.8645 1.2762 1.2106 0.6200 0 0 2.7648 2.9329 2.9061 2.3235 0 0 0 2.4279 2.1906 1.9004 0 0 0 0 0.2549 0.7000 0 0 0 0 0 0.5943 0 0 0 0 0 0

3 Comments

I am very grateful Thanks
i use this code, but de program have error in line 12 " d{i}(j-i) = norm(p(i,:)-p(j,:));"
"p =table(T.lon,T.lat);" the command extracts the table I need from the excel file
Do you have an idea what I could do? ( i need with matlab)
when i replaced the command, the one result should be: 0 0.0046 0.0054 0.0052 0.0043 0.0077 0.0084 0.0060 0.0037 0.0058 0.0034 0.0016 0.0005
and the actual result are :
0 79.9301 79.9336 79.9335 79.9300 79.9316 79.9316 79.9320 79.9308 79.9317 79.9308 79.9299 79.9294
Help!
for i=1:n-1
for j=i+1:n
d1(i, j) = norm(T.lon(i,:)-T.lat(j,:)); %T.lon and T.lat extrac extract columns from excel document
end
end
d1
n = 6; %in my code it is replaced by the count made by the Length (n) command,
p = randn(n, 2); %randomly generate two columns of size N, in my code it is replaced by the
% two columns that it extracts from the excel file
for i=1:n-1 % for bucle
for j=i+1:n %counter to start from 0
d{i}(j-i) = norm(p(i,:)-p(j,:)); % correct way to find distances,
% do not use the long formula that I use
% (F(i)=(sqrt((T.lon(i)-T.lon(1))^2+((T.lat(i)-T.lat(1))^2)))*100000)
end
end
(d)
clear
clear
T=readtable('mini2.xlsx')
H = table(T.lon,T.lat)
U=length(T.lon)
n=U
p =table(T.lon,T.lat);
for i=1:n-1
for j=i+1:n
d{i}(j-i) = norm(p(i,:)-p(j,:));
end
end
(d)
size(d{2})
d1 = zeros(n, n);
for i=1:n-1
for j=i+1:n
d1(i, j) = norm(p(i,:)-p(j,:));
end
end
d1
the excel file i use colum with name lon and lat
lon= axes X
lat= axes Y
The code I used require p to be a matrix of Nx2. You can either modify the distance calculation line or prepare the data to be Nx2, such as
p = [T.lat T.lon];

Sign in to comment.

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!