MATLAB: Sorting cycling data, with numerous measurements taken at each cycle into a matrix, based on cycle.

vectors

Hi guys!
I want to sort voltage measurements I'm getting into a matrix based on cycle number. I am taking numerous measurements in each cycle. Basically I want to create discrete bins for each cycle and then place each voltage value into its respective bin. I have attached a picture of what I want to try and get. Any help would be greatly appreciated!
Thanks.

Best Answer

This will create a large matrix, one that is almost entirely composed of zeros. But only you know what it is that you need/want to do with that array once you have built it.
It would be better in terms of memory to make the matrix a sparse one. And in fact, that makes it trivially easy to write the code.
ndata = size(rawdata,1);
nbins = max(rawdata(:,2));
binneddata = sparse((1:ndata)',rawdata(:,2),rawdata(:,1),ndata,nbins);
For example, here is some garbage data, since I have no desire to carefully type the data from the picture in your example.
rawdata = [rand(1,10);1 1 1 2 2 2 2 3 3 3]'
rawdata =
0.81472 1
0.90579 1
0.12699 1
0.91338 2
0.63236 2
0.09754 2
0.2785 2
0.54688 3
0.95751 3
0.96489 3
Now, see if the code that I wrote works. What does it produce? binneddata is a sparse matrix, of size 10x3.
binneddata
binneddata =
(1,1) 0.81472
(2,1) 0.90579
(3,1) 0.12699
(4,2) 0.91338
(5,2) 0.63236
(6,2) 0.09754
(7,2) 0.2785
(8,3) 0.54688
(9,3) 0.95751
(10,3) 0.96489
We can see that it is the matrix you desire if we display it as a full matrix.
full(binneddata)
ans =
0.81472 0 0
0.90579 0 0
0.12699 0 0
0 0.91338 0
0 0.63236 0
0 0.09754 0
0 0.2785 0
0 0 0.54688
0 0 0.95751
0 0 0.96489
As I said though, you might better construct a cell array, splitting the data into separate cells on one cell array. So put the data from each bin into a separate cell.
bincounts = accumarray(rawdata(:,2),1,[nbins,1]);
binneddata = mat2cell(rawdata(:,1),bincounts,1);
So binneddata is now a cell array.
binneddata
binneddata =
3×1 cell array
{3×1 double}
{4×1 double}
{3×1 double}
binneddata{:}
ans =
0.81472
0.90579
0.12699
ans =
0.91338
0.63236
0.09754
0.2785
ans =
0.54688
0.95751
0.96489
Access any specific cell using curly braces. Thus:
binneddata{3}
ans =
0.54688
0.95751
0.96489
What matters in the end is how you will use the result, what will you do with it once done with this step. So when you build code, you need to look to that next step in the process.