title: Time-summing data in matlab
body: <p>I'm trying to process data from a transient simulation. The data is structured as follows:</p>
  
  <pre><code>+-------+-------+-------+-------+------------+
  | Bin 1 | Bin 2 | Bin 3 | Bin 4 | Time Step  |
  +-------+-------+-------+-------+------------+
  | 0.1   | 0     | 0.2   | 0     | 0.1        |
  | 0     | 0.3   | 0.1   | 0.5   | 0.1        |
  | 0.5   | 0     | 0     | 0.2   | 0.2        |
  +-------+-------+-------+-------+------------+
  </code></pre>
  
  <p>I would like it such that the data for each bin is summed in the individual time step, so for the above data, it would be:</p>
  
  <pre><code>+------------------------------------------------------------------------+
  | Bin 1         Bin 2          Bin 3          Bin 4            Time step |
  +------------------------------------------------------------------------+
  | 0.1            0.3           0.3            0.5               0.1      |
  | 0.5            0             0              0.2               0.2      |
  +------------------------------------------------------------------------+
  </code></pre>
  
  <p>The data is initially stored as a matrix called "data". My current approach is:</p>
  
  <pre><code>[h,n] = size(data);
  timestep = 0.1;
  timeperiod = data(h,5)-data(3,5)
  %the time data is in the 5th column, the numerical data starts on the 3rd row of the table
  numberofsteps = int64(timeperiod/timestep);
  starttime = data(3,5);
  
  vals_per_time=zeros(numberofsteps+1,5);
  %pre-allocating a new matrix for my new restructured data
  
  for z =1:(numberofsteps+1)
       flowtime = starttime + double(z-1)*timestep;
       %the z-1 statement is done such that the first flowtime value is the start time, but also so the 
       %equation will hold for the rest of the for loop
       vals_per_time(z,5)=flowtime;
  
       %populating the time column of my new matrix
  
    for j = 1:h
        if(data(j,5)==vals_per_time(z,5))
        %checking for if the time values in my data matrix match the current time step in my new matrix 
             for i = 1:4
                   vals_per_time(z,i) = vals_per_time(z,i) + data(j,i);
                   %there's going to be multiple entries in my data matrix that match the time step in the 
                    new matrix, and so I loop over the 4 bins and add the contributions from each entry
             end
         end
    end
  
  end
  
  </code></pre>
  
  <p>For the most part, this approach works; however, for random time steps, the if statement</p>
  
  <pre><code>if(data(j,5)==vals_per_time(z,5))
  </code></pre>
  
  <p>doesn't work, and the vals_per_time matrix will be completely empty for that time step.</p>
  
  <p>If I plot the results for one bin over time, I get something like this<a href="https://i.stack.imgur.com/q5PB9.jpg" rel="nofollow noreferrer">1</a>, where basically the time steps for the vals_per_time matrix aren't populated in regular intervals.</p>
  
  <p>I feel like it may be due to converting values between integers and decimals in the for loop, causing certain time steps to not match in the data and vals_per_time matrix. </p>
  
  <p>Please let me know if anyone has faced similar problems/has suggestions for better ways to do this!</p>