Create new column for data after each iteration of a for loop
조회 수: 11 (최근 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에서 Electrophysiology에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!