function [out] = reorder(in, orderVec) %reorder Reorders columns of a matrix according to a specification. % % USE % [out] = reorder(in, orderVec) % % in = a matrix or a row vector. % orderVec = a vector of integers. % % out = the reordered matrix. % % For example, in = [1, 2, 4; % 3, 2, 0] % % orderVec = [3, 1, 2], % % reorder(in, orderVec) will return % % [2, 4, 1; % 2, 0, 3] % Tomo Eguchi % 5 February 2002 [nrIn, ncIn] = size(in); lengthOrder = length(orderVec); if lengthOrder ~= ncIn, error('Dimensions did not match.'); end out = zeros(size(in)); for i = 1:lengthOrder, column = orderVec(i); out(:, i) = in(:, column); end