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

3 次查看(过去 30 天)
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
  15 个评论

请先登录,再进行评论。

采纳的回答

Rik
Rik 2018-1-7
%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
  18 个评论
Rik
Rik 2018-1-11
Sure. Start off by thinking in a structure way what should happen when. In other words, write psuedocode. Then convert that pseudocode into real Matlab. The first step you should be able to do on your own, the second you can ask follow up questions for on this forum.
tip: Write down explicitly what rules n_rows must obey.
Prabha Kumaresan
Prabha Kumaresan 2018-1-19
I would like to check if
m=[10 20 30 40 50] and
n=[60 70 80 90 100]
how it can be done with respect to the below section
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

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by