MATLAB: How to compare columns within the matrix

comparisonmatrix manipulation

I have a matrix with 2:j number of columns. I would like to compare values in the column #2 to the values in the columns 3:j and save the output as a matrix of 0 and 1, where 0 = (value from column #2 >= value from the column #j) and 1 = (value from column#2 < value from column #j). I have tried the following code:
output = zeros([val1 val2]);
for i = 1:val1
for j = 1:val2
if AUC(:,j+2) >= AUC(:,2)
output(i,j) = 1;
end
end
end
As this code is executed nothing is saved to the output variable. Also running the statement below only calculates comparison for one of the columns.
AUC(:,j+2) >= AUC(:,2)
How should I do this? Please help!

Best Answer

I understand that you want to compare second column with the rest of columns. Then, try this:
A=randi([1 7],5,5); %demo data
idx=A(:,2)<A(:,setdiff(1:size(A,2),2))
This will compare the second column with the rest of columns. It will return a logical array of 1's where element in second column is smaller, 0 otherwise.