Reputation: 89
I'm trying to check a vector in matlab. As example a vector has 3 different possibilites 0 1 and 2. Now i want to check how many of 0s 1s and 2s there are inside of this vector within matlab, so i can determine the best split choice for my decision tree. Maybe there is any easier way to do it?
best regards
Upvotes: 1
Views: 39
Reputation: 329
Although I like Brendan's answer, I prefer the following code.
array = [1 2 1 0 3 1 1 4];
num_1 = numel( find(array==1) );
find
gives you the indices of the values. You can also use ~=
, <
and >
.
I suggest you adapt this to a function.
Upvotes: 1
Reputation: 1025
With vector == some_num
, you return a binary array of locations of some_num
in vector. With vector(binary_array)
you return a subset of the array, indicated by '1's in binary_array
.
Putting that together:
vec = [ 0 0 1 1 1 2 2 2 2 2];
num_0 = length(vec(vec==0)); % = 2
num_1 = length(vec(vec==1)); % = 3
...
Upvotes: 1