# 3D data non-transparent presentation

1 次查看（过去 30 天）
Valeriy 2020-10-23

There is a X,Y,Z rectangular matrix M partly filled by the non-zero numerical data. There is a direction of view, along X, or, Y, Z direction.
It is simple to calculate projection of the matrix on view direction, Z in following case, as a mean(M(:,:,1)), median(M(:,:,1)), max(M(:,:,1)), etc.
Is there a simple method how to find such projection, when each corresponding projection's value equal to the first non-zero element along direction of projection? It is similar to the case, when each non-zero M element is "non-transparent", shadowing all other elements behind it.
Of course, it is possible to organize 2D loops to calculate such element, but it will be very time consuming operation.
Thanks for proposals and help

### 采纳的回答

Tim 2020-10-31
One way to do this is to make a binary matrix that is 1 for all values greater than your threshold (zero, for example), and zero elsewhere, then compute the max-operation on the binary matrix along the desired projection dimension. For duplicate values, the MAX operation returns the minimum index, which is what you are looking for. Then you can grab the 3D matrix values corresponding to the max indices as a 2D array to form your projection.
Here is an example for a projection of the type you want along dimension 2, with the caveat that how I am computing the indices is kind of ugly. There is probably a better/simpler solution for that. You will have to modify the indexing according to the dimension you want to project along.
% Example for finding the "first-index-above-threshold projection"
% Example volume containing some >0 values:
tst = rand(5, 5, 5);
tst(tst < 0.5) = 0;
threshold = 0; % Insert threshold here: using 0
% Example for dimension 2:
% - Modify the following code for performing this operation in dimensions 1
% or 3.
% Find indices of first non-zero values in dimension 2:
[~, mxi] = max(bmask, [], 2);
% Get associated indices in dimensions 1 and 3:
idx1 = repmat((1:size(tst, 1))', [1, 1, size(tst, 3)]);
idx3 = repmat(permute((1:size(tst, 3)), [3, 1, 2]), [size(tst, 1), 1, 1]);
% Convert to matrix index value:
inds = sub2ind(size(tst), idx1, mxi, idx3);
% Grab from original matrix:
proj = tst(inds);
% Show:
imagesc(squeeze(proj))
##### 1 个评论显示 -1更早的评论隐藏 -1更早的评论
Valeriy 2020-11-5
Hi Tim,
Excellent solution, thank you very much!
And thanks for very useful applicaton expamples of <max> and <sub2ind> functions
Best regards
Valeriy

### 类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

### Community Treasure Hunt

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

Start Hunting!

Translated by