image thumbnail
from Reshape 1D to 2D with Balance by Dr. Murtaza Khan
Reshapes 1D array to 2D array with size of rows and columns of B is balance

B=reshape1dto2dbalance(A)
% % If possible then reshapes 1D array (vector) 'A' into 2D array 'B' such that:
% % abs(NC-NR) is minimized
% % where
% % NR=number of rows in B
% % NC=number of columns B
% % Note:  Elements of A are placed column-wise in B.

function B=reshape1dto2dbalance(A)

L=length(A);
f=factor2(L); %f is vector that holds factors of L

for k=1:length(f)
    f1=f(k);
    f2=L/f(k);
    %%f1*f2=L;
    
    %f1f2d holds f1,f2 and difference of factors values
    f1f2d(k,1:3)=[f1,f2,abs(f1-f2)]; 
end

[val,ind]=min(f1f2d(:,3));
B=reshape(A,f1f2d(ind,1),f1f2d(ind,2));

% % % --------------------------------
% % % Author: Dr. Murtaza Khan
% % % Email : drkhanmurtaza@gmail.com
% % % --------------------------------

% % Accompaneid program factor2.m is written by
% % Brett Shoelson, Ph.D.
% % shoelson@helix.nih.gov

Contact us at files@mathworks.com