Generating combination of matrix with 3 elements

I want to optimally generate a 100 matrix, of size MxN using the combination of 3 elements (0,1,2) and save them into seperate mat files. Can someone please help. I dont want to generate the matrix randomly.

댓글 수: 3

What do you mean by 1000 matrix? Do you just want the permutations of (0,1,2)?
p=perms([0 1 2]);
Jan
Jan 2022년 2월 16일
편집: Jan 2022년 2월 16일
I don't get it: Why 100 matrices? What kind of combinations do you mean? If they are not random, what procedure is wanted instead?
perms(0:2)
ans = 6×3
2 1 0 2 0 1 1 2 0 1 0 2 0 2 1 0 1 2
And now?
By the way: This is not twitter: no hash before the tags.
@David Hill @Jan I am sorry about that. I want to generate 100 mat files in more optimal way when it comes to the series of 3 elements in a row. I dont want to repeat the rows in a matrix. My matrix size is 64x12. Is there a way I can do it?

댓글을 달려면 로그인하십시오.

 채택된 답변

David Hill
David Hill 2022년 2월 16일
편집: David Hill 2022년 2월 16일
So each row of 12 elements can only contain the elements [0 1 2] ? Are there any other contrains? How many times can each element be repeated in each row? Why don't you want to generate the matrix randomly?
A=[repmat([0 1 2],1,12)];
N=zeros(64,12,100);
for m=1:100
for k=1:100
M(k,:)=A(randperm(numel(A),12));
end
M=unique(M,'rows');
N(:,:,m)=M(1:64,:);
end

댓글 수: 5

Shourya
Shourya 2022년 2월 17일
편집: Shourya 2022년 2월 17일
Thank you for your response. As I am preparing a dataset for my problem, I want to avoid repeating rows in a single matrix as much as possible. Yes,each row of 12 elements can only contain the elements [0 1 2]. However, there is no limit on the number of repeated elements.
While the above code is helpful, can you please tell me how to save these 100 matrices as separate mat files. I want to generate 100 separate matrix files of size 64x12. I tried using " save(['N' num2str(m) '.mat'],'N');" I am getting each file with 100 matices.
Why not just save a single file that contains all 100 matrices (3rd dimension of N).
save('yourMatrix','N');
Shourya
Shourya 2022년 2월 17일
편집: Shourya 2022년 2월 17일
Thank you. I want just 2D matrix as I will be using each of them seperately as input. Is there a way I can save them?
It is always much easier to code using indexing instead of having a bunch of variables. A 3D matrix (N) can be indexed to obtain all 100 of your 2D matrices.
M1=N(:,:,1);%is a 2D matrix
M2=N(:,:,2);%is a 2D matrix
M3=N(:,:,3);%is a 2D matrix
M100=N(:,:,100);%is a 2D matrix
All 100 of your 2D matrices are stored in a single 3D matrix.
Hello David, I shall do that thank you for the solution!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2022년 2월 16일

댓글:

2022년 3월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by