Borrar filtros
Borrar filtros

how to alter this code ???

1 visualización (últimos 30 días)
Firas Al-Kharabsheh
Firas Al-Kharabsheh el 25 de Abr. de 2016
Comentada: Image Analyst el 26 de Abr. de 2016
if i have A matrix like this
A = [ 1 0 1 1 1
0 0 1 0 1
0 1 0 1 1
1 1 0 O 0 ]
S = randi([0 1], [size(A), 5]); %Sk is S(:, :, k) , GENERATE 5 RANDOM MATRIX
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
this code return the matrix which have the MIN value
how to make this code to return the second min value?? like this
z = 5 2 7 9 1
min value is 1
i want to return the 2 where is the second min value

Respuestas (1)

Image Analyst
Image Analyst el 25 de Abr. de 2016
sorted_z = sort(z, 'descend');
secondMin = sorted_z(2);
  2 comentarios
Firas Al-Kharabsheh
Firas Al-Kharabsheh el 25 de Abr. de 2016
thanks but i want to return the matrix in index 2 in the sorted_z
how can i do this
i try this code but its did not work
sorted_z = sort(z, 'ascend')
secondMin = sorted_z(2)
[z2min , mindx] = secondMin
Smin2 = S(:, :, mindx);
Image Analyst
Image Analyst el 26 de Abr. de 2016
S has just ones and zeros in it while z has other integers. So you will not find the min value of z anywhere in S. Try this though:
A = [ 1 0 1 1 1
0 0 1 0 1
0 1 0 1 1
1 1 0 0 0 ]
S = randi([0 1], [size(A), 5]) %Sk is S(:, :, k) , GENERATE 5 RANDOM MATRIX
z = squeeze(sum(sum(bsxfun(@ne, S, A), 1), 2)) %compare A to each page with bsxfun and sum in two dimensions
sorted_z = sort(z, 'descend')
secondMin = sorted_z(2)
% Find location of second smallest value in z
index = find(z == secondMin)
% Smin = S(:, :, minidx) %and return that page

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by