简体   繁体   中英

How to swap in 3D array using MATLAB?

Is this the right statement to swap 3D matrix :

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

A is defined as a 3D matrix of 3x3x3.

Try to visualize you have 3D matrix of 3x3x3 (a cube type). Now you have to swap the column of front face to column of top face. Is this type of swapping possible in MATLAB?

Yes.

This switchs the 2 top rows in A(:,:,2) :

A = reshape(1:27,[3,3,3]);
before = A(:,:,2)
A([1 2],:,2) = A([2 1],:,2);
after = A(:,:,2)

The result:

before =
    10    13    16
    11    14    17
    12    15    18
after =
    11    14    17
    10    13    16
    12    15    18

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM