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

2 visualizaciones (últimos 30 días)
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 comentarios
jaah navi
jaah navi el 17 de En. de 2018
could you please tell me how to store all the C_part in to one cell array

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 7 de En. de 2018
%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 comentarios
Rik
Rik el 11 de En. de 2018
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 el 19 de En. de 2018
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

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by