How can I compare columns within the matrix?

25 ビュー (過去 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.
  1 件のコメント
Madina Makhmutova
Madina Makhmutova 2018 年 3 月 30 日
That works! Thanks a lot!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by