How to save each iteration of a nested for loop as rows

2 次查看(过去 30 天)
Hi there,
I am currently attempting to write a code that provides the time stamp(s) in which a cell "spikes" during a specific time of interest within a much longer recording.
for i = 1:size(starttime,1) %starttime is a ix1 matrix of the start times of each period of interest
for j = 1:size(cell,2) %cell is a 1xj array of the time points of each spike for each individual cell
k = find(cell{j} >= starttime(i) & cell{j} <= endtime(i)); %This reports the event, the cell, and the time-stamp of each spike in an array
end
end
The challenge I am facing is saving each iteration of the loop as its own separate row, while reporting the coinciding indexes. What I had hoped for is something along these lines (assuming there were 2 periods of interest and 3 cells, for simplicity's sake):
[1] [1] [2x1 double]
[1] [2] [0x1 double]
[1] [3] [0x1 double]
[2] [1] [ 20]
[2] [2] [0x1 double]
[2] [3] [2x1 double]
In reality, it gives me this:
[1] [3] [0x1 double]
[2] [3] [2x1 double]
I recognize that as it constructs the array it goes by row. Therefore, it is storing each event (i), but not with each of the corresponding iterations of each cell (j). Simply the last iteration.
I have been searching online for a solution I could adapt to this problem, but I have had limited success. I am new to MatLab and would appreciate any suggestions. Thank you in advance.
Cheers,
Elias
  1 个评论
Ameer Hamza
Ameer Hamza 2018-4-24

Are you getting the output from above code? The code you cannot give this output because, in each iteration, you are overwriting the k.

请先登录,再进行评论。

回答(1 个)

Amit
Amit 2018-4-24
p =0;
for i = 1:size(starttime,1)
for j = 1:size(cell,2)
k{p,1} = find(cell{j} >= starttime(i) & cell{j} <= endtime(i));
p = p+1;
end
end
Try this if it solves yours problem.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by