GucciBucketHat
GucciBucketHat

Reputation: 21

Separating range into intervals and outputting it into an array

I'm trying to separate a range of 1200 values into 10 different intervals and find the number of values in each interval. This is what I have come up with so far.

load('Data.mat')
% % Train=Data(i_tr,:);
l_train=height(Train);
Sdrr=Train.SDRR;

R_SDRR=range(Sdrr);
min_SDRR=min(Sdrr);
max_SDRR=max(Sdrr);
intervals=10;
int_SDRR=R_SDRR/intervals;
data=zeros(intervals,1);

for j=1:l_train
    for k=1:intervals-1
        if min_SDRR+(k-1)*int_SDRR<=Sdrr(j)<min_SDRR+(k)*int_SDRR
            data(k)=data(k)+1;
        else

        end
    end
end 

The values I am getting seem to be cumulative rather than separate in the output array i.e. 238,519,745...(this has to be wrong because the total number of inputs are 1200)

Upvotes: 2

Views: 133

Answers (3)

GucciBucketHat
GucciBucketHat

Reputation: 21

i used the and operator and changed the order of the for loops and it fixed my answer

R_SDRR=range(Sdrr);
min_SDRR=min(Sdrr);
max_SDRR=max(Sdrr);
intervals=10;
int_SDRR=R_SDRR/intervals;
data=zeros(intervals,1);
int_array = zeros(10,1);


for j=1:intervals
    for k=1:l_train
     if Sdrr(k)<min_SDRR+(j)*int_SDRR && Sdrr(k)>=min_SDRR+(j-1)*int_SDRR
int_array(j)= int_array(j)+1;     
     end
    end
end

Upvotes: 0

pho
pho

Reputation: 25489

If I've understood correctly, you want to distribute N = 1200 values into intervals buckets.

intervals = 10;
min_range = 0;
max_range = 100;

data = rand(1200, 1) * max_range; % 1200 random numbers between 0 and 100

intervals_size = (max_range - min_range) / intervals; % Size of each interval

Now we can calculate the "interval number" in which a given data falls by subtracting the minimum value from the data, dividing the result by the interval size, and flooring the result.

interval_num = floor((data - min_range) / intervals_size);

Then, it's just a question of counting how many of each number exist. You can do it a whole bunch of ways:

Ex. 1:

intervals_count = zeros(intervals, 1);
for ii = 1:intervals
    intervals_count = sum(interval_num == ii - 1)
end

Ex. 2:

intervals_count = zeros(intervals, 1);
for ii = 1:length(interval_num)
    group_num = interval_num(ii) + 1;
    intervals_count(group_num) = intervals_count(group_num) + 1;
end

Ex. 3: If you have Matlab >= 2019a, simply use the groupcounts() function

[intervals_count, interval_num2] = groupcounts(interval_num);

Upvotes: 1

juju89
juju89

Reputation: 549

I tried to reproduce what I think you're trying to do.

I created some random data that matches your size:

X = rand(1613,4);
X = X(1:1200,:);

Now X is 1200-by-4.

You want to use 10 intervals:

n_interval = 10;

Need to calculate how many values (len_interval) you will have in each interval:

len_X = size(X,1);

% using round() in case len_X/n_interval is not an int
len_interval = round(len_X/n_interval);

Then go through each interval and store the values in interval cell.

interval = {};
for i = 1:n_interval
    if i==n_interval % last interval
        interval{i} = X((i-1)*len_interval+1:len_X);
    else
        interval{i} = X((i-1)*len_interval+1:i*len_interval);
    end
end

Upvotes: 1

Related Questions