BSXFUN Vectorizing. How can I vectorize the function?
1 次查看(过去 30 天)
显示 更早的评论
%And this is what I have so far, but I'm still getting this error: Non-singleton dimensions of the two input arrays must match each other.
function [spikes_train, n_bins] = make_spikes_matrix(data,index1,index2,bin_size, n_neurons, lag, bias)
n_bins = floor(length(data(index1,index2).spikes)/20); % keep them all
spikes_train = zeros(n_neurons,n_bins);
spikes_train(1:n_neurons)= bsxfun(@plus,spikes_train(1:n_neurons,1:n_bins),...
data(index1,index2).spikes(1:n_neurons,1:(n_bins*bin_size)));
spikes_train = spikes_train(:,bias+1-lag:n_bins-lag);
end
2 个评论
Rik
2021-3-15
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
Rik
2021-3-15
@Iman Choukari Why do you attempt to edit away parts of your question? Are you trying to find out if I'm more stubborn than you? There is a copy of this question here. If you edit away your code again, I will probably put it in a comment myself.
回答(1 个)
Athul Prakash
2021-3-12
编辑:Athul Prakash
2021-3-12
Hi Iman,
bsxfun intends to apply @plus to elements of same index across the two input arrays. So in your call to bsxfun, the dimensions of the two input matrices must match exactly, but 1:n_bins and 1:n_bins*bin_size are mismatched.
What you are looking for may be achieved through a simple sum() call.
% For a 2D matrix A (MxN),
bin_size = 100;
for i=1:M
r = reshape(A(i,:), bin_size, []); % reshape i-th column into size (num_bins, ...)
spikes = sum(r); % sum 'r' along the first dimension, thus adding all elements of same bin.
end
If reshape is unfamiliar, you may check its doc:
If needed, the outer for-loop may also be vectorized, something like:
% For a 2D matrix A (MxN),
bin_size = 100;
A_new = reshape(M, bin_size, []); % reshapes A into 3-D with each bin now along 2nd dimension.
B = sum(A_new,2); % sum along the second dimension = along each bin.
B = squeeze(B); % gets rid of the second dimension of size 1.
Hope it helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!