How order a dataset

3 views (last 30 days)
Giuli
Giuli on 2 Jan 2023
Answered: Star Strider on 2 Jan 2023
Hello,
I am using this cancer dataset. I have 569 samples( rows) and 30 attributes ( 3:30 columns). The second colunms indicates the type of tumor ( 'M' = malignant, 'B' = benign). I want to order first the M samples , then the B samples. How can I do it on matlab?
I would appreciate your response.
P.S. : It's not a homework or an exam
%% Load data
%filename='wdbc.data';
%url=['https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Diagnostic)/' filename];
%file_destination = filename;
%websave(file_destination,url);
%Data = readtable(file_destination, 'FileType','text', 'Delimiter', 'comma');
VarNames = {'ID','B/M','mean radius', 'mean texture', 'mean perimeter', ...
'mean area', 'mean smoothness', 'mean compactness', 'mean concavity', ...
'mean concave points', 'mean symmetry', 'mean fractal dimension', ...
'radius error', 'texture error', 'perimeter error', 'area error', ...
'smoothness error', 'compactness error', 'concavity error', ...
'concave points error', 'symmetry error', ...
'fractal dimension error', 'worst radius', 'worst texture', ...
'worst perimeter', 'worst area', 'worst smoothness', ...
'worst compactness', 'worst concavity', 'worst concave points', ...
'worst symmetry', 'worst fractal dimension'};
Data.Properties.VariableNames = VarNames;
Data_30 = Data(:,3:32); % remove column 1 and 2
Data_ar = table2array(Data_30); % transform from table to array
  1 Comment
Walter Roberson
Walter Roberson on 2 Jan 2023
[~, idx] = sort(T{:, 2}, 'descend');
newT = T(idx, :) ;
for table T

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 2 Jan 2023
One approach is to usee the sortrows function —
Data = table(randi(1E4,6,1),['M';'B';'M';'M';'B';'B'], rand(6,1), randn(6,1), randi(9,6,1))
Data = 6×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ________ _________ ____ 2546 M 0.080991 -1.7018 5 2152 B 0.82521 1.8717 8 8207 M 0.55569 0.074832 7 8258 M 0.65973 -0.021411 3 8687 B 0.95815 0.64405 5 8609 B 0.73313 -1.166 6
Data = sortrows(Data,2, 'descend')
Data = 6×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ________ _________ ____ 2546 M 0.080991 -1.7018 5 8207 M 0.55569 0.074832 7 8258 M 0.65973 -0.021411 3 2152 B 0.82521 1.8717 8 8687 B 0.95815 0.64405 5 8609 B 0.73313 -1.166 6
.

Categories

Find more on Fractals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!