Selecting part of each row from a bigger matrix without loops

2 views (last 30 days)
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 Comment
Athul Prakash
Athul Prakash on 20 Nov 2019
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.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!