find nonzero minimum element in a matrix
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
negin tebyani
el 8 de Feb. de 2018
Comentada: negin tebyani
el 9 de Feb. de 2018
hello every one, I am new in matlab and I have a simple question. I have a 5 * 10 matrix which concludes numbers less than 1 what I need to do is that I have to find the minimum element in the matrix in a loop and do some functions on that according to the row and col of this element. so the row and col are imortant. after finding this element, based on the results of the functions that is done on it, I have to delete the whole row or the whole column of this element. and this loop goes on until all the rows or all the columns are removed. at first I did this with (d1 and d2 are matrix's size):
while (d1~=0 || d2~=0)
do stuff
if (something)
RequiredMinPower(:,column)=[];
else
RequiredMinPower(row,:)=[];
end
but after debugging the program, I understood that it is giving me the wrong row and col from the second loop to the end, since I am removing one row or one column each time. so it just makes the matrix smaller. I need the real row and col according to the first 5 * 10 matrix.
so I decided to turn all elements of that row or column to zero instead of removing the whole. but now since there are 0's in the matrix, it picks them as the min element. so I was wondering how can I do this? so it picks the min element and not the zeros?
and also "I find the min element like this:
[minval, row] = min(min(RequiredMinPower,[],2));
[minval, column] = min(min(RequiredMinPower,[],1));
and I also have to change the while conditions, how can I do this?
0 comentarios
Respuesta aceptada
Jos (10584)
el 8 de Feb. de 2018
Why not set the values to Inf, rather then zero? Or to Nan and use nanmin instead?
To find the overall minimum of a matrix M use the colon-operator:
[minval, minidx] = min(M(:))
and use ind2sub to get the row and column subindices:
[rmin,cmin] = ind2sub(size(M), minidx)
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!