0

Thanks guys for help. With this code:

clc; 
T = importdata('data_jana.xls');
 result = cell(1, size(T,2));

for col = 1:size(T,2)
    %// Get length-value pairs
    [lengths, values] = runLengthEncode(T(:,col));
    %// Compute all deltas
    deltas = 0.2*lengths(values == 0.2);
    %// Remove deltas following 5 zeros
    idxFiveZeros = find(lengths > 4 & values == 0, 1);
    if(isempty(idxFiveZeros))
        idxFiveZeros = numel(lengths);
    end
    deltas = deltas(1:sum(values(1:idxFiveZeros) == 0.2));
    %// Store result for column
    result{col} = deltas;
end

There is no errors.

But the problem is that this script stop definitely the deltas calculation when he found five zeros consecutively. It is not that I want to do. One example: col1 = 0, 0, 0.2, 0.2, 0, 0, 0, 0, 0, 0.2, 0.2, 0.2, 0, 0.2, 0, 0.2; result_col1= 0.4 1.0; In this example, after the first two 0.2 there is five 0 consecutively (the rule to stop), so the script add these two values. Then, the script continue and found 0.2 spaced by less than five 0 consecutive so he add all values. Thanks.

  • are each row/column of the matrix continuous? i.e. if row/column 1 ends in 0.2 and row/column 2 starts with 0.2, are they supposed to be added? Or are they independent, in which case you should treat each row/column independently? – GameOfThrows Aug 25 '15 at 10:13
  • each columns are independant. so, I have 34 columns, I need to have 34 results columns at the end. – Julien Boulay Aug 26 '15 at 12:23

2 Answers2

0
T = importdata('data.xls')   
T= T.Sheet1; % I expect this to be a MxN matrix
d = cumsum(T); % Adds values consecutively
[M,N] = size(T)
for ii = 1:N
    tmp = unique(d(:,ii)); % Extracts the unique values of the cumsum
    tmp2 = [tmp(1) diff(tmp)]; % Returns the actual sum as you wanted
    result{ii} = tmp2; % save in a cell, since not all tmp2s will be the same length
end

How to add the stopping condition based on 5 or more consecutive zeros I'm not sure yet.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • you could do that by adding 5 consecutive numbers and see if the result is greater than 0, if it is, it means that the following 4 numbers contains 0.2s, in which case you would keep adding; where if the result is 0, then the 4 following must also be 0s, in which case you stop adding? – GameOfThrows Aug 25 '15 at 10:22
0

I had a related question on a series of zeros and ones. Using the code from knedlsepp's answer there, you can do:

result = cell(1, size(T,2));
for col = 1:size(T,2)
    %// Get length-value pairs
    [lengths, values] = runLengthEncode(T(:,col));
    %// Compute all deltas
    deltas = 0.2*lengths(values == 0.2);
    %// Remove deltas following 5 zeros
    idxFiveZeros = find(lengths > 4 & values == 0, 1);
    if(isempty(idxFiveZeros))
        idxFiveZeros = numel(lengths);
    end
    deltas = deltas(1:sum(values(1:idxFiveZeros) == 0.2));
    %// Store result for column
    result{col} = deltas;
end

Like in A. Visser's answer, the results are stored in a cell array as each delta array can be of a different length.

buzjwa
  • 2,632
  • 2
  • 24
  • 37