How to manipulate nested cell arrays using for loop?

Hi, I have a cell array of size 1x316 with each cell different sizes.In each cell, there is two columns of data: first is speed and second is time.Both are double numeric in types. Firstly, i loaded that cell array in a structure 's1'.Its variable is called ''all_cords''.Now, using for loop, i am trying to do some arithmatics here.i need to calculate pause time variable in a cell array of size 1x316.
I need to check in each cell, if there is 0's in first column i.e speed field,if there is single 0, then pause time will be its time value and if there are multiple 0's then pause time will be tn-t0.
Thanks in advance!
Here is my codes:-
s1 = load('cell_speed_data.mat');
tpause = cell(1,length(s1.all_cords));
pause_times = cell(1,length(s1.all_cords));
for i = 1:length(s1.all_cords)
if find(s1.all_cords{i}(:,1)==0)
tpause{i} = deal(s1.all_cords{i}(:,1));
pause_times{i} = max(tpause{i}(:,1))-min(tpause{i}(:,1));
end
continue;
end;

2 comentarios

Chad Greene
Chad Greene el 12 de Mzo. de 2016
This question is unclear. Can you provide some sample data and describe exactly what you are trying to do?
Guillaume
Guillaume el 16 de Mzo. de 2016
From a dimensional analysis point of view, there is some inconsistency in the result that you want.
If there is only one time where the speed is zero, you want as an output the time at which it occurs. Hence your output is a time.
If there is more than one time where the speed is zero, you want the difference between the first and last time, so your output is a duration.
Even if both have the same unit (time), they don't represent the same thing, so how is that output going to be useful?

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 16 de Mzo. de 2016
See my comment on your question about the mismatch in what the output represents. I'm creating two outputs here, pause_start which is the time at which the pause starts, and pause_duration which is the duration of the pause.
pause_start = cell(size(s1.all_coords)); %safer than length
pause_duration = cell(size(s1.all_coords)); %safer than length
for iter = 1:numel(s1.all_coords) %safer than length as well
ispause = s1.all_coord{iter}(:, 1) == 0; %no need for find, use logical array
pause_times = s1.all_coords{iter}(ispause, 2);
if ~isempty(pause_times)
pause_start{iter} = pause_times(1)
pause_duration{iter} = pause_times(end) - pause_times(1);
end
end
Note that the pause duration ignores cases where speed goes back up between start and end of the pause, as per your question. To me that doesn't sound correct but maybe it doesn't happen with your data, so it does not matter.

5 comentarios

Jung BC
Jung BC el 16 de Mzo. de 2016
Editada: Jung BC el 16 de Mzo. de 2016
Hi Guillaume, Thanks for your answer.I should describe my problem more in details here now,then you may understand it more.
The sample data of first cell of s1.all_coords looks like this:-
44.88 0.0043
28.94 0.0040
0 0.0044
0 0.0037
0 0.0042
10.83 0.00415
35.03 0.00432
57.87 0.00400
24.65 0.00432
0 0.00402
8.88 0.00406
Now, in above sample data in the cell, i need to compute pause time variable everytime where it detects 0's in the speed column.
So lets say, for above sample,
first pause time = p1 = 0.0044(highest among 3 consecutives)-0.0037(lowest)
second pause time= p2 = 0.00402(since its single 0's)
similar this way, i need to store all pause time values from every cells and store in a new cell array of size 1x316.
In your solution, i find only one pause time value from entire cell.I need to store all the pause times of cell.
e.g. cell_1 : p1, p2, p3 ,p4,.... . . .
.
cell_316: p1, p2, p3, p4, and so on.
Hope you got my problem much broad way this time.
Guillaume
Guillaume el 16 de Mzo. de 2016
Editada: Guillaume el 16 de Mzo. de 2016
Ok, so the problem is a bit more complicated. You want to identify runs of 0 in the speed column. diff and find on the logical array will do that.
I still think that using the time when the run is of length 1 is not consistent (and makes the code more complicated since you need to special-case it).
pause_duration = cell(size(s1.all_coords)); %safer than length
for iter = 1:numel(s1.all_coords) %safer than length as well
ispause = s1.all_coord{iter}(:, 1) == 0; %a logical array of 0 and 1
transitions = find(diff([0 ispause 0])); %location of transitions from 0 to 1 and 1 to 0 in logical array
%because of the prepending and appending of 0, transition is
%guaranteed to have an even numbers of elements and to start
%with the index of a transition from 0 to 1 (i.e. a beginning
%of a run).
runstarts = transitions(1:2:end);
runends = transitions(2:2:end) - 1;
durations = cell(1, numel(runstarts));
for run = 1 : numel(runstarts)
runtimes = s1.all_coords{iter}(runstarts(run):runends(run), 2);
if numel(runtimes) == 1 %special case when single 0
durations{run} = runtimes; %but it's not a duration!
else
durations{run} = max(runtimes) - min(runtimes);
end
pause_duration{iter} = durations;
end
end
Jung BC
Jung BC el 16 de Mzo. de 2016
Hi Guillaume, I really appreciate your effort.Just an error showing while executing these codes.I tried to figure it out but i failed.
''Error using horzcat Dimensions of matrices being concatenated are not consistent.''
Guillaume
Guillaume el 16 de Mzo. de 2016
Editada: Guillaume el 16 de Mzo. de 2016
Oh, of course, ispause is a column vector. Change the transitions line to:
transitions = find(diff([0; ispause; 0]));
I've edited my answer and fixed another bug later on. There may still be some more bugs. Code is untested.
Jung BC
Jung BC el 16 de Mzo. de 2016
Thanks a lot Guilaume,finally it's working well.
I really appreciate your help.One last request to you,
How can i extract minimum, maximum, and average
values from the final output- the cell array- pause_duration{}
and save them in different cell
arrays?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 10 de Mzo. de 2016

Comentada:

el 16 de Mzo. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by