[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

selecting 'first' element/vector/matrix of an arbitrary n-dim array

From: Nicholas Jankowski
Subject: selecting 'first' element/vector/matrix of an arbitrary n-dim array
Date: Mon, 7 Dec 2020 13:06:09 -0500

i'm sure there's a compact, efficient way to do this, but it's eluding me and my google-fu is failing:

give a n-dimensional matrix A (n unknown apriori), and a user specified dimension to operate on, return the first 'part' of the array for that dimension, preserving shape:

e.g, for a 3D array, dim 2:

B = A(:,1,:)

for dim 3:

B = A(:,1,:)

for a 4D array, dim 2:

B = A(:,1,:,:)

for dim 4:

B = A(:,:,:,1)


what's an efficient way to extract that subarray for any number of dimensions? i assume I could do something clever directly with subsasgn and expanding a cell array of some number of ':' with {:}, but i'm sure I'm missing something cleaner, no?  

(does not need to be matlab compatible)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]