how to add element in a vector ?

2 views (last 30 days)
i want to create a 20 random binary matrix where in every one from these matrix i want to do some calculation
after that i want to find the min value from all matrix and after that i want to return the matrix which have the min value
like that
A = [ 1 1 0 1
0 0 1 0
1 0 1 0
0 1 1 0 ]
for k=1:20
s = randi([0 1],n,m)
z(k) = (sum(sum(A ~= s)));
end
then i want to find the min value from all matrix and after that return the matrix which have the min value like that
if z = [ 4 4 5 3 2 5 7 8 9 7 9 7 8 9 7 5 4 1 7 ]
then i want from the function to return the matrix number 19 because the value of 19 is the min value like that
s19 = [ 1 1 0 1
0 0 1 0
0 0 1 0
0 1 1 0 ]

Accepted Answer

Guillaume
Guillaume on 20 Apr 2016
The simplest way would be to generate all 20 random matrices in one go as pages of a 3D matrix:
A = [ 1 1 0 1
0 0 1 0
1 0 1 0
0 1 1 0 ];
nummatrices = 20;
S = randi([0 1], [size(A), nummatrices]); %Sk is S(:, :, k)
z = squeeze(sum(sum(bsxfun(@ne, S, A), 1), 2)) %compare A to each page with bsxfun and sum in two dimensions
[zmin, minidx] = min(z) %find location of minimum in z
Smin = S(:, :, minidx) %and return that page
  2 Comments
Firas Al-Kharabsheh
Firas Al-Kharabsheh on 25 Apr 2016
how can i return the second min value matrix ??

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!