MATLAB Answers

3D data non-transparent presentation

9 views (last 30 days)
Valeriy
Valeriy on 23 Oct 2020
Commented: Valeriy on 5 Nov 2020 at 17:54
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

  0 Comments

Sign in to comment.

Accepted Answer

Tim
Tim on 31 Oct 2020
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;
% Make binary mask:
threshold = 0; % Insert threshold here: using 0
bmask = zeros(size(tst));
bmask(tst > threshold) = 1;
% 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 Comment

Valeriy
Valeriy on 5 Nov 2020 at 17:54
Hi Tim,
Excellent solution, thank you very much!
And thanks for very useful applicaton expamples of <max> and <sub2ind> functions
Best regards
Valeriy

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by