Search code examples
matlabmultidimensional-arrayindexingmatrix-indexing

Filtering 3D matrix above certain threshold?


I have two 3D matrices of the same size. Say, A contain values ranging from 0 to 1 and B contains certain discrete values.

I want to extract values from B matrix which are above certain threshold values in A?

Can anyone help me out?


Solution

  • Like this? Have a look at logical indexing.

    % Input.
    B = reshape(1:18, 2, 3, 3)
    A = rand(size(B))
    
    % Threshold.
    thr = 0.75
    
    % Output.
    output = B(A > thr)
    
    B =
    ans(:,:,1) =
       1   3   5
       2   4   6
    ans(:,:,2) =
        7    9   11
        8   10   12
    ans(:,:,3) =
       13   15   17
       14   16   18
    
    A =
    ans(:,:,1) =
       0.80533   0.24370   0.89180
       0.90358   0.22422   0.69243
    ans(:,:,2) =
       0.119366   0.168337   0.771999
       0.206004   0.065481   0.979772
    ans(:,:,3) =
       0.0057303   0.1469925   0.0556628
       0.0454038   0.4122576   0.9847027
    
    thr =  0.75000
    
    output =
        1
        2
        5
       11
       12
       18