Create new column for data after each iteration of a for loop

15 次查看(过去 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!!

采纳的回答

Benjamin Avants
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

更多回答(1 个)

Sara
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.

类别

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