How do I sort automatic variables in matlab?

조회 수: 1 (최근 30일)
David Alejandro Ramirez Cajigas
David Alejandro Ramirez Cajigas 2021년 7월 21일
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
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:
David Alejandro Ramirez Cajigas
David Alejandro Ramirez Cajigas 2021년 7월 21일
wow thanks

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

채택된 답변

Rik
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
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
Rik
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.
David Alejandro Ramirez Cajigas
David Alejandro Ramirez Cajigas 2021년 8월 18일
@Rik ahahha i have new qustion on: https://la.mathworks.com/matlabcentral/answers/1435772-count-pixels-within-a-specified-range

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

추가 답변 (1개)

Chunru
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)
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
David Alejandro Ramirez Cajigas
David Alejandro Ramirez Cajigas 2021년 7월 21일
the excel file i use colum with name lon and lat
lon= axes X
lat= axes Y
Chunru
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 CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by