How can I select two out of 6 points for every page without loops?

lit · Aug 11, 2023 · 2.3K views
Question
I am trying to select two out of 6 points in an array whose size is 6x3xn. These two points are not in the same row on each page as I am trying to select those two of the six points that meet a certain condition. Let's assume that condition is that every coordinate is between 0 and 30.   Is there away to do this without loops?
Expert Answer
Profile picture of Prashant Kumar
Prashant Kumar PhD Expert
Answered Nov 20, 2025
n    = 2;
X    = randi(40, 6, 3, n)

 

X = 
X(:,:,1) =

    25    18    19
    19    23    18
    26     5     7
    15    39    10
    22    35    10
    26    35    23


X(:,:,2) =

    35    22     6
    25    29     2
     5    39     5
    34    33    36
    32    14    17
     5    22    33

 

mask = all(X >= 0 & X <= 30, 2)

 

mask = 6×1×2 logical array
mask(:,:,1) =

   1
   1
   1
   0
   0
   0


mask(:,:,2) =

   0
   1
   0
   0
   0
   0
Y    = reshape(X(cat(2, mask, mask, mask)), [], 3)

 

Y = 4×3
    25    23     7
    19     5    25
    26    19    29
    18    18     2

 

Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!