I am super new in Matlab.
For a University proyect I need to calculate a co-location quotient, and after calculating, I need to repeat the process 1000 times randomizing the values of the vectors in order to check my results and see the distribution of the quotient.
I managed to write the script but I dont know how to start the simulations.
Could you help me correcting my script ? Thank you in advance.
SM= vertcat(latlonA,latlonB);
random_SM= (SM(randperm(size(SM, 1)), :));
randlatlonA= random_SM(1:NA,:);
randlatlonB= random_SM(1:NB,:);
k = 1; %the number of closest neighbours eligible to be considered
% STEP 3 Calculating the distances
%by using D = pdist2(X,Y,Distance,DistParameter)
axa= pdist2(randlatlonA(:, 1:2), randlatlonA(:, 1:2)); %shows a matrix nxn calculating
%the euclidean distance for latlonA (foods)
axb = pdist2(randlatlonA(:, 1:2), randlatlonB(:, 1:2)); %shows a matrix nxn calculating
%the euclidean distance for latlonA (foods) and latlonB(textile)
bxb = pdist2(randlatlonB(:, 1:2), randlatlonB(:, 1:2)); %shows a matrix nxn calculating
%the euclidean distance for latlonB(textile)
bxa = pdist2(randlatlonB(:, 1:2), randlatlonA(:, 1:2)); %shows a matrix nxn calculating
%the euclidean distance for latlonB(textile) and latlonA (foods)
%STEP 4 Searching for the closest neighbor
%to calculate the smallest distance between each firm and search for the
% closest neighboor we use [Idx,D] = knnsearch(___)
%
% to find the B nearest neighbor for A
[idx,DAB] = knnsearch(randlatlonA(:, 1:2),randlatlonB(:, 1:2), 'k', 1);
% to find the A nearest neighbor for B
[idx2,DBA] = knnsearch(randlatlonB(:, 1:2),randlatlonA(:, 1:2), 'k', 1);
%We are finding every point relative to every other point. That includes finding every
%point relative to itself. Which will be distance 0, which will be "nearest". We want
%to exclude the point to itself, so we need to find k+1 nearest neighbors and then
%ignore the first. (case of A-A and B-B)
% to find the A nearest neighbor for A
[idx3, DAA] = knnsearch(randlatlonA(:, 1:2), randlatlonA(:, 1:2), 'K', k+1); %A-A
idx3 = idx3(:, 2:end); %discard the point to itself
DAA = DAA(:, 2:end); %discard the distance of the point to itself
% to find the B nearest neighbor for B
[idx4, DBB] = knnsearch(randlatlonB(:, 1:2), randlatlonB(:, 1:2), 'K', k+1); %B-B
idx4 = idx4(:, 2:end); %discard the point to itself
DBB = DBB(:, 2:end); %discard the distance of the point to itself
%STEP 5 concatenate A-B, B-A, A-A and B-B in a single matrix
%we will use function called PADCAT (run first PADCAT)
%PADCAT - concatenate arrays with different lengths by padding with NaN
% for the case of A
MA = padcat(DAA,DAB); %for the case of manufactures A
%We should remember that
%DAB (B nearest neightbor of A)
%DBA (A nearest neightbor of B)
%DAA (A nearest neightbor of A)
%DBB (B nearest neightbor of B)
% for the case of B
MB = padcat(DBB,DBA); %for the case of manufactures B
%STEP 6 Finding and counting the nearest neighbor%
MA(any(isnan(MA(:,1)),2),:) = []; %limit only for the number of firms under anlysis
MB(any(isnan(MB(:,1)),2),:) = [];
%To find where the minimum distance is located for each firm we use M = min(A,[],dim)
%If dim = 2, then min(A,[],2) returns a column vector containing
%the smallest element in each row.
[A,IAA]=min(MA,[],2); %find the column where the minimum distance is located
%A-A A-B
[A,IAB]=min(MB,[],2); %find the column where the minimum distance is located
% B-B B-A
TA=groupcounts(IAA);%frecuency of nearest neighbor for manuf A
%in this case
%array 1 (A-A) is the number of firms A that its nearest neighbor is a firm A
%array 2 (A-B) is the number of firms A that its nearest neighbor is a firm B
TB=groupcounts(IAB);%frecuency of nearest neighbor for manuf B
%Each column returns the value of closest neighbor
%array 1 (B-B)= is the number of firms B that its nearest neighbor is
%a firm B
%array 2 (B-A)= is the number of firms B that its nearest neighbor is
%a firm A
%STEP 7 Calculating the CLQ
T= [TA TB];
NA= size(manuf1,1); %count the size of manufacture A
NB= size(manuf2,1); %count the size of manufacture B
N= size(manuf,1); %count the size of total manufacture
N1= minus(N,k); %count the size of total manufacture -1
CLQQ = (T/NA)/(NB/(N1)); %Calculates the Colocation Quotient

댓글 수: 1

Do you mean to repeat the steps from line
random_SM= (SM(randperm(size(SM, 1)), :));
1000 times?
If yes, won't a simple loop help in this case?

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

답변 (0개)

제품

릴리스

R2021a

댓글:

2021년 7월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by