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
NOTE:-
Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.
Try
%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)=[]
SEE COMPLETE ANSWER CLICK THE LINK
Comments
Post a Comment