Create new column for data after each iteration of a for loop
18 次查看(过去 30 天)
显示 更早的评论
Hello, i'm trying to understand how to create a column for data after each iteration of a for loop. I need to run the loop 50 times, and recover the data inside of one matrix. The vector t should change with each iteration of the loop, and this is what I want to record into S matrix as columns... but I cant seem to get it.
if true
% clear all
% lambda, firing rate
lambda = 10;
%total time of simulation 5 seconds
totalTime = 5000; % milliseconds
N = 50; % # trials
spikeTimes = [];
spikeTrain = [];
% Loop
for i = 1:N
for T = 1 : totalTime*1.2
if lambda / 1000 >= -log(rand());
spikeTimes(end + 1) = T;
end
isi = diff(spikeTimes);
t = cumsum(isi)/1000;
end
S = t(:); % <-------- ????
end
end
Any help would be appreciated!!
0 个评论
采纳的回答
Benjamin Avants
2014-5-1
As the code appears in your question, you are overwriting S in every loop.
Assuming you don't have a length mismatch as Sara pointed out, that line should read like this:
S(:,i) = t;
or this
S(:,i) = t';
depending on whether t is a column or row vector.
You probably will have a length mismatch since number of spikes and spike times are pseudo random, so you will either need to follow Sara's advice to use a cell array or preallocate an array of NaN's at some number larger than the number of spikes you expect x 50. Saving t into S would then look like this:
S = nan(10000,50);
% loop
% calculations
S(1:numel(t),i) = t;
% end loop
0 个评论
更多回答(1 个)
Sara
2014-5-1
It seems that the vector t could be of different size at each iteration, depending on the if condition. If that's the case, consider using:
S = cell(N,1)
before the for loop and
S{i} = t;
to store t in a cell array.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!