3 matrices named as A
, B
and C
. Each matrix have random M x N
dimension. However, the N
(column number) are equal. Randomly generate 1 row from each of the matrix and put into new matrix (named as Fdata
) one by one, and then randomly generate 1 row from the each of the matrix again but can not be repeated from the previous randomly row generation and put into Fdata
one by one again..do this accordingly...if one of the matrix is randomly generated once for each row without repeating, then the rest of the row can be randomly repeated again until the maximum rows from one of the matrix.
If A
is matrix of 5x5, B
is a matrix of 2x5 and C
is the matrix of 3x5, the Fdata
will be a 15x5 matrix
example:
A =
6 6 4 7 8
4 7 1 6 9
3 5 8 0 1
6 5 8 1 2
9 4 2 0 1
B =
4 7 2 8 5
5 6 0 1 2
C =
1 6 4 5 1
8 6 7 1 3
3 0 4 5 8
The final data i wish to get is
Fdata =
4 7 1 6 9 %randomly from matrix A
5 6 0 1 2 %randomly from matrix B
1 6 4 5 1 %randomly from matrix c
6 6 4 7 8 %randomly from matrix A (but not repeated from previ开发者_如何学Goous randomly generated row)
4 7 2 8 5 %randomly from matrix B (but not repeated from previous randomly generated row)
3 0 4 5 8 %randomly from matrix C (but not repeated from previous randomly generated row)
3 5 8 0 1 %randomly from matrix A (but not repeated from previous randomly generated row)
4 7 2 8 5 %randomly from matrix B, repeated row because the row of matrix B is fully randomly generated
8 6 7 1 3 %randomly from matrix C (but not repeated from previous randomly generated row)
9 4 2 0 1 %randomly from matrix A (but not repeated from previous randomly generated row)
....
what code shall i write to get the Fdata
?
2nd question:
let say Fdata=xlsread('abc.xls,);
original Fdata
has 5x2 matrix, after some codes, Fdata
become 5x5 matrix..then xlswrite('abc.xls',Fdata,'sheet2');
..error occur...??? Index exceeds matrix dimensions
...how can i solve this problem?
Quick working try, it can be optimized but then a few more details are needed: how many iterations are you doing ? is there a breaking condition ? Will you always use three matrices to feed Fdata ?
Here is a working sample of code:
function Fdata=buildFdata
A=rand(5,5);
B=rand(2,5);
C=rand(3,5);
selectedA=[];
selectedB=[];
selectedC=[];
n=max([size(A,1) size(B,1) size(C,1)]);
Fdata=zeros(3*n,5);
for i=1:3:3*n-2
[Fdata,A,selectedA]=randomfeed(Fdata,A,selectedA,i);
[Fdata,B,selectedB]=randomfeed(Fdata,B,selectedB,i+1);
[Fdata,C,selectedC]=randomfeed(Fdata,C,selectedC,i+2);
end
end
function [Fdata,Mat,selectedMat]=randomfeed(Fdata,Mat,selectedMat,i)
if(~isempty(Mat))
index=randline(size(Mat,1));
Fdata(i,:)=Mat(index,:);
selectedMat(end+1,:)=Mat(index,:);
Mat(index,:)=[];
else
index=randline(size(selectedMat,1));
Fdata(i,:)=selectedMat(index,:);
end
end
function n=randline(nlines)
if(nlines==1)
n=1;
else
n=randi(nlines-1)+1;
end
end
精彩评论