Selecting part of each row from a bigger matrix without loops

1 次查看(过去 30 天)
Hello there,
I have matrix h, let say it is 10,000x1000. I want to somehow select some known part of each column. these parts are in different sizes but all start from first element of the column. Then I want to partition these selections to form a final array of, for instance (20,1000);
currently I'm doing that using a for loop, but it has became a bottleneck in my script (as all other parts are vectorized):
%duration is an array that says how many element of each column is needed
for i = 1:size(h,2)
temp = h(1:uint32(duration(i)/delta),i);
h_select(:,i) = sum(reshape(temp,size(temp,1)/histogram_bins,histogram_bins,size(temp,2)));
end
The other problem is if I create a zeros matrix with size of ((longest selection)x1000) in this example and just do the same reshaping and sum to have (20x1000) answer, the shortest one would actually loose its resolution as "histogram_bins" in the code is fixed. So for the longest selection I will end up with 20 valid values but for shortest one I might end up with 2 valid values and 18 zeros.
I would be appreciated if anyone have an idea what is the fastest way to deal with this situation.
  1 个评论
Athul Prakash
Athul Prakash 2019-11-20
Hey, the code you've shown is not easily vectorizable, not without making it fairly more complex.
I think you should try parfor, if you have the parallel computing toolbox installed. That way you can run the same for loop in parallel across many cores. Please have a look here:
Since parfor does come with a few contraints, you will need to make a few tweaks to your code. Once again, documentation will help you.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by