Finding the minimum value of each row of a matrix
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jen
el 17 de Nov. de 2014
Respondida: Azzi Abdelmalek
el 17 de Nov. de 2014
I've created an arbitrary matrix called 'mat.'
Here's the code I've written to find and display the minimum value of each row of the matrix:
[m,n] = size(mat);
for i = 1:m;
mvec = mat(i,:);
mmin = mvec(1);
for j = 2:length(mvec)
if mvec(j) < mmin
mmin = mvec(j);
fprintf('The min of row %d is %d \n',i,mmin)
end
end
end
Depending on the matrix, this will display multiple values (every element smaller than the first element) for a row and/or skip a row entirely. How do I fix this so that one minimum value for each row will be displayed?
Note: I'm trying not to use the built-in min function.
0 comentarios
Respuesta aceptada
Azzi Abdelmalek
el 17 de Nov. de 2014
Just change the place of fprintf in your code
mat=[11 11 2 3; 5 2 6 9]
[m,n] = size(mat);
for i = 1:m;
mvec = mat(i,:);
mmin = mvec(1);
for j = 2:length(mvec)
if mvec(j) < mmin
mmin = mvec(j);
end
end
fprintf('The min of row %d is %d \n',i,mmin)
end
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Custom Layers 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!