MATLAB: Please help me to get with the scenerio mentioned

matrix manipulation

If A is a matrix of (n,m) how to group n(rows) in a random manner such that the values present in (n) in each row should be in all the rows present in that corresponding group.
say for example
if A=(5,3)
and it has the value of [1 0 0;0 2 0;3 0 0;0 0 4;0 5 0]
after grouping in a random manner it should be [1 5 4;3 2 0;3 2 0;1 5 4;1 5 4]
(in this example row1,row4 and row5 are in one group where as row2 and row3 are in another group).
The same scenario should be applied to (n,m) indicates n number of rows and m number of columns.

Best Answer

[r,c]=find(A(1:size(A,1),:)~=0);
for i=1:numel(c)
if sum(ismember([1 4 5],r(i)))>0
A([1 4 5],c(i))=A(r(i),c(i));
elseif sum(ismember([2 3],r(i)))>0
A([2 3],c(i))=A(r(i),c(i));
end
end