pcarrollg
pcarrollg

Reputation: 11

Creating increasing bins in Matlab

I am attempting to create an array with the number of columns equal to a selected bin size. I want each row in the array to use the values in t in sequence. For example, t goes from -4 to 4 in intervals of 0.01 seconds. If the bin width is 6, I would like row 1 to be [-4 -3.99 -3.98 -3.97 -3.96 -3.95], and then row 2 would be [-3.99 -3.98 -3.97 -3.96 -3.95 -3.94]... and this is repeated until the last row at time = 4 seconds. I got the code starting to work, however I only make it to row 364 before I get a subscript assignment mismatch error. Can anyone help me figure out this error?

bin_width = 6;
time_interval = 0.01;
t = -4:time_interval:4;
bin_number = bin_width/2;
t_bin = zeros(length(t),bin_width);

for n = 1:length(t)-bin_width
  t_bin(n,:) = [t(n):time_interval: t(n+bin_width-1)];
  bin_number = bin_number+1;
  n = n+1;
end

The error I am getting is

Subscripted assignment dimension mismatch.

Error in Untitled (line 15)

t_bin(n,:) = [t(n):time_interval: t(n+bin_width-1)];

Upvotes: 1

Views: 30

Answers (1)

hiandbaii
hiandbaii

Reputation: 1331

You are running into floating point issues. at the point of failure

t(n):time_interval: t(n+bin_width-1)

ans =

   -0.3600   -0.3500   -0.3400   -0.3300   -0.3200

where t(n+bin_width)-1 = -0.3100

Here's a work around.

bin_width = 6;
time_interval = 0.01;
t = -4:time_interval:4;
bin_number = bin_width/2;
t_bin = zeros(length(t),bin_width);

for n = 1:length(t)-bin_width
    t_bin(n,:)=linspace(t(n),t(n+bin_width-1),6)
end

Upvotes: 2

Related Questions