difficulties with basic syntax in array subsetting
显示 更早的评论
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
回答(1 个)
dpb
2015-5-23
for i=151:length(full_array)
if binary_vec(i)
spike_vec = response((i-150):i)
end
end
Your code had two basic issues --
- 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
- 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.
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!