필터 지우기
필터 지우기

how to generate a 20 random matrix and according to equation ?

조회 수: 1 (최근 30일)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 23일
편집: John D'Errico 2016년 4월 23일
how to generate a 20 random matrix and according to equation calculate them ,
after that i want to re-order them from the small value to the bigger value and i want to return the two smallest matrix like that
s1 = [ 0 1 1
0 0 1
1 1 1 ]
s2 = [ 1 0 0
1 1 1
0 0 1 ]
s3 = [ 0 0 1
0 1 0
1 0 1 ]
.
.
.
s20 = [ 1 0 1
1 1 0
0 1 1 ]
the equation is
z = sum(sum(A~s) ) >>>> calculate for all a 20 matrix where A is
A = [ 1 1 0
1 1 1
0 1 1 ]
for example
z = sum(sum(A~=s1) ) >>>> z = 5
z = sum(sum(A~=s2) ) >>>> z = 2
z = sum(sum(A~=s3) ) >>>> z = 7
z = sum(sum(A~=s20) ) >>> z = 3
sort_value = 2 , 3 , 5 , 7
and finally how to return the matrix in position 1 and two in the sort_value like that
p1 = [ 1 0 0
1 1 1
0 0 1 ]
p2 = [ 1 0 1
1 1 0
0 1 1 ]

답변 (1개)

John D'Errico
John D'Errico 2016년 4월 23일
편집: John D'Errico 2016년 4월 23일
DON'T. Don't do this.
Instead, learn to use 3-dimensional arrays. Just generate a 3x3x20 array, then work with the planes of that matrix when you need to work with any of them.
So, instead of writing s1, write s(:,:,1). But when you need to access the n'th array from that set, you need only do s(:,:,n). Other methods would be far more difficult to access the nth array, since you cannot simply type s_n or anything like that.
But things like sums now get far easier to do, and you can compute those sums for all matrices in one operation. And your workspace will now be far less cluttered.
Finally, operations like sorts (as you stated you wish to do) now become TRIVIAL.
Or, learn to use cell arrays. So now you will access the n'th array as simply s{n}, with curly braces.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by