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 functionfunction 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
MATLAB: How to run the following code for n number of rows and m number of columns.
clustering
Related Question
- Could anyone help me to solve the following issue
- Could anyone help me to check with the following code
- Could anyone clarify the doubt with respect to the following code
- How to make sure that if creating a diagonal matrix from another, the matrices are the same size
- Replace all numbers within a matrix
- How to change the rng value
Best Answer