1

I would like to chain these two functions: 1. select the third dimension of a m x n x 3 matrix 2. flatten the matrix into a (m+n) x 1 vector

Something like: mat(:,:,1)(:) or (mat(:,:,1))(:)

But this is not the correct syntax.So how do I chain these two functions together without assigning some variable to the intermediate result mat(:,:,1) ?

xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88

1 Answers1

2

You can't chain those two indexings in Matlab. But in this case you can substitute the second indexing by a reshape:

reshape(mat(:,:,1),[],1)

If the second indexing is more complicated than just flattening the matrix, the reshape trick will not be applicable in general. In that case there is a way to avoid using an intermediate variable, but it results in cumbersome and ugly code, so it's not recommended.

Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147