フィルターのクリア

Sorting through labelled data

1 回表示 (過去 30 日間)
Tino
Tino 2019 年 6 月 4 日
回答済み: Priysha Aggarwal 2019 年 6 月 4 日
Given the data of two columns below with negative and positive label
'0,Negative'
'1,Negative'
'1,Positive'
'2,Negative'
'2,Positive'
'3,Positive'
'4,Negative'
'4,Positive'
'5,Positive'
'9,Positive'
Please I want to use the code below to sort for negative or positive numbers instead of even and odd numeric data example code below
for col = 1:size(croppedz, 2)
%sort even/odd rows, reshape each into a Kx? matrix, sum across rows and divide the two sums
% K = can be anynumber that groups it
result(:, col) = sum(reshape(sort(croppedz(1:2:end, col)), K, []), 1) ./ sum(reshape(sort(croppedz(2:2:end, col)), K, []), 1);
end
end
Thanks for your help in advance
Tino

採用された回答

KSSV
KSSV 2019 年 6 月 4 日
str = { '0,Negative'
'1,Negative'
'1,Positive'
'2,Negative'
'2,Positive'
'3,Positive'
'4,Negative'
'4,Positive'
'5,Positive'
'9,Positive'} ;
% Get positive
idx = contains(str,'Positive') ;
str(idx)
  2 件のコメント
Tino
Tino 2019 年 6 月 4 日
Thanks for your swift anwser KSSV
how do I seperate the values from the character to get a different cell for positives and negatives
Thanks in advance
KSSV
KSSV 2019 年 6 月 4 日
str = { '0,Negative'
'1,Negative'
'1,Positive'
'2,Negative'
'2,Positive'
'3,Positive'
'4,Negative'
'4,Positive'
'5,Positive'
'9,Positive'} ;
V = regexp(str,'\d+','match') ;
V = [V{:}] ;
iwant = cellfun(@str2num,V')

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

その他の回答 (1 件)

Priysha Aggarwal
Priysha Aggarwal 2019 年 6 月 4 日
If you can skip using the above given for loop, you can store the above strings in a string array as :
A = ["0","Negative";
"1","Positive";
...];
Now you can sort A according to column 2:
[mat,idx] = sort(A(:,2)); % this will sort 'negative' and 'positive' labels alphabetically
sortedA = A(idx,:); %sorting column 1 accordingly

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by