how to compare non numerical values or character strings element-wise between a table and a one dimensional array ?
Show older comments
IrisD = readtable('irisdata.csv');
classes = categorical(IrisD{:,5});
Icats = categories(classes);
setosa = IrisD((IrisD(:,5)==Icats(1)),:);
Ttest1 = setosa(1:40,:);
Ttrain1 = setosa(41:50,:);
versicolor = IrisD((IrisD(:,5)==Icats(2)),:);
Ttest2 = versicolor(1:40,:);
Ttrain2 = versicolor(41:50,:);
virginica = IrisD((IrisD(:,5)==Icats(3)),:);
Ttest3 = virginica(1:40,:);
Ttrain3 = virginica(41:50,:);
Ttest = [Ttest1; Ttest2; Ttest3];
Ttrain = [Ttrain1; Ttrain2; Ttrain3];
I am getting this error: Undefined operator '==' for input arguments of type 'table'.
I am trying to split the Iris database into a training and testing dataset, with the first 40 values of each class being in the testing dataset and the remaining 10 for each class be in the training dataset.
6 Comments
madhan ravi
on 19 Mar 2019
{} instead of ()
Apurva Jariwala
on 19 Mar 2019
madhan ravi
on 19 Mar 2019
Edited: madhan ravi
on 19 Mar 2019
setosa = IrisD(strcmp(IrisD{:,5},Icats(1)),:); % if it doesn't work attach your data file
[comment moved to answer section]
Apurva Jariwala
on 19 Mar 2019
Peter Perkins
on 19 Mar 2019
Apurva, IrisD{:,5} certainly will work.
But I suggest that you use names, and dot indexing, and ==. So you code becomes
setosa = IrisD(IrisD.Species == Icats(1),:)
or maybe even
setosa = IrisD(IrisD.Species == 'setosa',:)
Isn't that nicer?
Apurva Jariwala
on 19 Mar 2019
Accepted Answer
More Answers (0)
Categories
Find more on Model-Based Calibration Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!