Recursive concatenation of cell arrays
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I have a cell array, let’s call it Events_ext. Each cell is an event, containing time stamps in sec (6.037 = 6sec 37msec). The length of these events vary. By processing Events_ext I would like to create a new cell array (Events_ext2) in which all those consecutive cells from Events_ext get concatenated, where the time difference between the last member of one event and the first member of the consecutive event is less than 100ms. It should do it recursively.
For example, let’s say we have 10 events in the cell array:
A B C D E F G H I J
The time difference between the last timestamp in B and the first timestamp in C is less than 100ms. In addition, the time difference between the last timestamp in E and the first timestamp in F is less than 100ms. Also, the time difference between the last timestamp in F and the first timestamp in G is less than 100ms.
How to write the algorithm that creates
A (B+C) D (E+F+G) H I J , therefore creating 7 events out of the original 10?
0 Kommentare
Akzeptierte Antwort
Sara
am 20 Jan. 2015
Events_ext2 = Events_ext;
i = 0;
while i < numel(Events_ext2)-1
i = i + 1;
if(Events_ext2{i+1}(1)-Events_ext2{i}(end) < 100)
Events_ext2{i} = [Events_ext2{i} Events_ext2{i+1}];
Events_ext2(i+1) = [];
i = i - 1;
end
end
Weitere Antworten (1)
Stephen23
am 21 Jan. 2015
Bearbeitet: Stephen23
am 21 Jan. 2015
A fully vectorized version (without using any loops) in just three lines of code:
>> A = {[0,1],[2,3,4],[4.05,6,7,8],[9],[10,11],[11.05,13],[13.05,15],[16,17],[18,19,20],[21,22,23]};
>> B = (cellfun(@(v)v(1),A(2:end)) - cellfun(@(v)v(end),A(1:end-1))) < 0.1;
>> C = cumsum([true,~B]);
>> D = arrayfun(@(c)[A{c==C}],1:C(end),'UniformOutput',false);
We can check if this produces the correct result:
>> D
D =
[1x2 double] [1x7 double] [9] [1x6 double] [1x2 double] [1x3 double] [1x3 double]
>> D{2}
ans =
2.0000 3.0000 4.0000 4.0500 6.0000 7.0000 8.0000
This has correctly concatenated the matrices if their adjacent timestamps are less than 0.1 seconds apart.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!