Clearing the upper section of a 3-dimensional matrix

Given a stack of 2D-images, such that they form an x-by-y-by-z volume data set, I would like to delete any data that lies above (column-wise) a set of predetermined column indexes.
For example if:
A(:,:,1) = [1 1 1 A(:,:,2) = [1 1 1 A(:,:,3) = [1 1 1
1 1 1 1 1 1 1 1 1
1 1 1] 1 1 1] 1 1 1]
And the column indexes are defined such as (Where rows correspond to different images):
B = [3 2 1
1 2 3
3 3 3]
The result should be:
C(:,:,1) = [0 0 0 C(:,:,2) = [0 0 0 C(:,:,3) = [0 0 0
0 0 1 1 0 0 0 0 0
0 1 1] 1 1 0] 0 0 0]
My current method involves the use of for-loops, looping through each image and x-position individually, but this proves to be very slow when dealing with large sets of image data. My second thought was to use linear indexing, by converting the column indexes into an array of linear indexes I can easily generate a 3D matrix with 0's at the required column index, but the issue remains of removing the data above those points.
Is there a less computationally intensive method of tackling this problem that I'm missing?
Thanks for any assistance with the matter! :)

3 件のコメント

Jan
Jan 2014 年 2 月 21 日
I'm not sure if I see the connection between A, B and C. Please post your code.
Image Analyst
Image Analyst 2014 年 2 月 22 日
Well if B(1,1) = 3, then any elements in A(1,1) above plane 3 should be set to zero. But there are none, since A is only 3 planes high - there are no planes above 3 for A. So C(,1,) should be the same as A for all planes. So C(1,1,:) should equal 1,1,1, which is what A is in the (1,1) column. Yet your C does not show that so I guess I misunderstood or you didn't explain it correctly.
Robert
Robert 2014 年 2 月 22 日
Apologies for the ambiguous example, I meant to illustrate that the masking is being applied up the columns, not between stacks - In fact the images can be treated as separate entities in this example.
A value of B(1,1) = 3 would only imply that C(1:3,1,1) = 0.

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

 採用された回答

David Young
David Young 2014 年 2 月 21 日
編集済み: David Young 2014 年 2 月 22 日

0 投票

Try
Bt = permute(B, [3 2 1]);
mask = bsxfun(@ge, Bt, (1:size(A,1)).');
C = A;
C(mask) = 0;

1 件のコメント

Robert
Robert 2014 年 2 月 22 日
編集済み: Robert 2014 年 2 月 22 日
Thank you! This is much more efficient and makes a huge difference for my application.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2014 年 2 月 21 日

編集済み:

2014 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by