필터 지우기
필터 지우기

How can I compare columns within the matrix?

조회 수: 7 (최근 30일)
Madina Makhmutova
Madina Makhmutova 2018년 3월 30일
댓글: Madina Makhmutova 2018년 3월 30일
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!

채택된 답변

Birdman
Birdman 2018년 3월 30일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by