Search code examples
matlabundefined-variable

Undefined function or variable inside a for loop within a function (matlab)


I write a function to compute the Kendall distance using a for-loop

function [ distance ] = kendall_tau(y, yy, pair_list)
distance = 0;
for row = 1:length(pair_list)
    i = pairlist(row,1);
    j = pairlist(row,2);
    if (y(i)<y(j))&&(yy(i)>yy(j)) || (y(i)>y(j))&&(yy(i)<yy(j))
       distance = distance + 1;
    end
end

When I try this function with real values this undefined variable keep raising and I dont know why

kendall_tau(y_valid,y_valid_shuffle,pair_list_valid)

y_valid, y_valid_shuffle is 150x1 matrix, pair_list_valid is 978x2 matrix

Undefined function or variable 'pairlist'.
Error in kendall_tau (line 17)
    i = pairlist(row,1);

I'm noob in matlab. Thank for your help !


Solution

  • I think, most likely is that

    i = pairlist(row,1);
    

    should have been

    i = pair_list(row,1);
    

    A typo, and since MATLAB uses same () for accessing array element and giving function arguments, it gives an undefined function error.