Create new column for data after each iteration of a for loop
18 views (last 30 days)
Show older comments
Jason Paladini
on 1 May 2014
Answered: Benjamin Avants
on 1 May 2014
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 Comments
Accepted Answer
Benjamin Avants
on 1 May 2014
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 Comments
More Answers (1)
Sara
on 1 May 2014
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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!