difficulties with basic syntax in array subsetting

3 views (last 30 days)
Hi, I am trying to create a matrix of vectors from a larger array. Basically every time there is some stimulus (recorded as a 1 in a binary vector) I want to create a new array of the previous 150 timesteps from the corresponding ("response") vector. But I get the "Subscript indices must either be real positive integers or logicals" error messages. How can I best create the new array? I am a very basic Matlab user.
for i=1:length(full_array)
if binary_vec(i) == 1
spike_vec(i) = response((i-150):i,1)
end
end

Answers (1)

dpb
dpb on 23 May 2015
for i=151:length(full_array)
if binary_vec(i)
spike_vec = response((i-150):i)
end
end
Your code had two basic issues --
  1. If the response starts before the 150th observation subscript i-150 <= 0. I solved it by not starting the scanning until past the desired length. If that throws out real data, use max(i-150,1) to get the initial shorter lengths, and
  2. The subscript on the result variable expression tries to put the RHS vector into a single location on the left; that can't work. The above solution works iff you process this set of observations before going on to the next; if you need to save these all as a separate array of some number of columns of length 150, then you need sotoo
j=j+1; % increment column
spike_vec(:,j)=response(... % save the subvector
If the indicator vector is indeed just a logical vector showing the locations; you can eliminate the loop over every element and use
ix=find(binary_vec); % return starting locations
and then just select those positions from that vector.

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!