how to run nested for loop efficiently on 3d matrix ?

I have a big 3d matrix of size (2000,700,300) for which I want to run a nested for loop on .
the loop will be spatially only, meaning i goes 1:2000 and j 1:700 , pixel by pixel.
the thing is , it takes a very very long time.
is there a way to speed this up? I have tried parfor but it only makes it slower ...
i have a strong gpu if that helps.
also , I thought of using functions like nlfilter or blockproc , but they only take a picture of 1 or 3 dimensions as an argument.

7 件のコメント

Walter Roberson
Walter Roberson 2019 年 1 月 23 日
What does the computation do ? That is going to determine whether it is suitable for GPU conversion.
For gpu use, you would probably want to do something like
temp = permute(reshape(YourMatrix, [], size(YourMatrix,3)), [2 3 1]);
this rearranges you first 2 dimensions into a vector, the moves the third dimension to the first and the first to the third... giving you a 300 x 1 x 1400000 array. You can then pagefun() that to have each 300 x 1 treated as a group.
Vinny
Vinny 2019 年 1 月 23 日
my comutation takes each pixel and calculates distances with it's surrounding , which is determined by a window parameter. that is why I can't use reshape , because than I will lose the spatial meaning..
Vinny
Vinny 2019 年 1 月 23 日
if it wasn't understood , I am treating the matrix as an Image with lots of bands , and I want to calculate local operation on that image as fast as possible
Walter Roberson
Walter Roberson 2019 年 1 月 23 日
If each band is to be processed separately then you could potentially use pagefun() on the original matrix if your algorithm could be run on a GPU.
However, it might be tricky to compute your algorithm in a GPU friendly way. Explicit indexing element by element is bad news on a GPU: you need vectorized algorithms.
Muhammad Usama
Muhammad Usama 2019 年 1 月 23 日
vectorize your matrix
Matt J
Matt J 2019 年 1 月 23 日
編集済み: Matt J 2019 年 1 月 23 日
We really need a lot clearer an explanation. What is a "band"? What is the "window parameter"? Are you extracting some 3D sliding NxNxN neighborhood around each pixel? What calculations are done on the pixels within that neighborhood?
Jan
Jan 2019 年 1 月 23 日
@Vinny: Please post your code. How can the readers suggest an improvement without seeing the current state?

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeParallel for-Loops (parfor) についてさらに検索

質問済み:

2019 年 1 月 23 日

コメント済み:

Jan
2019 年 1 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by