user3536870
user3536870

Reputation: 249

MATLAB reading a matrix due to conditions of first column

Here is a sample of data

Time    Data
0.32    1.5
0.45    0.6
0.68    2.1
0.91    0.8
1.23    1.3
1.54    1.0
1.68    2.0
1.92    2.3
1.95    0.7
1.98    1.6
2.12    1.9
2.34    0.3

My problem is I want to be able to have all data between the time range 0-0.3 and 0.3-0.6 for example in its own nx2 matrix. The time always continues to increase. It then would also be nice to set 'n' multiple increase in the bins to save writing 0.3,0.6,0.9,1.2 etc.

I can split the time into the relevant ranges no problem but I do not know how to keep the relevant data with its accompanying time.

I would then want to go on and plot this once I can do the above.

Thanks in advance for any help/suggestions :)

Upvotes: 1

Views: 57

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112689

Assuming the first column contains non-decreasing values, a small modification of my answer to your previous question will work. Let data denote your input matrix and s be the step used for defining groups:

data = [ 0.32    1.5
         0.45    0.6
         0.68    2.1
         0.91    0.8
         1.23    1.3
         1.54    1.0
         1.68    2.0
         1.92    2.3
         1.95    0.7
         1.98    1.6
         2.12    1.9
         2.34    0.3 ]; %// example data
s = .3; %// step to define groups

Then

result = mat2cell(data, diff([0; find(diff([floor(data(:,1)/s); NaN]))]) , size(data,2));

gives

result{1} =
    0.3200    1.5000
    0.4500    0.6000
result{2} =
    0.6800    2.1000
result{3} =
    0.9100    0.8000
result{4} =
    1.2300    1.3000
result{5} =
    1.5400    1.0000
    1.6800    2.0000
result{6} =
    1.9200    2.3000
    1.9500    0.7000
    1.9800    1.6000
result{7} =
    2.1200    1.9000
    2.3400    0.3000

Note that if some group is not present in the input it will simply be skipped in the result. For example,

data = [ 0.32    1.5
         0.45    0.6
         0.68    2.1
         2.12    1.9
         2.34    0.3 ]; %// example data
s = .3;  %// step to define groups

will produce

result{1} =
    0.3200    1.5000
    0.4500    0.6000
result{2} =
    0.6800    2.1000
result{3} =
    2.1200    1.9000
    2.3400    0.3000

Upvotes: 3

Divakar
Divakar

Reputation: 221574

If you would like to define custom bin edges for binning rows of input array, here's one approach with histcounts and arrayfun -

bin_edges = [0.3,0.6,1,12 15]; %// Define bin edges here
[~,~,bins] = histcounts(A(:,1),bin_edges);
groups = arrayfun(@(n) A(bins==n,:),1:max(bins),'Uni',0);

Sample input, output -

>> A
A =
         0.32          1.5
         0.45          0.6
         0.68          2.1
         0.91          0.8
         1.23          1.3
         1.54            1
         1.68            2
         1.92          2.3
         1.95          0.7
         1.98          1.6
        12.12          1.9
        12.34          0.3
>> celldisp(groups)    %// Display cells of output
groups{1} =
         0.32          1.5
         0.45          0.6
groups{2} =
         0.68          2.1
         0.91          0.8
groups{3} =
         1.23          1.3
         1.54            1
         1.68            2
         1.92          2.3
         1.95          0.7
         1.98          1.6
groups{4} =
        12.12          1.9
        12.34          0.3

Upvotes: 2

Related Questions