xiaolingxiao
xiaolingxiao

Reputation: 4895

How do you compose functions in matlab?

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) ?

Upvotes: 1

Views: 320

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112759

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.

Upvotes: 2

Related Questions