2

Context

You have a 3D variable A

rng('default')
A = randi(100,5,7,3);

Problem

You want to get a column vector with all the values of a given slice along the third dimension of A, e.g.

Tmp = A(:,:,2);
out = Tmp(:);

Question

Is there a built in way to do this directly without having to use a temporary variable or a function (i.e. with a combination of brackets, ...)? The closest to what I look for I have found yet would be

out = reshape(A(:,:,2),[],1)

Which I find a bit "heavy". I was looking for something like (A(:,:,2))(:) but it doesn't work in MATLAB.

The fact that they added the input all in functions like any would suggest that there is not but I figured I'd still ask

BillBokeey
  • 3,168
  • 14
  • 28
  • How much more "built-in" of a function do you want than `reshape`? – Wolfie Jul 11 '22 at 11:25
  • I've edited to try and give more sense to what I'm asking. I was looking for something like `(A(:,:,2))(:)` but it does not work in MATLAB. I figured there might be a valid way to do it – BillBokeey Jul 11 '22 at 12:15
  • 1
    If it's something you do often then you could make a wrapper, i.e. `v = @(X) X(:);` or equivalently `v = @(X) reshape(X,[],1);` then `out = v( A(:,:,2) );`, but [reshape is fast](https://stackoverflow.com/questions/36062574/why-is-reshape-so-fast-spoiler-copy-on-write), so it's just syntax you're dodging. I think that just leaves you asking about indexing an array without a temp variable, which is a [duplicate](https://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it) of one of the highest voted MATLAB questions here – Wolfie Jul 11 '22 at 12:51
  • 1
    Also note that you can always index a matrix using [linear indexing](https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html). In your case, `Tmp(20)` returns the same value as `out(20)`. Depending on your use case (if you have to loop over all elements), that works and avoids the `reshape`. If you want to reshape for linear algebra operations, I would go with the `reshape` function as it makes your intention clear. – nonDucor Jul 11 '22 at 13:04
  • @Wolfie Fair enough, the answer I was looking for is "no" then :) I should delete the question right? – BillBokeey Jul 11 '22 at 13:45
  • 1
    No, don’t delete the question. Let us know how you will use the resulting vector, there might be workarounds. One thing to consider is to reshape the full matrix (no data will be copied, it’s fast): `A = reshape(A,[],size(A,3))`. – Cris Luengo Jul 11 '22 at 13:47
  • @CrisLuengo At first (Before seeing that one can use `all` as an input for `any`), it was to check for a condition over a whole slice of a 3D matrix, without having to declare a temp variable (I was looking for a short version because I wanted to put it as a conditional breakpoint for debugging). Then, it was just pure curiosity – BillBokeey Jul 11 '22 at 14:27
  • 1
    That's fair. Note that you can (since fairly recently) specify multiple dimensions in `all`, for example `all(A,[1,2])` would return a 1D vector, with the result of `all` for each slice. Previously this would have been `all(all(A,1),2)`. – Cris Luengo Jul 11 '22 at 14:35
  • 1
    If you wanted to use the _first_ dimension instead of the third you could do `A(2,:)`. That last `:` collapses (linearly indexes) all _trailing_ dimensions – Luis Mendo Jul 11 '22 at 15:32
  • Oh wow. Does this mean good practice should be to organize the data so that the default "slicing" parameter (If this makes any sense) is the along the first dimension? – BillBokeey Jul 11 '22 at 15:36
  • Just a note, in case it helps - you can always index arrays with linear indexing - in your example, you can access `Tmp(10:20)` without first converting with `out = Tmp(:)`. So, depending on your application, it may not be necessary to explicitly flatten the array. – Brionius Jul 12 '22 at 00:24
  • @BillBokeey I don't see that as good (or bad) practice in general. There are other factors. For example, traversing an array along the first dimension is usually fastest than in other dimensions, because the accessed values are stored contiguously in memory, whereas for other dimensions there is an implied stride in the memory representation (which is necessarily linear) – Luis Mendo Jul 12 '22 at 13:27

0 Answers0