Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要转换一个矩阵:
X = [1 2; 3 4; 5 6; 7 8] X = 1 2 3 4 5 6 7 8
至
X = [1 2; 5 6; 3 4; 7 8] X = 1 2 5 6 3 4 7 8
并对具有任意行数的矩阵执行此操作。这样在一个有 200 行的矩阵中,第 101 行将成为第 2 行,第 102 行将成为第 4 行,依此类推。如何在 MATLAB 中实现这一点?
对于偶数行的数组,请执行以下操作:
nRows = size(X,1); idx = [1:nRows/2;nRows/2+1:nRows]; X_rearranged = X(idx(:),:);
对于奇数行的数组,您将 1 添加到nRows, 并使用idx(1:end-1)而不是idx(:)
nRows
idx(1:end-1)
idx(:)
您可以使用:
X = [1 2; 3 4; 5 6; 7 8] Y = [a(1,:); a(3,:); a(2,:); a(4,:)]