How to manipulate nested cell arrays using for loop?

3 次查看(过去 30 天)
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 个评论
Chad Greene
Chad Greene 2016-3-12
This question is unclear. Can you provide some sample data and describe exactly what you are trying to do?
Guillaume
Guillaume 2016-3-16
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?

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2016-3-16
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 个评论
Guillaume
Guillaume 2016-3-16
编辑:Guillaume 2016-3-16
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 2016-3-16
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by