I have a 3D matrix of a movie (say a matrix M of size J*K*L). I want to flip left right each frame. Using fliplr(M) doesn't work as M must be a 2-D matrix. I know I can use a for loop of the following:

 for ii=1:size(M,3)
     M(:,:,ii)=fliplr( M(:,:,ii) )
 end

Is the a "vectorized" way to do it?

More generally speaking, is the a "vectorized" way to do any of Matlab's matrix manipulations (flipud, repmat, etc) in this case?

有帮助吗?

解决方案

Alternatively, you can use simple indexing:

>> M = rand(3,4,5);
>> M(:, end:-1:1, :);

This is a lot faster and less resource intensive than flipdim, and I think a lot cleaner too.

However, for some people, this particular usage of the end keyword is confusing, so if you're one of those people, flipdim will do just fine :)

其他提示

I think you are looking for

M = flipdim(M, 2);

This flips an N dimensional matrix along the dimension you specify as the second parameter. Thus, the flipud could be replaced with

M = flipdim(M, 1);

Not sure where you are going with the repmat question, but I often find I can use bsxfun instead of repmat. Look it up.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top