MATLAB: How to run the following code for n number of rows and m number of columns.

clustering

A=[ 1 2 3 4 5;
11 12 13 14 15;
6 7 8 9 10;
21 22 23 24 25;
26 27 28 29 30];
B=[0 0 41 0 0;
45 0 0 0 0;
0 43 0 0 0;
0 0 0 42 0;
0 0 0 0 44];
C_after_step_1=cluster_rows(A,B,[1 4])
% C_after_step_1=cluster_rows(B_part)
C=cluster_rows(A,C_after_step_1,[2 3 5])
% C=cluster_rows(A,C_after_step_1)
calling function
function C=cluster_rows(A,B,rows)
%extract the parts of the matrices
A_part=A(rows,:);
B_part=B(rows,:);
%sum along the the vertical axis to get indices for the non-0 columns
non_0=sum(B_part);
%Repeat the vector back to the same size as the partial matrix. It will be
%converted to a logical later on.
non_0=repmat(non_0,length(rows),1);
%create a copy of B, as that is the basis for C
C=B;C_part=B_part;
%for all non-zero columns, replace the 0 positions with the values from A
C_part(non_0 & C_part==0)=A_part(non_0 & C_part==0);
%paste the edited partial matrix back
C(rows,:)=C_part;
end

Best Answer

%generate random data for A and B (replace this with your real data)
m=50;n=2*m;
A=randi(m*n,m,n);
%B=randi(m*n,m,n).*(rand(m,n)>0.9);
B=[diag(1:n),diag(n+1:m)];
unused_rows=1:m;C=B;
while ~isempty(unused_rows)
%This method of random selection will have a bias to start off with
%large groups and end with small ones. If this is a problem, you will
%have to devise a method yourself that results in a more stable
%distribution. Taking a square root and rounding the result already
%helps a bit to equalize group size.
n_rows=ceil(sqrt(randi(numel(unused_rows))));
rows=unused_rows(randsample(length(unused_rows),n_rows));
[~,idx]=find(ismember(unused_rows,rows));
unused_rows(idx)=[];
%If you want to see the rows that were grouped, uncomment the next two
%lines of code. (e.g. if you want to see the group size)
%fprintf('%02d ',rows)
%fprintf('\n')
%apply the row clustering
C=cluster_rows(A,C,rows);
end