How do I sort automatic variables in matlab?
2 次查看(过去 30 天)
显示 更早的评论
David Alejandro Ramirez Cajigas
2021-7-21
评论: David Alejandro Ramirez Cajigas
2021-8-18
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 个评论
Stephen23
2021-7-21
"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:
采纳的回答
Rik
2021-7-21
编辑:Rik
2021-7-21
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
5 个评论
Rik
2021-7-21
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.
更多回答(1 个)
Chunru
2021-7-21
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)
size(d{2})
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
3 个评论
Chunru
2021-7-22
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];
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!