MATLAB: Delete rows in a matrix to the largest power of 2.

matrixrows

For example if i have a 10×2 matrix, since 10 is not a power of 2 i want to delete rows to make it a 8×2 matrix, where 8 is the largest power of 2.

Best Answer

One approach:
M = randn(10,2); % Create Matrix
if rem(log2(size(M,1)),1) ~= 0
rowlim = 2^(nextpow2(size(M,1))-1); % Calculate Row Limit
M = M(1:rowlim,:); % Limited Matrix
end
.