Effacer les filtres
Effacer les filtres

How to manipulate nested cell arrays using for loop?

3 vues (au cours des 30 derniers jours)
Jung BC
Jung BC le 10 Mar 2016
Commenté : Jung BC le 16 Mar 2016
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 commentaires
Chad Greene
Chad Greene le 12 Mar 2016
This question is unclear. Can you provide some sample data and describe exactly what you are trying to do?
Guillaume
Guillaume le 16 Mar 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?

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 16 Mar 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 commentaires
Guillaume
Guillaume le 16 Mar 2016
Modifié(e) : Guillaume le 16 Mar 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 le 16 Mar 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?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by