Selecting part of each row from a bigger matrix without loops

조회 수: 1 (최근 30일)
Pooya Poolad
Pooya Poolad 2019년 11월 15일
댓글: Athul Prakash 2019년 11월 20일
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