Why my MATLAB program doesn't stop?

2 views (last 30 days)
phdcomputer Eng
phdcomputer Eng on 1 Dec 2019
Commented: Walter Roberson on 1 Dec 2019
I wrote this program in MATLAB to compute the distance between each feature with the rest of features in CNS data (attached) , but the program run for a long time and doesn't show the result.
I'll be gratefull to have your opinions about what is the reason of this problem and how can I solve It to have the output. Thanks.
clc;
clear;
close all;
tic
load CNS.mat
data=CNS;
[n,m]=size(data);
for i=1:m-1
for j=i+1:m
t5(i,j)=fHammingDist(data(:,i),data(:,j));
b5=sum(t5)/(m-1);
end
end
[B5,indB5]=sort(b5,'descend');
databs5=data(:,indB5(1:10));
databs5=[databs5,data(:,m)];
save('databs5.mat');
I defined the distance as a function (fHammingDist)
function [ fhd ] = fHammingDist( p,q )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
fhd=sum(abs(p-q));
end
  1 Comment
Walter Roberson
Walter Roberson on 1 Dec 2019
for i=1:m-1
for j=i+1:m
t5(i,j)=fHammingDist(data(:,i),data(:,j));
b5=sum(t5)/(m-1);
end
end
Notice that you calculate b5 in each iteration of the inner for loop, overwriting the previous value completely, but you do not use the value inside the loops. There is therefore no point in calculating it inside the loops. It would make more sense to calculate it after the end of the nested loops.
You appear to be calculating the lower triangle of distances, but when you form b5 you do not fill in the upper triangle first by symmetry, so the later columns are going to be totalling fewer and fewer entries. It is not obvious that is what is desired.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!