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

2 次查看(过去 30 天)
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